簡體   English   中英

表格視圖中的單元格單擊不能快速導航到新控制器

[英]Cell Click in tableview doesn't navigate to new controller in swift

我的應用程序中有tableview,並且我想在tableview的單元格單擊上推送一個新的視圖控制器,但是它不起作用。

    func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
    tableView.deselectRowAtIndexPath(indexPath, animated: true)
    let section = indexPath.section
    let controller : ProductInfoViewController = self.storyboard?.instantiateViewControllerWithIdentifier("ProductInfoViewController") as! ProductInfoViewController
    controller.product = TableData[section]
    self.navigationController?.pushViewController(controller, animated: true)
     print("clicked section : \(section)")
   // print(TableData[row])
}

有人知道我在這里犯什么錯誤嗎? 我在分鏡腳本中具有ID的適當視圖控制器。 控制台中也沒有錯誤。

如果您的navigationController為nil,則您沒有任何導航堆棧可用於將viewcontroller推入。看起來您正在使用情節提要,因此將此控制器嵌入到navigationcontroller。為此,請在情節提要中選擇此視圖控制器,單擊頂部菜單中的“編輯器”,轉到“嵌入” in”,然后選擇navigationcontroller ...希望有幫助:)

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
    tableView.deselectRowAtIndexPath(indexPath, animated: true)
    let section = indexPath.section
    let controller : ProductInfoViewController = self.storyboard?.instantiateViewControllerWithIdentifier("ProductInfoViewController") as! ProductInfoViewController
    controller.product = TableData[section]

    // execute in main thread
    dispatch_async(dispatch_get_main_queue()) { () -> Void in
        self.navigationController?.pushViewController(controller, animated: true)
    }


     print("clicked section : \(section)")
   // print(TableData[row])
}

為第二個視圖控制器創建一個swift文件(SecondViewController.swift),並在適當的函數中鍵入以下內容:

let secondViewController = self.storyboard.instantiateViewControllerWithIdentifier("SecondViewController") as SecondViewController

self.navigationController.pushViewController(secondViewController, animated: true)

如果self.navigationController nil,則意味着您的視圖控制器未添加到導航堆棧中。因此,您必須在didFinishLaunchingWithOptions:方法中編寫代碼

  func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject : AnyObject]?) -> Bool {
    // Override point for customization after application launch.
    self.window = UIWindow(frame: UIScreen.mainScreen().bounds)
    self.viewController = ViewController()
    self.navigationController = UINavigationController(rootViewController: self.viewController)
    self.window.rootViewController = self.navigationController!
    self.window.makeKeyAndVisible()
    self.navigationController.navigationBar.hidden = true
    return true
}

試試這個可能會幫助您:-

使用情節提要名稱並檢查您是否將設置情節提要ID,並在下面輸入我的代碼,

        let storyboard = UIStoryboard(name: "Main", bundle: nil)
        let controller = storyboard.instantiateViewControllerWithIdentifier("PlayViewController") as! PlayViewController
        controller.titleLabel = (myArrayOfDict[indexpath.section].valueForKey("Title") as? String)!

        self.navigationController?.pushViewController(controller, animated: true)

希望它有所幫助

暫無
暫無

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

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