对不起新手的问题我一直在尝试一切,但都没能做到:(
我在表格视图中有一个列表,理想情况下,当选择一个单元格(行)时,它会将选定的单元格文本显示为导航栏的标题。
以下是我认为应该将所选值传递给下一个分段的调用
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
}
任何帮助都将不胜感激,因为这让我抓狂,我不知道我尝试了多少次不同的解决方案都无济于事。
提前感谢
当您使用情节串连板和分段时,请将此函数添加到视图控制器的代码中:
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
}
}
顺便说一句,在命名方案中,要小心,尽量不要在代码中使用大写的vars。Swift使用"camelCase"约定,大写单词通常保留给类型名称(类等)。为了清晰起见,您可以将Topics
重命名为topics
,将TopicPassed
重命名为topicPassed
等。
这样更改您的分段:
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]
}
}