【问题标题】:Can't set UINavigationItem title programatically on initial load无法在初始加载时以编程方式设置 UINavigationItem 标题
【发布时间】:2015-09-25 00:38:35
【问题描述】:

我正在执行从 TableViewController(嵌入在 NavigationController 中)到另一个 TableViewController 的 segue(通过情节提要)。即选择一个单元格并呈现另一个 TableView,我想在其上显示所选单元格的文本作为下一个视图标题。

我正在实现这一点,但不是 100% 正确。在第一次初始选择单元格时,未设置 navigationItem 标题。只有当我向后导航并再次通过同一个单元格时,才会设置标题。

第一个 sn-p 是我的第一个 viewController,我正在选择一个单元格,在该单元格上我使用所选单元格标题设置 destinationViewControllers 变量。

    var valueToPass:String?
    override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
        print("You selected cell #\(indexPath.row)!")

        // Get Cell Label
        let indexPath = tableView.indexPathForSelectedRow!;
        let currentCell = tableView.cellForRowAtIndexPath(indexPath) as UITableViewCell!;

        valueToPass = currentCell.textLabel!.text
    }

    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {

    if (segue.identifier == "tripSegue") {

        // initialize new view controller and cast it as your view controller
        let viewController = segue.destinationViewController as! TripTableViewController
        // setting the view controllers property that will store the passed value
        viewController.passedValue = valueToPass
    }

}

第二个sn-p来自destinationViewController设置navigationItem的标题。

var passedValue: String?

override func viewDidLoad() {
    super.viewDidLoad()

    self.navigationItem.title = passedValue
}

【问题讨论】:

  • 在导航控制器中 valurEoPass 的设置位置
  • @DavidBerry 我已编辑帖子以反映您的评论。先看sn-p
  • 我仍然看不到你在哪里设置 valueToPass(可能在 didSelect...)?
  • 抱歉,我的编辑失败。请立即参考。 @大卫贝瑞
  • 尝试将 self.navigationItem.title = passedValue 移动到 viewController 生命周期的 viewDidLoad 之前的某个位置,等等,viewWillAppear

标签: ios swift uitableview segue navigationitem


【解决方案1】:

这是因为prepareForSegue 被称为之前 didSelectRowAtIndexPath。所以第一次选择一行时,valueToPass 为 nil。 prepareForSegue 被调用,而 valueToPass 仍然是 nil 并且你传递它,然后在你传递它之后,调用 didSelectRowAtIndexPathvalueToPass 设置为所需的值,这是你下次选择行时传递的值。

您需要在prepareForSegue 中完成所有操作。

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {

if (segue.identifier == "tripSegue") {
       // Get Cell Label
       let indexPath = self.tableView.indexPathForSelectedRow!;
       let currentCell = self.tableView.cellForRowAtIndexPath(indexPath) as UITableViewCell!;
       // initialize new view controller and cast it as your view controller
       let viewController = segue.destinationViewController as! TripTableViewController
       // setting the view controllers property that will store the passed value
       viewController.passedValue = currentCell.textLabel!.text

    }
}

【讨论】:

  • 或者,在didSelectRow... 中执行此操作并以编程方式调用segue 而不是自动调用。
  • 最佳实践是什么?
  • @DavidBerry 但我认为他仍然需要在prepareForeSegue 中执行完全相同的操作,无论segue 是通过编程方式执行还是由行选择自动触发。
  • @Vince 不客气,很高兴我能帮上忙!!唯一不同的是,我不是从单元格的文本标签中获取标题,而是从最初用于填充单元格标签的数据源数组中获取它。虽然我没有强有力的论据来解释为什么你不应该从单元格的标签中获取它。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-04-04
  • 2011-12-23
  • 2020-05-31
  • 1970-01-01
  • 2011-04-10
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多