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 に答える