[英]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,並且無法取消情節提要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
中創建一個名稱為mainDelegate
的id
實例對象(屬性),每次顯示viewController
,我都會在viewDidAppear
方法中使用以下命令將其設置為當前視圖控制器:
appDelegate.mainDelegate = self
我使用了一個自定義的navigationBar
作為navigationController
。
在此自定義navBar的類中,在用戶點擊按鈕時,我檢查:
if ([appDelegate.mainDelegate isKindOfClass:[myViewController class]]){ if ([appDelegate.mainDelegate allFieldsAreFilled]){ //perform action of pushing view } }
allFieldsAreFilled
是myViewController
的委托方法。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.