NSNotificationCenter 用于呈现多种模式?
我有一个应用程序委托,其默认视图应该位于一个模态视图控制器之前,有时是两个模态视图控制器。因此,在应用程序委托的 didFinishLaunchingWithOptions
中,我检查是否需要第一个模态视图控制器,并且在这种情况下显示第一个模态视图控制器。
关闭第一个模态视图控制器(使用[selfmissModalViewControllerAnimated:YES];
)后,我可能想显示第二个模态视图控制器。应用程序委托也知道这一点。
所以我的解决方案是使用 NSNotificationCenter 告诉应用程序委托第一个模式视图控制器现在已被解雇。发生这种情况时,如果需要,应用程序委托可以显示第二个模式视图控制器。
它工作得很好,但是有更干净的解决方案吗?我认为 NSNotificationCenter 真的很丑陋。
关于一次显示多个模态视图控制器的注意事项
我确实尝试在 didFinishLaunchingWithOptions
内显示第一个和第二个模态视图控制器,但我从未让它工作。这是我尝试过的:
- (BOOL)application:(UIApplication *)application
didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
[window setRootViewController:tabBarController];
[self.window makeKeyAndVisible];
[tabBarController presentModalViewController:pinViewController animated:NO];
if([self needsActivation]) {
[tabBarController presentModalViewController:activationViewController
animated:YES];
}
}
更新:上面的代码可以通过以下更正来使用:
if([self needsActivation]) {
[pinViewController presentModalViewController:activationViewController
animated:YES];
}
I have an app delegate, whose default view should be preceeded by a modal view controller, and sometimes by two modal view controllers. So in the app delegate's didFinishLaunchingWithOptions
, I'm checking if there is need for, and in that case displays, the first modal view controller.
Upon dismissing the first modal view controller (using [self dismissModalViewControllerAnimated:YES];
), I may want to display the second modal view controller. This is known by the app delegate as well.
So my solution was to use NSNotificationCenter
to tell the app delegate that the first modal view controller now have been dismissed. When that happens, the second modal view controller can be displayed by the app delegate, if it is needed.
It works fine, but is there a cleaner solution? I think NSNotificationCenter
is really ugly stuff.
Note on displaying multiple modal view controllers at once
I did try to display the first AND the second modal view controller inside of didFinishLaunchingWithOptions
, but I never got it working. Here's what I tried:
- (BOOL)application:(UIApplication *)application
didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
[window setRootViewController:tabBarController];
[self.window makeKeyAndVisible];
[tabBarController presentModalViewController:pinViewController animated:NO];
if([self needsActivation]) {
[tabBarController presentModalViewController:activationViewController
animated:YES];
}
}
UPDATE: The above code works with the following correction:
if([self needsActivation]) {
[pinViewController presentModalViewController:activationViewController
animated:YES];
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在这种特殊情况下,正如我所想,不需要
NSNotificationCenter
。我曾尝试显示多个模式视图控制器,但我犯了一个错误。当从模态视图控制器 A 显示模态视图控制器 B 时,它工作正常。我曾尝试从父视图控制器中呈现模态视图控制器 A 和 B。
因此,当在层次结构中呈现模式视图控制器时,不需要
NSNotificationCenter
。视图控制器正在自行解散,动画可以正常工作,并且我朝着让 UIKit 屈服于我的意愿又迈进了一步。我已经编辑了问题中的代码,现在工作正常。
In this particular case, there was no need for
NSNotificationCenter
, as I thought. I had tried to display multiple modal view controllers, but I'd made an error.When displaying the modal view controller B from the modal view controller A, it works fine. I had tried presenting modal view controller A and B from a parent view controller.
So when presenting modal view controllers in a hierarchy instead, there is no need for
NSNotificationCenter
. The view controllers are dismissing themselves, animations works and I'm a step further towards bending the UIKit to my will.I've edited the code in my question, which now works fine.