2

我有一个Navigation Controller,当我按下后退按钮时,我不想弹出视图,但我想显示一个UIAlertView。我只想在做出选择后弹出视图UIAlertView!我能怎么做?我试图捕捉事件“按下后退按钮”但没有结果:(

4

4 回答 4

3

在 viewDidload中创建一个 UIBarButtonItem并将其添加到导航栏

UIBarButtonItem *backButton=[[UIBarButtonItem alloc] initWithTitle:@"Back" style:UIBarButtonItemStyleBordered target:self action:@selector(backButtonClicked)];
self.navigationItem.leftBarButtonItem=backButton;

创建一个方法,该方法将在单击 backButton 时调用

-(void)backButtonClicked{
       UIAlertView *alert=[[UIAlertView alloc] initWithTitle:@"Pop View!" message:@"Are you sure?" delegate:self cancelButtonTitle:@"No" otherButtonTitles:@"Yes", nil];
       [alert show];
}

编写此 UIAlertView 委托方法,当 UIAlertView 中的按钮被单击时将调用该方法

-(void)alertView:(UIAlertView *)alertView willDismissWithButtonIndex:(NSInteger)buttonIndex{
      if (buttonIndex==1) {
             [self.navigationController popViewControllerAnimated:YES];
      }
}

注意:不要忘记在头文件中添加 UIAlertView 委托<UIAlertViewDelegate>

于 2012-09-18T10:05:09.827 回答
1

您可以使用这样的方法检查后退按钮是否按下viewWillDisappear(从此处获取):

-(void)viewWillDisappear:(BOOL)animated {
    if ([self.navigationController.viewControllers indexOfObject:self]==NSNotFound && self.isBackButtonPressed) {
        // back button was pressed.  We know this is true because self is no longer
        // in the navigation stack.
    }
    [super viewWillDisappear:animated];

希望这可以帮助!

于 2012-09-18T09:59:59.300 回答
0

在 UIAlertview 委托方法中实现弹出 clickedButtonAtIndex:后退按钮按下只需实现只显示 UIAlertview

参考这个

于 2012-09-18T09:48:46.130 回答
0

遵循此代码

在按钮动作中。

UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Message" message:@"Do you want to open?" delegate:self cancelButtonTitle:@"NO" otherButtonTitles:@"YES", nil];
[alert show];

//Alert View delegate method
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if(buttonIndex==0)//NO
    {
        //your code
    }
    else if(buttonIndex==1)//YES
    {
        // your code
    }
}
于 2012-09-18T09:54:07.450 回答