I have the following problem:
I created a iOS 5 SDK application with Storyboard that contains a TabBar and three ViewControllers.
From another Class (Receiver.m) I want to access a UI Label on e.g. ThirdViewController.m which is one of the thee ViewControllers of the TabBar.
In ThirdViewController.h i got
@interface ThirdViewController : UIViewController {
}
@property (weak, nonatomic) IBOutlet UILabel *textlabel1;
@end
and in ThirdViewController.h:
@implementation ThirdViewController
@synthesize textlabel1;
...
This seems OK, since i can set the label properties from within the ThirdViewController instance.
Now to get access from within Receiver.m I use:
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:[NSBundle mainBundle]];
ThirdViewController *myVC = [storyboard instantiateViewControllerWithIdentifier:@"ThirdViewController"];
myVC.textlabel1.text = @"Hello";
This does not work. I do not want to create a new instance of ThirdViewController but access the existing one to update just the UILabel. My Storyboard in Project Navigator is default named MainStoryboard.storyboard .
Am I doing something wrong here with getting the ThirdViewController classes instance?
The error says:
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason:
'Storyboard (<UIStoryboard: 0x190590>) doesn't contain a view controller with
identifier 'ThirdViewController''
Thanks!
Storyboard doesn’t give you a way to access the already presented version of a view controller – as you’ve found, the method you are using creates a new instance.
I would question the structure of your code if you are expecting an external object to update a specific label belonging to a view controller. This is not a typical design pattern as it makes a tight dependency between the two objects. Usually you would set your view controller to be the delegate of your Receiver object (if it is created by that view controller), or your receiver object would send a notification that your third view controller is listening for.