我用的是Xcode 8.2、Swift 3、iOS 10。我知道这个标题可能很难理解,所以让我解释一下我想做什么。
我有一个有三个孩子的孩子,每个孩子都有自己的孩子和其他东西。下面显示了一个这样的选项卡(表1)。这是一个TabViewController
单元格,我在上面显示我的自定义ViewControllers
单元格和信息。
在另一个选项卡(例如,选项卡2)上,单击按钮将单元格添加到选项卡1我已经让它工作得很好了。现在,我想要的行为是,当用户单击同一个按钮(在选项卡2上)时,我想要添加一个单元格(如前所述),同时添加该单元格的详细信息视图并跳转到该详细信息视图(而不必切换到选项卡1并单击相应的单元格)。在这个细节视图中,我希望有一个Back按钮,它会将我返回到Tab 1,在这里可以访问所有的细节视图。
我一直在试图找到正确的方法来实现这种行为,所以任何帮助都会非常感谢。谢谢您!
编辑
我根据温特下面的回复做了一些编辑。但是,我仍然无法将新创建的NavigationController
与表视图单元格“连接”。
我做了以下工作:
在表2中:
// this code is located in a function where I call to create a new custom cell in Tab 1.
//Switch to first tab
tabBarController?.selectedIndex = 0
//Show the detail view
if let navigationController = tabBarController?.viewControllers?[0] as? UINavigationController {
let detailViewController = UIViewController()
detailViewController.view.backgroundColor = UIColor.red
navigationController.pushViewController(detailViewController, animated: true)
let tempNavVC = self.tabBarController?.viewControllers?[0] as! UINavigationController
let resultsTab = tempNavVC.viewControllers[0] as! Tab1VC
resultsTab.detailVCArray.append(detailViewController)
}
在表1中:
// this is an array that holds the newly created view controllers passed in from Tab 2
// when the user clicks on a cell, look up which cell it clicked on and display the view controller for that cell
var detailVCArray = [UIViewController]()
// called when a cell is clicked
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let newChildVC = detailVCArray[indexPath.row]
self.addChildViewController(newChildVC)
self.present(newChildVC, animated: true, completion: nil)
}
当我运行它时,表2中的代码成功执行,我看到一个红色背景的新视图控制器。但是,如果单击“表视图”单元格,则会出错:
'NSInvalidArgumentException',reason:'应用程序试图呈现
模块化主动控制器
而不是继续使用同一个红色视图控制器。
我意识到这可能是一个非常愚蠢的方法,但我想不出其他的。
在一天结束时,我希望我的Tab 2在Tab 1中创建一个表视图单元格,其中包含一些带有详细信息的视图控制器,并将我“转换”到这些详细信息。
最佳答案
代码如下:
//Click function in viewController of Tab 2
func click(_ sender: Any) {
//Adding cells into Tab 1
//Switch to Tab 1
tabBarController?.selectedIndex = 1
//Show the detail view
if let navigationController = tabBarController?.viewControllers?[1] as? UINavigationController {
let detailViewController = UIViewController()
detailViewController.view.backgroundColor = UIColor.white
navigationController.pushViewController(detailViewController, animated: true)
}
}
//Code in viewController of Tab 1
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
//For example: need to show detail view when row is 1
if indexPath.row == 1 {
//Show the detail view
let detailViewController = UIViewController()
detailViewController.view.backgroundColor = UIColor.white
navigationController?.pushViewController(detailViewController, animated: true)
}
}
关于ios - 在单击按钮后跳转到该详细信息时,如何为UITableViewCell创建详细信息?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/42151597/