【问题标题】:Trying to Pass a selected tableview cell value to a NavigationBar Title in swift试图将选定的表格视图单元格值快速传递给导航栏标题
【发布时间】:2015-02-24 08:36:59
【问题描述】:

请原谅新手的问题,我一直在尝试一切,但无法让它发挥作用:(

我在TableView中有一个列表,理想情况下,选择单元格(行)时,它将所选单元格文本显示为导航栏的标题。

这是我认为应该将所选值传递给下一个 segue 的调用

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
    
    let selectedTopic = Topics[indexPath.row]
    
    let Sentences = SentencesVC()
    Sentences.TopicPassed = selectedTopic.heading
    
    self.performSegueWithIdentifier("gotoSentences", sender: tableView)
    let pathSelect = TopicTableView.indexPathForSelectedRow()
    pathSelect
}

还有另一个 ViewController 的代码:

var TopicPassed:String!

@IBOutlet weak var NavBar: UINavigationBar!

override func viewDidLoad() {
    super.viewDidLoad()

    // Do any additional setup after loading the view.
    self.navigationItem.title = TopicPassed
    
}

任何帮助都将不胜感激,因为它让我发疯,我不确定我尝试了多少次不同的解决方案都无济于事。

提前致谢

【问题讨论】:

    标签: ios uitableview swift uinavigationbar


    【解决方案1】:

    当您使用故事板和转场时,请将此函数添加到视图控制器的代码中:

    override func prepareForSegue(segue: UIStoryboardSegue!, sender: AnyObject!) {
        if segue.identifier == "gotoSentences" {
            let selectedTopic = Topics[tableView.indexPathForSelectedRow().row]
            let sentences = segue.destinationViewController as SentencesVC
            sentences.TopicPassed = selectedTopic.heading
        }
    }
    

    附带说明,在您的命名方案中,请注意不要在代码中使用大写的 var。 Swift 使用“camelCase”约定,大写的单词通常保留用于类型名称(类等)。为清楚起见,您可以将Topics 重命名为topics,将TopicPassed 重命名为topicPassed,等等。

    【讨论】:

    • 感谢您的回答,它部分有效,但我认为这更多是由于我的编码标准差而不是您的解决方案。我确实得到了我所追求的最终结果,非常感谢。
    【解决方案2】:

    像这样改变你的转场:

    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject!) {
        if (segue.identifier == "gotoSentences") {
            var otherView = segue.destinationViewController as OtherViewController;
    
            var index = mainTableView.indexPathForSelectedRow()
    
            otherView.title = Topics[index.row]
        }
    
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-04-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多