Reputation: 1483
Let's assume we have 2 view controllers named ViewControllerA and ViewControllerB.
I have following intialization mechanism when I did not use the storyboard.
//ViewControllerA.h
@interface ViewControllerA : UIViewController
@end
//ViewControllerA.m
@implementation ViewControllerA
- (IBAction)showViewControllerB:(id)sender {
ViewControllerB *vcB = [[ViewControllerB alloc] initWithTitle:@"Test"];
[self.navigationController pushViewController:vcB animated:YES];
}
@end
//ViewControllerB.h
@interface ViewControllerB : UIViewController
- (id)initWithTitle:(NSString *)title;
@end
//ViewControllerB.m
@interface ViewControllerB()
@property (nonatomic, retain) NSString *title;//Private scope.
@end
@implementation ViewControllerB
- (id)initWithTitle:(NSString *)title {
self = [self init];
if(self)
{
_title = title;
}
return self;
}
@end
How can I achieve this using storyboards without declaring the title property in public scope (ViewControllerB.h)?
Thanks in advance.
Upvotes: 0
Views: 1044
Reputation: 931
UIStoryboard *storyboard=self.storyboard;
ViewControllerB *vc=(ViewControllerB*)[storyboard instantiateViewControllerWithIdentifier:@"Scene_Id"];
[vc setTitle:@"your Text"];
[self.navigationController pushViewController:vc animated:YES];
Upvotes: 0
Reputation: 52237
You can use KVC to access the property.
- (IBAction)showViewControllerB:(id)sender
{
ViewControllerB *viewcontroller = [self.storyboard instantiateViewControllerWithIdentifier:@"ViewControllerB"]; // corresponding to the storyboard identifier
[viewcontroller setValue:@"Hello World" forKey:@"title"];
[self.navigationController pushViewController:viewcontroller animated:YES];
}
Upvotes: 2
Reputation: 6042
If you really need to keep the property private you could attach data to vcB
object using objc_setAssociatedObject
. Or you could save the property in the AppDelegate instead, and fetch it from there when ViewControllerB initializes.
Upvotes: 0