检测是否按下了后退按钮并等待用户操作弹出到上一个视图

Erz*_*iel 3 iphone button back uinavigationbar ios

我看到有关如何检测用户何时按下 UINavigationBar上的"后退"按钮的多个问题,但答案并未解决我的问题.

实际上,我想在用户按下UINavigationBar"后退"按钮时显示UIAlertView,问他"你想保存更改吗?" .

当用户按下"后退"按钮(带有以下代码段)时,我可以显示UIAlertView,但同时弹出上一个视图.我不想要这种行为!我只是希望应用程序弹出上一个视图之前等待用户回答 ...

-(void) viewWillDisappear:(BOOL)animated
{
    if ([self isMovingFromParentViewController])
    {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Avertissement" message:@"Voulez-vous enregistrer les modifications effectuées ?" delegate:self cancelButtonTitle:@"Retour" otherButtonTitles:@"Oui", @"Non", nil];
        [alert show];
    }
    [super viewWillDisappear:animated];
}
Run Code Online (Sandbox Code Playgroud)

谢谢你的帮助......

Nit*_*hel 6

我建议您使用自己LeftbarButtonItem而不是默认的后退按钮事件,viewWillDisappear如下所示: -

- (void)viewDidLoad
{
  UIBarButtonItem *left=[[UIBarButtonItem alloc]initWithTitle:@"Back" style:UIBarButtonItemStyleBordered target:self action:@selector(actionBack:)];
    self.navigationItem.leftBarButtonItem = left;
 [super viewDidLoad];
}

- (IBAction)actionBack:(id)sender {
   UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Avertissement" message:@"Voulez-vous enregistrer les modifications effectuées ?" delegate:self cancelButtonTitle:@"Retour" otherButtonTitles:@"Oui", @"Non", nil];
    [alert show];
}
Run Code Online (Sandbox Code Playgroud)

并使用提醒代表 clickedButtonAtIndex

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex  
    {
        if(alertView.tag == 1)
        {
            // set your logic
        }
    }
Run Code Online (Sandbox Code Playgroud)