【问题标题】:I cant set the UINavigationBarTitleItem. How do I set it?我无法设置 UINavigationBar 标题项。我该如何设置?
【发布时间】:2017-09-28 09:56:47
【问题描述】:

我的导航标题和使用代码设置有问题。

这是我的代码结构:

Navigation Controller -> TableView -> UIScreen where i want to set the title.

这是我的代码:

TableViewController:

     import UIKit

  class ProductsTableViewController: UITableViewController {

override func viewDidLoad() {
    super.viewDidLoad()

    tableView.register(UserCell.self, forCellReuseIdentifier: cellId)
}

let cellId = "cellId"
let products = [

    "Happy Face",
    "Sad Face",
    "High Five",
    "Angry Face",
    "The Earth"
]

override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

    return products.count
}

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

    let cell = tableView.dequeueReusableCell(withIdentifier: cellId, for: indexPath) as! UserCell

    let product = products[indexPath.row]
    cell.textLabel?.text = product

    return cell
}

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

    let buyProductViewController = BuyProductViewController()
    let product = products[indexPath.row]
    buyProductViewController.nameOfItem = product

    buyProductViewController.navigationItem.title = product

    performSegue(withIdentifier: "segue", sender: self)
}
 }

  class UserCell: UITableViewCell {

 }

设置我的标题视图控制器:

import UIKit

类 BuyProductViewController: UIViewController {

var nameOfItem: String? {

    didSet {

        print(nameOfItem)
    }
}

override func viewDidLoad() {
    super.viewDidLoad()
}

}

如果any1可以帮助我,我将非常感激,因为我非常沮丧并且已经为此工作了一个多星期,只是遇到了堆栈溢出。

谢谢。

【问题讨论】:

  • 尝试将self.title = "title name" 设置为viewDidLoad

标签: ios swift uinavigationcontroller ios-simulator swift4


【解决方案1】:

如果满足以下条件,这将适用于您: 1. ProductsTableViewController嵌入在UINavigationController中; 2. 标识符为“segue”的 Segue 是 push。请注意,区别在于直接设置推送控制器的标题。 NavigationBar 自动使用显示的 VC 的标题作为导航栏的标题。

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

    let buyProductViewController = BuyProductViewController()
    let product = products[indexPath.row]
    buyProductViewController.nameOfItem = product

    buyProductViewController.title = product

    performSegue(withIdentifier: "segue", sender: self)
}

【讨论】:

  • 那么解决方法是什么?
  • 设置 ViewController 的 title 属性,iOS 将处理其余的。 buyProductViewController.title = product 并且不使用 .navigationItem 属性。如果这不起作用,则上面的第 1 点或第 2 点都不是先决条件。
【解决方案2】:

请检查您是否必须在BuyProductViewController中设置标题:

import UIKit
class BuyProductViewController: UIViewController {
    var nameOfItem: String? {
        didSet {
            print(nameOfItem)
        }
    }

    override func viewDidLoad() {
        super.viewDidLoad()
        self.title = nameOfItem
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-01-21
    • 1970-01-01
    • 1970-01-01
    • 2014-02-14
    • 1970-01-01
    • 2010-09-22
    • 1970-01-01
    相关资源
    最近更新 更多