0

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.

John Topley
  • 113,588
  • 46
  • 195
  • 237
Bhanu Prakash
  • 1,493
  • 8
  • 20
  • 1
    What do you mean when you say "**achieve this using storyboards**"? – Popeye Nov 11 '13 at 13:00
  • @Popeye yes, but without declaring the ViewControllerB's title property in .h file. It should be in .m file(private scope) as mentioned in above example. – Bhanu Prakash Nov 11 '13 at 13:09
  • Your question isn't clear at all. – Popeye Nov 11 '13 at 13:32
  • @Popeye, what clarification you needed from the above question?I am able to explain my problem. – Bhanu Prakash Nov 11 '13 at 13:57
  • Well we get you want a private property or something o_O from the comments but where do you say that in your question and what does that have to do with storyboards? Your question just doesn't make sense. – Popeye Nov 11 '13 at 14:02

3 Answers3

2

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];
}
vikingosegundo
  • 52,040
  • 14
  • 137
  • 178
0
UIStoryboard *storyboard=self.storyboard;

ViewControllerB *vc=(ViewControllerB*)[storyboard instantiateViewControllerWithIdentifier:@"Scene_Id"];

[vc setTitle:@"your Text"];

[self.navigationController pushViewController:vc animated:YES];
vikingosegundo
  • 52,040
  • 14
  • 137
  • 178
Sandeep Ahuja
  • 931
  • 6
  • 17
0

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.

Community
  • 1
  • 1
pojo
  • 5,892
  • 9
  • 35
  • 47