【问题标题】:Don't Pop Navigation Controller in Xcode if AlertView如果 AlertView,不要在 Xcode 中弹出导航控制器
【发布时间】:2012-09-10 13:31:54
【问题描述】:

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

【问题讨论】:

  • 你想弹出视图控制器吗?或在按钮操作上显示警报视图?
  • 使用自定义方法创建一个后退按钮以显示警报并添加到导航栏实现在 alertviewdelegate 中弹出

标签: iphone xcode ipad uinavigationcontroller uialertview


【解决方案1】:

您可以像这样在viewWillDisappear 方法中检查后退按钮是否按下(从here 获取):

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

希望这会有所帮助!

【讨论】:

  • 如果您提到来源并附有答案的链接,那就太好了。 stackoverflow.com/a/3445994/876283
  • 你说得对,我自己的代码里有这个,忘记了源代码。
【解决方案2】:

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

UIBarButtonItem *backButton=[[UIBarButtonItem alloc] initWithTitle:@"Back" style:UIBarButtonItemStyleBordered target:self action:@selector(backButtonClicked)];
self.navigationItem.leftBarButtonItem=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>

【讨论】:

  • 好的,我认为这是正确的方法,而且效果很好!是否可以让 UIBarButtonItem 看起来像后退按钮?
  • 你可以。一种方法是使用自定义图像。
【解决方案3】:

遵循此代码

在按钮动作中。

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

【讨论】:

    【解决方案4】:

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

    参考this

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-07-07
      • 2013-08-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-06-06
      相关资源
      最近更新 更多