【问题标题】:Cancel Navigation Bar action in iOS在 iOS 中取消导航栏操作
【发布时间】:2013-09-24 16:15:07
【问题描述】:

我正在开发一个涉及用户填写文本字段和浏览视图的 iOS 应用程序。目前,导航有时使用导航栏处理,有时通过按钮处理。我尝试将一些字段设为必填:如果这些字段留空,则阻止通过应用程序的进度。这适用于基于按钮的导航,但不适用于导航栏。我的代码如下:

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    if ([[segue identifier] isEqualToString:@"birthPopBus"]) {
        currentPopoverSegue = (UIStoryboardPopoverSegue *)segue;
        pvc = [segue destinationViewController];
        [pvc setDelegate:self];
    }
    else if([[segue identifier] isEqualToString:@"SecondBusPageSegue"]) {
        //This code breaks the next page

        //Check fields, and create error message (code removed for readability)...

        //If the fields are not filled in, display the alert with generated string.
        if(!(first && last && email && phone && address && zip && ssn && birthFilled)){
            UIAlertView *message = [[UIAlertView alloc] initWithTitle:@"Required Fields Missing:"
                message:alertMessageMutable
                delegate:nil
                cancelButtonTitle:@"OK"
                otherButtonTitles:nil];
            [message show];

        }

        //Perform the segue, should only run if all required fields are filled.
        else{     
            [self fillBus1Dictionary];
            NSLog(@"application dictionary: %@", self.application);

            SecBusPage * secondBusPage = segue.destinationViewController;
            secondBusPage.application = self.application;
        }
    }
}

错误消息将显示,但仅在视图更改之后。因此,用户进入 SecondBusPage,并收到一条提示“您没有填写 X、Y、Z 字段”的警报。这对他们来说非常令人困惑。防止导航栏切换视图的任何想法?

提前致谢!

【问题讨论】:

    标签: ios uinavigationbar


    【解决方案1】:

    您可以通过在下面链接的帖子中实现我的答案中的条件转场来轻松实现这一点

    Conditional Segue Using Storyboard

    您无法从 prepareForSegue 取消转场。而不是根据条件执行segues

    【讨论】:

      【解决方案2】:

      您正在从情节提要中进行转场,并且无法取消情节提要转场。您应该做的是删除该 segue 并使用:

          if(!(first && last && email && phone && address && zip && ssn && birthFilled)){
              UIAlertView *message = [[UIAlertView alloc] initWithTitle:@"Required Fields Missing:"
                  message:alertMessageMutable
                  delegate:nil
                  cancelButtonTitle:@"OK"
                  otherButtonTitles:nil];
              [message show];
      
          }else
         { 
               [self.navigationController pushViewController:yourViewController animation:YES];
         }
      

      【讨论】:

        【解决方案3】:

        故事板限制了自定义行为的灵活性。

        我在应用中控制导航的方法是:

        • 在项目的AppDelegate 中创建一个名为mainDelegateid 实例对象(属性),每次出现viewController 时,我在viewDidAppear方法:

          appDelegate.mainDelegate = self
          
        • 我为navigationController 使用了自定义navigationBar

        • 在这个自定义导航栏的类中,当用户点击一个按钮时,我会检查:

          if ([appDelegate.mainDelegate isKindOfClass:[myViewController class]]){
          
              if ([appDelegate.mainDelegate allFieldsAreFilled]){
                   //perform action of pushing view
              }
          }
          
        • 这里allFieldsAreFilledmyViewController的委托方法。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多