簡體   English   中英

在iOS中取消導航欄操作

[英]Cancel Navigation Bar action in iOS

我正在開發一個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字段”。 這對他們來說非常混亂。 有什么防止導航欄切換視圖的想法嗎?

提前致謝!

您可以通過執行條件鏈接(例如以下鏈接中我的答案中的鏈接)輕松實現此目的

使用情節提要的條件Segue

您無法從prepareForSegue取消序列。 而不是根據條件執行搜索

您正在從情節提要中執行segue,並且無法取消情節提要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];
   }

故事板限制了自定義行為的靈活性。

我為控制應用程序中的導航所做的事情是:

  • 在項目的AppDelegate中創建一個名稱為mainDelegateid實例對象(屬性),每次顯示viewController ,我都會在viewDidAppear方法中使用以下命令將其設置為當前視圖控制器:

     appDelegate.mainDelegate = self 
  • 我使用了一個自定義的navigationBar作為navigationController

  • 在此自定義navBar的類中,在用戶點擊按鈕時,我檢查:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM