3

我看到了多个关于如何检测用户何时按下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];
}

谢谢你的帮助...

4

1 回答 1

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

并使用警报代表clickedButtonAtIndex

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex  
    {
        if(alertView.tag == 1)
        {
            // set your logic
        }
    }
于 2013-10-16T07:39:55.030 回答