导航控制器的问题(后退按钮和 prepareForSegue)

Problems with Navigation Controller (back button and prepareForSegue)

我保证我是 Xcode 和 Swift 的新手,所以我知道我犯了愚蠢的错误,但我不知道在哪里。这是我的 iOS 应用故事板的一部分:

第一个 table 视图和第二个导航控制器之间的 segue 称为 myTaskDetailSegue,其类型为 Show (e.g. Push)。现在我遇到了一些问题:

  1. 第一个 table 视图控制器和第二个视图控制器都没有显示后退按钮,我不知道为什么。很多人告诉我,导航栏和后退按钮在导航控制器中是默认的,但他们没有出现

  2. 在第一个table视图控制器的class中这里是方法prepareForSegue()

    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    
        if (segue.identifier == "myTaskDetailSegue" ) {
            let indexPath = self.tableView.indexPathForSelectedRow()
            let task = self.taskCollection[indexPath!.row] as Task
    
            let controller = (segue.destinationViewController as! UINavigationController).topViewController as! DetailsMyTasksViewController
    
            controller.detailItem = task
    
            println("segue mostra task \(task.id)")
            controller.navigationItem.leftItemsSupplementBackButton = true
        }
    }
    

所以你可以读到 segue 标识符是正确的,但是当点击一行时没有任何反应并且第二个 table 视图控制器没有显示。

我真的不知道我缺少什么,因为我没有经验。

这是完整的故事板:

您不需要两个 UINavigationController 就可以实现您想要实现的目标。重要的是要注意,每次您 push(使用 segue 或手动)时,都会将新的 UIViewController 添加到导航堆栈中。

根据Apple

Pushing a view controller displays its view in the navigation interface and updates the navigation controls accordingly. You typically push a view controller in response to user actions in the current view controller—for example, in response to the user tapping a row in a table.

因此您可以删除 Storyboard 中的第二个 UINavigationController 并直接将 segue 连接到您的 DetailsMyTaskViewController 并按以下方式更新您的 prepareForSegue

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {

   if (segue.identifier == "myTaskDetailSegue" ) {

       let indexPath = self.tableView.indexPathForSelectedRow()
       let task = self.taskCollection[indexPath!.row] as Task

       let controller = segue.destinationViewController as! DetailsMyTasksViewController

       controller.detailItem = task
       println("segue mostra task \(task.id)")
   }
}

并且您的后退按钮应该如您之前所说的那样默认显示。尽管如此,我还是强烈建议您阅读以下两个指南:

为了更好的理解导航栈等

希望对你有所帮助。