ios - Swift:在 ViewController 中显示名称

标签 ios swift

我有一个数组,其名称位于 TableViewController 中,并且我有 ViewController。如果我单击 TableViewController 中的单元格,我想在 ViewController 导航栏标题中显示数组中的名称。怎么做?

最佳答案

试试这个:

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

    tableView.deselectRow(at: indexPath, animated: true)
    navigationItem.title = myArray[indexPath.row]

}

更新:

如果您使用 Storyboard,您可以:

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

    tableView.deselectRow(at: indexPath, animated: true)
    let ame = myArray[indexPath.row]
    performSegue(withIdentifier: "ViewController", sender: selectedName)

}

然后,在 prepareForSegue 中恢复 selectedName 并在那里设置目的地VC的标题:

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    if let selectedName = sender as? String,
    let viewController = segue.destination as? ViewController {
      detailViewController.navigationItem.title = selectedName
    }
  }

关于ios - Swift:在 ViewController 中显示名称,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44995106/

相关文章:

ios - 应用程序在 iOS 7.1 上运行时无法全屏启动?

ios - 加载 xib 时无法识别的选择器

ios - 支持 Watch 的 iOS 应用程序的 Crashlytics/Fabric 分布?

ios - 应用代表 : Service call coming back from background

ios - 如果应用程序正在使用 GPS,即使应用程序处于后台,如何避免电池耗尽

ios - 从小部件(今日扩展)打开主机应用程序时未调用 AppDelegate 函数

ios - spinnerView 无法在 appdelegate 中工作

ios - AuthenticationMethodServerTrust 在 iOS 9 中为 nil

xcode - 是否需要从 swift 2 迁移到 swift 2.1?

ios - 如何在 swift 中从可加载包创建主类的实例