单击tableview单元格行时如何推送新的viewcontroller?

Posted

技术标签:

【中文标题】单击tableview单元格行时如何推送新的viewcontroller?【英文标题】:How to push new viewcontroller when click on tableview cell row? 【发布时间】:2017-03-15 06:04:27 【问题描述】:

我是 SWIFT 编程方面的新人(更新 3 天)。 我在 swift 中创建了一个示例 tableview 单元演示。 我在 xib 中使用过“Testviewcontroller”。

代码是

import UIKit


class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate 

    @IBOutlet weak var myTableview:UITableView!

    let fruits = ["Apple","Banana","Grapes","Chiku","Watermalon","Bear"]
    let blogSegueIdentifier = "TestViewController"

    override func viewDidLoad() 
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.


    

    override func didReceiveMemoryWarning() 
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    
    public func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int
    
        return self.fruits.count
    
    public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
    
        let cell: UITableViewCell = UITableViewCell(style: UITableViewCellStyle.subtitle, reuseIdentifier: "MyTestCell")
        cell.textLabel?.text = self.fruits[indexPath.row]
        return cell
    

    public func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath)
    

        let testVC = TestViewController(nibName : "TestViewController" , bundle:nil)
        navigationController?.pushViewController(testVC, animated: true)

        print("You have selected \(indexPath.row) row")
    


Appdelegate

import UIKit

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate 

    var window: UIWindow?


    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool 
        // Override point for customization after application launch.
        return true
    

    func applicationWillResignActive(_ application: UIApplication) 
        // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
        // Use this method to pause ongoing tasks, disable timers, and invalidate graphics rendering callbacks. Games should use this method to pause the game.
    

    func applicationDidEnterBackground(_ application: UIApplication) 
        // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
        // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
    

    func applicationWillEnterForeground(_ application: UIApplication) 
        // Called as part of the transition from the background to the active state; here you can undo many of the changes made on entering the background.
    

    func applicationDidBecomeActive(_ application: UIApplication) 
        // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
    

    func applicationWillTerminate(_ application: UIApplication) 
        // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
    



我的问题是

我使用了“didSelectRowAt indexPath”委托方法。 我无法推送新的视图控制器 请帮助我,我无法解决这个问题。

【问题讨论】:

你面临什么错误,你是使用storyboard还是xib布局 @Harshal Valanda:我用过XIB文件,对我有用吗? 在导航控制器中嵌入视图控制器。从Editior -> EmbedIn -> UInavigationcontroller 点击行时会发生什么?你在使用故事板吗?如果是,请确保您已嵌入导航控制器。 您是否已经有像UIWindow->UINavigationController->ViewController 这样的层次结构设置,然后您从ViewController 调用navigationController?.pushViewController?也许您没有设置 UINavigationController 并且由于您使用可选的? 调用了navigationController?.pushViewController,所以对 nil 的调用会被忽略? 【参考方案1】:

由于您为每个UIViewController 使用XIB,您需要在AppDelegate.swift 文件中添加以下代码,然后您可以在ViewController.swift 中访问navigationController

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool 

        let viewController = ViewController(nibName:"ViewController", bundle:nil)

        let navController = UINavigationController(rootViewController: viewController)

        self.window!.rootViewController = navController

        self.window!.makeKeyAndVisible()
        return true


【讨论】:

不工作,给我黑屏。 我的要求是当我点击表格视图单元格的行然后打开新的视图控制器。对不起,我不知道该怎么做。我是全新的。如果它在目标 c 中非常简单。 由于未捕获的异常'NSInternalInconsistencyException'而终止应用程序,原因:'无法在捆绑包中加载NIB:'NSBundle /******/Library/Developer/CoreSimulator/Devices/E88F5A80 -1793-4593-A27F-77FB561904EA/data/Containers/Bundle/Application/ED218E72-DC6A-4E9A-85FB-D7D5A6413877/TableView.app>(已加载)',名称为'ViewController'' /////崩溃 你没有 ViewController.swift 文件的 XIB 文件吗? 显然我有 xib 文件【参考方案2】:

我认为你没有导航控制器。如果这个 viewController 是你的 rootViewController,你可以在你的 appDelegate 中做这样的事情。

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool 

let navController = UINavigationController()
let yourViewContoller = YourViewController()

navController.viewControllers = [yourViewContoller]

self.window?.rootViewController = navController
return true


确保您已经像这样在 viewController 中编写了 init

    init() 
    super.init(nibName: "YourViewController", bundle: Bundle.main)

【讨论】:

【参考方案3】:

我认为导航控制器在您的情况下为零。单击您的 xib 文件。单击主视图。进入Editor菜单,点击Embed in,选择Navigation controller

【讨论】:

以上是关于单击tableview单元格行时如何推送新的viewcontroller?的主要内容,如果未能解决你的问题,请参考以下文章

如何在 C# 中删除 TableView 中的行时从右到左单元格设置动画

ios 5子视图在tableview单元格中的两个视图之间翻转

无法识别单击单元格后的 tableView segue

如何从 tableview 框架中调用 tableview 单元格单击事件?

如何通过单击单元格中的按钮来删除 tableView 中的单元格?使用核心数据

单击tableview单元iphone时如何更改图像