【发布时间】:2015-02-14 19:00:36
【问题描述】:
我在 viewDidLoad 的循环中定义了几个按钮。它们位于滚动视图中的内容视图内。
for var i:Int = 0; i < colleges.count; i++ {
let collegeButton = UIButton.buttonWithType(UIButtonType.System) as UIButton
collegeButton.frame = CGRectMake(0, 0, self.contentView.frame.size.width, 30)
collegeButton.center = CGPoint(x: self.contentView.center.x, y: 30 * CGFloat(i) + 30)
collegeButton.setTitle(sortedColleges[i], forState: UIControlState.Normal)
collegeButton.setTitleColor(UIColor.darkGrayColor(), forState: UIControlState.Normal)
collegeButton.titleLabel?.font = UIFont(name: "Hiragino Kaku Gothic ProN", size: 15)
collegeButton.addTarget(self, action: "collegeSelected:", forControlEvents: UIControlEvents.TouchUpInside)
self.contentView.addSubview(collegeButton)
}
如您所见,每个按钮的标题都是根据先前定义的字符串数组设置的。 Whenever a button is selected, a segue is called to move to a table view, which is embedded in a navigation controller.
我需要将导航控制器标题设置为与特定按钮标题相同。
以下是我尝试过的一些事情:
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
var destViewController:videosTableView = segue.destinationViewController as videosTableView
//destViewController.title = collegeButton.titleLabel?.text
//var buttonTitle = sender.titleLabel?.text
//destViewController.title = buttonTitle
}
注释行是失败的尝试。
我还尝试在按下按钮时调用的动作函数中工作:
func collegeSelected(sender: UIButton!) {
self.performSegueWithIdentifier("goToTableView", sender: self)
//Set navigation title of next screen to title of button
var buttonTitle = sender.titleLabel?.text
println(buttonTitle)
}
使用 sender 获取按钮标题有效,但我不知道如何将其传递给下一个视图控制器。
非常感谢任何可以提供帮助的人。
【问题讨论】:
标签: ios swift uibutton segue viewcontroller