简体   繁体   English

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

[英]Can i load second viewDidload in first viewDidload?

Hi guys i have a dual viewcontroller. 大家好,我有一个双重viewcontroller。

My firstviewcontroller have a button and this button send NSNotification and secontviewController receive this notification and NSLOG any string. 我的firstviewcontroller有一个按钮,此按钮发送NSNotification,secontviewController接收此通知,NSLOG任何字符串。

But if i dont load secontView; 但是,如果我不加载secontView; my Notification is dont work .. This codes in my firstViewController.m 我的通知不起作用..此代码在我的firstViewController.m中

-(IBAction)tapper:(id)sender {


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

and this codes in my secontViewController.m 这是我的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");
}

How can i load second viewDidload in first viewDidload or something else? 我如何在第一个viewDidload或其他东西中加载第二个viewDidload?

It sounds like you want your first view controller to present the second one. 听起来您想要第一个视图控制器显示第二个视图控制器。 Correct? 正确?

If so you should not be using notifications. 如果是这样,则不应使用通知。 You can simply present the second view controller from the first, take a look at "Presenting View Controllers from Other View Controllers" 您可以简单地从第一个显示第二个视图控制器,看看“从其他视图控制器呈现视图控制器”

It's not the right way post a notification to a view controller which doesn't exist yet because (as you said) it can't execute the istruction for becoming observer. 这不是将通知发布到尚不存在的视图控制器的正确方法,因为(如您所说)它无法执行要成为观察者的指令。 So a solution can be present the second view controller and passing the notification information. 因此,可以在第二视图控制器中提供解决方案并传递通知信息。 Using the notification center is a good solution if the second view controller is the top view controleller and something else post the notification. 如果第二个视图控制器是顶视图控制器,并且其他人发布了通知,则使用通知中心是一个很好的解决方案。 I hope i helped you. 我希望我能帮助你。

我用prepareForSegue方法解决了所有问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM