如何在iOS中按"返回"按钮时创建确认弹出窗口?

rec*_*ens 17 objective-c popup viewcontroller ios

当有人按下我的iOS应用程序的"后退"按钮时,我想添加一个弹出窗口,询问用户他是否真的想回来.然后,根据用户的响应,我想撤消操作或继续.我试图在我的视图的viewWillDisappear函数中添加代码然后编写正确的委托但它不起作用,因为它总是更改视图然后显示弹出窗口.我的代码是:

    -(void) viewWillDisappear:(BOOL)animated {
       _animated = animated;
       if ([self.navigationController.viewControllers indexOfObject:self]==NSNotFound) {
           UIAlertView *alert_undo = [[UIAlertView alloc] initWithTitle:@"UIAlertView"
                                                                message:@"You could be    loosing information with this action. Do you want to proceed?"
                                                               delegate:self
                                                      cancelButtonTitle:@"Go back"
                                                      otherButtonTitles:@"Yes", nil];
           [alert_undo show];
       }
       else [super viewWillDisappear:animated];
   }
Run Code Online (Sandbox Code Playgroud)

代表实现是:

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    NSString *title = [alertView buttonTitleAtIndex:buttonIndex];
    if([title isEqualToString:@"Yes"])
    {
        [super viewWillDisappear:_animated];
    }
}
Run Code Online (Sandbox Code Playgroud)

这根本不起作用.现在有人有更好的方法去做或可能出错吗?

非常感谢你,

sta*_*Man 18

一旦-viewWillDisappear:被召唤,就没有阻止你viewController消失.

理想情况下,您应该覆盖navigationBar后退按钮,并在其方法中显示警报(其余部分几乎相同)

- (void)viewDidLoad
{
    //...
    UIBarButtonItem *bbtnBack = [[UIBarButtonItem alloc] initWithTitle:@"Back"
                                                                 style:UIBarButtonItemStyleBordered
                                                                target:self
                                                                action:@selector(goBack:)];

    [self.navigationItem setBackBarButtonItem: bbtnBack];
}

- (void)goBack:(UIBarButtonItem *)sender
{
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Alert"
                                                    message:@"...Do you want to proceed?"
                                                   delegate:self
                                          cancelButtonTitle:@"No"
                                          otherButtonTitles:@"Yes", nil];
    [alert show];
}

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    switch(buttonIndex) {
        case 0: //"No" pressed
            //do something?
            break;
        case 1: //"Yes" pressed
            //here you pop the viewController
            [self.navigationController popViewControllerAnimated:YES];
            break;
    }
}
Run Code Online (Sandbox Code Playgroud)

注意:不要忘记<UIAlertViewDelegate>.h文件中声明viewController


rec*_*ens 15

谢谢你的回答,@ staticVoidMan!我最后使用了你的代码进行了一些修改.后退按钮无法修改,因此应创建一个额外的按钮并隐藏标准按钮.唯一的问题是新的"后退"按钮的样式,它不等于标准的样式.最终的代码是:

- (void)viewDidLoad
{

    self.navigationItem.hidesBackButton = YES;
    UIBarButtonItem *bbtnBack = [[UIBarButtonItem alloc] initWithTitle:@"Back"
                                                                 style:UIBarButtonItemStyleBordered
                                                                target:self
                                                                action:@selector(goBack:)];

    self.navigationItem.leftBarButtonItem = bbtnBack;

}

- (void)goBack:(UIBarButtonItem *)sender
{
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Alert"
                                                    message:@"...Do you want to proceed?"
                                                   delegate:self
                                          cancelButtonTitle:@"No"
                                          otherButtonTitles:@"Yes", nil];
    [alert show];
}

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    switch(buttonIndex) {
        case 0: //"No" pressed
            //do something?
            break;
        case 1: //"Yes" pressed
            //here you pop the viewController
            [self.navigationController popViewControllerAnimated:YES];
            break;
    }
}
Run Code Online (Sandbox Code Playgroud)