ios - 使用 Swift 中的导航 Controller 在 View Controller 中缺少后退按钮

标签 ios swift uinavigationcontroller

通过我在这个论坛上的研究,我开始了解导航 Controller 的工作原理。让我们将导航 Controller 设置为 firstView( TableView )作为 Root View Controller 。 firstView 中有一个按钮可将我带到 secondView( View Controller )。当然,如果我通过 Push segue 将 firstView 连接到 secondView,secondView 中应该有一个后退按钮。但是我的 secondView 中没有显示任何内容。

这是我在 firstView 中的代码:

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
    let sb = UIStoryboard(name: "Main", bundle: nil)
    let messagesVC = sb.instantiateViewControllerWithIdentifier("MessagesViewController") as! MessagesViewController        

    //Just querying, not important to my question
    roomQuery.findObjectsInBackgroundWithBlock { (results: [AnyObject]?, error: NSError?) -> Void in
        if error == nil {
            //Push the message onto the navigation controller

            //This will have a back button but the functionality of secondView breaks (for some weird reason?????)
            self.navigationController?.pushViewController(messagesVC, animated: true)

            //This will have no back button, but has functionalities that I had intended it to have 
            self.navigationController?.presentViewController(messagesVC, animated: true, completion: nil)
    }
}

所以这不应该在 firstView 和 secondView 之间创建一个连接,在 secondView 中有一个后退按钮吗?我目前正在使用 presentViewController(),所以我只需要一个该死的后退按钮。

我还尝试在 secondView 中创建一个导航栏和一个栏按钮项,但它也没有显示。有人帮我做这件事真是太好了。

最佳答案

您在后台线程而不是主线程中执行 UI 工作。在本网站和其他地方对此进行的讨论多得数不过来。寻找模式:

dispatch_async(dispatch_get_main_queue()) {
    // update some UI
}

特别是

roomQuery.findObjectsInBackgroundWithBlock {
    [unowned self] 
    (results: [AnyObject]?, error: NSError?) -> Void in
    if error == nil {
        // Push the message onto the navigation controller
        dispatch_async(dispatch_get_main_queue()) {
          self.navigationController?.pushViewController(messagesVC, animated: true)
        }
    }
}

如果您想要真正的见解,请从 http://www.raywenderlich.com/79149/grand-central-dispatch-tutorial-swift-part-1 等教程开始

关于ios - 使用 Swift 中的导航 Controller 在 View Controller 中缺少后退按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31735572/

相关文章:

ios - 在两个 VC 之间传递数据

ios - "Restore Purchases"按钮在第二次按下时崩溃应用程序

ios - 自定义iOS导航 Controller 动画: is this wrong?

iOS - 没有滑动背景图像的pushViewController

ios - 使用 Xcode 7 将应用程序提交到应用程序商店的解决方法

ios - Presented View Controller 状态栏颜色不一样,如何修改为默认值?

objective-c - Podspec 模式包括未在 source_files 中列出的头文件

ios - 如何为iOS正确构建Swift框架

ios - 如何将 UINavigationControllerDelegate 连接到导航 Controller ?

ios - 移动应用程序数据管理