一个View Controller A,弹出了一个Modal View Controller B,B又弹出了一个Modal ViewController C⋯⋯and so on。
如果你在第n个Modal View Controller的时候,想回到第一个View Controller A,或者它前面的第m个ModalView Controller C,那怎么办?
你一定想,一层层地解散呗。没错,我一开始也是这样干的:在C里解散D,在B里解散C,在A里解散B。那么你会获得一个“试图解散当前未显示的View Controller”错误。
苹果的思维非同凡响,其实你只需要解散一个Modal View Controller就可以了。即处于最底层的View Controller,这样处于这个层之上的ModalView Controller统统会被解散。
例如:你只用在A中解散B,则C、D、E、F……统统会自动被解散了。
问题在于,你在F中如何获得A? 如果是iOS 4,你可以使用parentViewController来获得当前Modal ViewController的“父View Controller”并解散自己。如果是iOS 5,你就得用presentingViewController
这样,你可以不停地parentViewController.parentViewController.parentViewController 下去就可以了。
代码:
if ([self respondsToSelector:@selector(presentingViewController)]) {
[self.presentingViewController.presentingViewControllerdismissModalViewControllerAnimated:YES]; // for IOS 5+
} else {
[self.parentViewController.parentViewControllerdismissModalViewControllerAnimated:YES]; // for pre IOS 5
}
问题是,有时候你不好确定当前Modal View Controller是链中的哪一级。比如有一个工作流,它在B处有两个分支,一个是A->B->C->D,一个是A->B->D,走哪一个分支,依赖于用户的选择。不管D或者F,处理完之后都要返回到A来进行刷新。
在D这个地方要返回A时,你不好决定要用几个parentViewController才能返回A。因为可能是3个(分支1),也可能是2个(分支2)。这样的情况下你只有用协议或者委托了。把A作为一个delegate,沿着工作流不停地传递下去,这样不管走分支1还是分支2的流程,D都始终有一个A的引用。当D的工作完成时,直接调用A实现的协议方法,在A的这个方法中解散B就可以了。- (void)viewDidLoad
{
[super viewDidLoad]; // listen to any requests to dismiss all stacked view controllers
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(dismissAllViewControllers:) name:@"YourDismissAllViewControllersIdentifier" object:nil];
// the remainder of viewDidLoad ...
}
// this method gets called whenever a notification is posted to dismiss all view controllers
- (void)dismissAllViewControllers:(NSNotification *)notification // dismiss all view controllers in the navigation stack
{
[self dismissViewControllerAnimated:YES completion:^{}];
}
And in any other view controller down the navigation stack that decides we should return to the top of the navigation stack:
[[NSNotificationCenter defaultCenter] postNotificationName:@"YourDismissAllViewControllersIdentifier" object:self];
NSNotificationCenter 在 init里面注册这个通知,
NSString* const str = @"FuckMe";
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(on:) name:str object:nil];
在dealloc里面移除这个通知的注册:
[[NSNotificationCenter defaultCenter] removeObserver:self name:str object:nil];
以上为不带参数的通知
一般在使用NSNotificationCenter的时候不使用参数,但是有些时候需要使用参数。
传递参数
[[NSNotificationCenter defaultCenter] postNotificationName:@"test" object:searchFriendArray];
接收参数并获取传递的参数
postNotificationName:通知的名字,也是通知的唯一标示,编译器就通过这个找到通知的。
object:传递的参数
- (void) test:(NSNotification*) notification
{
searchFriendArrary = [notification object];//通过这个获取到传递的对象
}