推送視圖控制器在 Swift 中不起作用 (Push view controller not working in Swift)


問題描述

推送視圖控制器在 Swift 中不起作用 (Push view controller not working in Swift)

        let storyboard = UIStoryboard(name: "Main", bundle: nil)
        let playGameViewController = (storyboard.instantiateViewController(withIdentifier: "PlayGameViewController") as! PlayGameViewController)
        self.navigationController?.pushViewController(playGameViewController, animated: true)

push 沒用,但我嘗試過現在的工作,我想用 push 導航。


參考解法

方法 1:

Try running the below code to know where you have gone wrong.

let storyboard = UIStoryboard(name: "Main", bundle: nil)
guard let playGameViewController = storyboard.instantiateViewController(withIdentifier: "PlayGameViewController") as? PlayGameViewController else {
    print("This means you haven't set your view controller identifier properly.")
    return
}
guard let navigationController = navigationController else {
    print("This means you current view controller doesn't have a navigation controller")
    return
}
navigationController.pushViewController(playGameViewController, animated: true)

Try using breakpoints to figure out if any variable is nil. In your case, there is more probability for having your navigationController being nil.

(by Ashot KhachatryanFrankenstein)

參考文件

  1. Push view controller not working in Swift (CC BY‑SA 2.5/3.0/4.0)

#storyboard #iOS #swift #uinavigationcontroller






相關問題

如何在 XAML 中為資源設置動畫? (How to animate a resource in XAML?)

使用情節提要設置 GLKView 的初始大小 (Set an initial size of GLKView with storyboard)

在情節提要中為“平鋪”創建更大的反面視圖 (Creating a larger flip-side view for 'tile' in storyboard)

XNA/XAML 鼠標懸停時動畫邊框顏色 (XNA/XAML Animate border color on mouseover)

帶有自定義按鈕的故事板 (Storyboard with Custom Buttons)

添加子視圖控制器並更改其標籤 (Adding a child view controller and changing its label)

Thêm các nút vào Ô Chế độ xem Bảng tùy chỉnh trong Lớp Bảng Xem tùy chỉnh (Adding buttons to custom Table View Cell under custom TableView Class)

ios在表格視圖單元格中視覺調整圖像大小 (ios visually resize image in a table view cell)

如何解決“在類型'ControlTemplate'中找不到可附加屬性'觸發器'”? (How to resolve "The attachable property 'Triggers' was not found in type 'ControlTemplate'"?)

Xcode 7.3、Swift 2.2 故事板極慢 (Xcode 7.3, Swift 2.2 Storyboards Extremely Slow)

Xcode Storyboard 警告未提供上下文:不支持的約束屬性配置 (Xcode Storyboard Warning not providing context: Unsupported Configuration of constraint attributes)

推送視圖控制器在 Swift 中不起作用 (Push view controller not working in Swift)







留言討論