iphone - 如何在 iPhone 应用程序中为弹出 View Controller 添加通知?

标签 iphone xcode uiviewcontroller uinavigationcontroller nsnotifications

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

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

// 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 方法。

同样,当用户从导航栏中按后退按钮时,我想触发一个方法(通过导航 Controller 返回到上一个 View Controller )。

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

最佳答案

将您自己的自定义后退按钮放入 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];
}

关于iphone - 如何在 iPhone 应用程序中为弹出 View Controller 添加通知?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1676881/

相关文章:

ios - 创建基于自动布局的指标 View

php - 如何建立从 iPhone 到 Apache 服务器的套接字连接并通过 PHP 进行通信?

xcode - 我得到 "clang: error: no input files"

ios - 使用 Swift 在 Xcode 中避免从一个 View Controller 到另一个 View Controller 的延迟

iOS:AutoresizingMask 未调整大小以适应屏幕上的 map 或 View

uiviewcontroller - 如何使用 SetNeedsDisplay 刷新我的 UIVIewController

ios - 呈现多个 View Controller 的最佳方法? swift 4

iphone - 无法使用 iOS 将数据插入 sqlite3 数据库

ios - Xcode 10 中的本地化

iphone - underPageBackgroundColor 被批准了吗?