如何在 iPhone 应用程序中为弹出视图控制器添加通知?

发布于 2024-08-10 11:38:10 字数 552 浏览 7 评论 0原文

我看过iPhone MP电影播放器​​-控制器的示例应用程序。

他们在示例代码中添加了通知。

// 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];

在上面的代码中,当 MPMoviePlayerController 完成加载时,它会调用 moviePreloadDidFinish 方法。

同样,当用户按下导航栏中的后退按钮(通过导航控制器返回到上一个视图控制器)时,我想触发一个方法。

我不知道如何为此添加通知。

I have seen the sample application of iPhone MP-movie player - controller.

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.

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

一片旧的回忆 2024-08-17 11:38:10

将您自己的自定义后退按钮放入 navigationItem 中:

UIBarButtonItem *btn = [[UIBarButtonItem alloc] initWithImage:yourImage style:UIBarButtonItemStyleBordered target:self action:@selector(goBack)];
self.navigationItem.leftBarButtonItem = btn;
[btn release];

在 viewController 的 goBack 方法中,您将放置所需的任何代码,然后弹出 viewController:

- (void)goBack {
 /* your code here */

[self.view.navigationController popToRootViewControllerAnimated:YES];
}

Put your own custom back button in the 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:

- (void)goBack {
 /* your code here */

[self.view.navigationController popToRootViewControllerAnimated:YES];
}
水波映月 2024-08-17 11:38:10

我设置了导航控制器的隐藏后退按钮。

- (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];
}

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];
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文