【问题标题】:How to add a notification for pop view controller in iphone application?如何在 iphone 应用程序中为弹出视图控制器添加通知?
【发布时间】:2010-12-13 05:01:41
【问题描述】:

我看过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 方法。

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

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

【问题讨论】:

  • View-Will-Appear & View-Will-Disappear 对我不起作用。因为我在标签栏控制器中使用了多个 UINavigation 控制器。

标签: iphone xcode uiviewcontroller uinavigationcontroller nsnotifications


【解决方案1】:

将您自己的自定义返回按钮放在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];
}

【讨论】:

  • 见我添加了我自己的代码。
【解决方案2】:

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-12-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多