簡體   English   中英

導航欄“取消”

[英]Navigation Bar “Cancel”

我目前面臨的問題是,如果我在表格視圖中的某行上進行選擇並返回並按下導航欄中的“ +”按鈕,則我的應用將崩潰。 我懷疑這可能是因為我在xcode中選擇了“模態顯示”選項。 無論如何,我可能完全不了解,但這是我的代碼。 請耐心等待,因為我只接受了編碼

-(void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
  NSIndexPath *path = [self.tableView indexPathForSelectedRow];
  displayViewController *vc;

  vc = [segue destinationViewController];
    if (path != NULL) {
        vc.pID = path.row;
    }
}

這是我的prepareForSegue函數,當我嘗試將vc.pID設置為path.row時發生錯誤,因為如果單擊“ +”按鈕,則該行將不存在。 如果我按下“ +”按鈕,有沒有辦法不進入prepareForSegue函數?

謝謝您的幫助!

您要做的是將針對segue標識符的測試添加到prepareForSegue中。

segue屬性具有標識符值。

if segue.identifier == "nameOfSegueIdentifier" { // do stuff here }

這樣,您可以根據UI的哪個方面觸發segue來運行特定的代碼。

編輯

抱歉,現在才看到您正在使用目標C。我的代碼在Swift中。 不確定如何在目標C中實現它,但是概念將是相同的。

在objective-c中准備segue的形式是...

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    if ([segue.identifier isEqualToString:@"someIdentifierA"]) {
        // get the table view selection, for example
        NSIndexPath *path = [self.tableView indexPathForSelectedRow];
        // get some part of my datasource (model), for example
        MyModelObject *modelObject = self.datasourceArray[indexPath.row];
        // initialize part of the model on the destination vc
        MyOtherViewController *destination = (MyOtherViewController *)segue.destinationViewController;
        destination.modelObject = modelObject;
    } else if ([segue.identifier isEqualToString:@"someIdentifierB"]) {
        // same idea here, but different segue to different destination
        NSIndexPath *path = [self.tableView indexPathForSelectedRow];
        displayViewController *vc = (displayViewController *)[segue destinationViewController];
        vc.pID = path.row;
    } else if // and so on
    }
}

暫無
暫無

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

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