我可以在第一个viewDidload中加载第二个viewDidload吗?

时间:2014-08-20 17:06:47

标签: ios objective-c ios7 nsnotificationcenter

大家好我有一个双视图控制器。

我的firstviewcontroller有一个按钮,这个按钮发送NSNotification,secontviewController接收此通知,NSLOG接收任何字符串。

但如果我不加载secontView;我的通知不起作用.. 这个代码在我的firstViewController.m

-(IBAction)tapper:(id)sender {


   [[NSNotificationCenter defaultCenter] postNotificationName:@"Twitter" object:nil];
}

这个代码在我的secontViewController.m

- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector( receiveNotificaiton: ) name:@"Twitter" object:nil];

}


-(void)receiveNotificaiton: (NSNotification *) notification {
NSLog(@"TWİTTER");
}

如何在第一个viewDidload或其他东西中加载第二个viewDidload?

3 个答案:

答案 0 :(得分:1)

听起来您希望第一个视图控制器呈现第二个视图控制器。正确的吗?

如果是这样,您不应该使用通知。您可以直接显示第一个视图控制器,查看"Presenting View Controllers from Other View Controllers"

答案 1 :(得分:0)

将通知发布到尚未存在的视图控制器上的方法不正确,因为(正如您所说)它不能执行构建成为观察者的指令。因此,解决方案可以存在于第二视图控制器并传递通知信息。如果第二个视图控制器是顶视图控制器以及其他发布通知的内容,则使用通知中心是一个很好的解决方案。我希望我帮助过你。

答案 2 :(得分:0)

我用prepareForSegue方法解决了我的问题,把它们都解决了。

相关问题