xcode 如何在 iphone 应用程序中为流行视图控制器添加通知?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1676881/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to add a notification for pop view controller in iphone application?
提问by Sagar R. Kothari
I have seen the sample application of iPhone MP-movie player - controller.
我看过 iPhone MP 电影播放器的示例应用程序 - 控制器。
They have added a notification on the sample code.
他们在示例代码上添加了一个通知。
// Register to receive a notification that the movie is now in memory and ready to play
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(moviePreloadDidFinish:)
name:MPMoviePlayerContentPreloadDidFinishNotification
object:nil];
In above code, When MPMoviePlayerController finishes loading, it invokes moviePreloadDidFinish method.
在上面的代码中,当 MPMoviePlayerController 完成加载时,它会调用 moviePreloadDidFinish 方法。
Similarly, I want to fire an method when user press back button from navigation bar, (back to previous view controller through navigation controller ).
同样,我想在用户从导航栏中按下后退按钮时触发一个方法(通过导航控制器返回到上一个视图控制器)。
I don't know how to add a notification for that.
我不知道如何为此添加通知。
回答by luvieere
Put your own custom back button in the navigationItem
:
将您自己的自定义后退按钮放在navigationItem
:
UIBarButtonItem *btn = [[UIBarButtonItem alloc] initWithImage:yourImage style:UIBarButtonItemStyleBordered target:self action:@selector(goBack)];
self.navigationItem.leftBarButtonItem = btn;
[btn release];
In the goBack
method of your viewController, you'll put whatever code you need and then pop the viewController:
在goBack
你的 viewController的方法中,你会放任何你需要的代码,然后弹出 viewController:
- (void)goBack {
/* your code here */
[self.view.navigationController popToRootViewControllerAnimated:YES];
}
回答by Sagar R. Kothari
I have set hidden back button of navigation controller.
我已经设置了导航控制器的隐藏后退按钮。
- (void)viewDidLoad {
[super viewDidLoad];
UIBarButtonItem *x=[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemRewind target:self action:@selector(gotoPreviousView)];
UINavigationItem *y=self.navigationItem;
y.hidesBackButton=YES;
y.leftBarButtonItem=x;
[x release];
}
-(void)gotoPreviousView{
MyAccountViewCtr *x=(MyAccountViewCtr*)[self.navigationController.viewControllers objectAtIndex:0];
[self.navigationController popViewControllerAnimated:YES];
[x refreshItems];
}