我正在尝试创建我计划发布到 App Store 的第一个 iOS 应用程序。它是一个选项卡式应用程序,每个 View Controller 中都嵌入了 TableView。这是我的一个选项卡式 View Controller 的代码:
import Foundation
import UIKit
class AU: UITableViewController {
var textArray = ["001", "002", "003", "004", "005", "006", "007", "008", "009", "010", "011", "012", "013", "014", "015", "016", "017", "018", "019", "020", "021", "022", "023", "024", "025", "026", "027", "028", "029", "030", "031", "032", "033", "034", "035", "036", "037", "038", "039", "040", "041", "042" ]
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("AUCell") as UITableViewCell!
cell.textLabel?.text = textArray[indexPath.row]
return cell
}
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return textArray.count
}
}
所以,基本上,当用户点击 textArray
中的一项时,我想切换到另一个 View Controller 。我目前正在使用 Swift 2 编写此内容。
最佳答案
UITabBarController
中嵌入的每个 UIViewController
都有一个对 UITabBarController
及其 tabBarController
属性的引用。要更改标签栏的选定 View Controller ,请设置标签栏的 selectedViewController
:
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
if (indexPath.row == 0) { // check the row that was selected
self.tabBarController?.selectedIndex = 0 // select a new tab on the tab bar, changing the selected view controller
}
}
关于ios - 如何使用 TableView 项目访问 Swift 2 中的 View Controller ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36119186/