我有以下问题: 我创建了一个包含TabBar和三个ViewController的Storyboard的iOS 5 SDK应用程序。 从另一个类(Receiver.m)中,我想访问例如ThirdViewController.m是TabBar的ViewController之一。iOS:从另一个类访问故事板TabBar ViewController
在ThirdViewController.h我得到
@interface ThirdViewController : UIViewController {
}
@property (weak, nonatomic) IBOutlet UILabel *textlabel1;
@end
和ThirdViewController.h:
@implementation ThirdViewController
@synthesize textlabel1;
...
这似乎是确定的,因为我可以从ThirdViewController实例中设置的标签属性。 现在从内部Receiver.m可以访问我用:
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:[NSBundle mainBundle]];
ThirdViewController *myVC = [storyboard instantiateViewControllerWithIdentifier:@"ThirdViewController"];
myVC.textlabel1.text = @"Hello";
这是行不通的。我不想创建ThirdViewController的新实例,但访问现有的实例只更新UILabel。 Project Navigator中的我的故事板默认名为MainStoryboard.storyboard。 我在这里得到ThirdViewController类的实例做错了吗?
错误说:
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason:
'Storyboard (<UIStoryboard: 0x190590>) doesn't contain a view controller with
identifier 'ThirdViewController''
谢谢!
好吧,这是我担心的。那么有没有其他的方式(我发现像AppDelegate.window.ThirdViewController这不起作用),以某种方式获得ThirdViewController实例通过ViewController'层次'?感谢您的建议,我知道这样的事实,它不是好的风格,代码会改变,但由于应用程序逻辑丢失,我试图直接访问ViewController。 – ZCDev 2012-02-04 09:30:01
你必须得到标签栏控制器(应用程序委托的根视图控制器。窗口),然后从标签栏控制器的viewcontrollers数组中获取第三个视图控制器。 – jrturton 2012-02-04 09:54:07
'AppDelegate.window.UINavigationController.ThirdViewController'不会让我那里。有没有人曾经这样做,并愿意分享一些代码片段?我将不胜感激。否则,我将不得不改变一些代码的结构。 – ZCDev 2012-02-05 18:08:41