Ios 如何在Swift中将按钮标题传递给下一个视图控制器
我在viewDidLoad的循环中定义了几个按钮。它们位于滚动视图中的内容视图中Ios 如何在Swift中将按钮标题传递给下一个视图控制器,ios,swift,uibutton,segue,viewcontroller,Ios,Swift,Uibutton,Segue,Viewcontroller,我在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.s
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)
}
注释行是失败的尝试
我还尝试了在按下按钮时调用的操作功能:
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)
}
使用发送器获取按钮标题是可行的,但我不知道如何将其传递给下一个视图控制器
非常感谢能够提供帮助的任何人。在按钮功能中,将按钮作为序列的发送者传递:
func collegeSelected(sender: UIButton!) {
self.performSegueWithIdentifier("goToTableView", sender: sender)
}
然后在prepareForSegue
中,从发件人处获取按钮标题(即按钮):
func collegeSelected(sender: UIButton!) {
self.performSegueWithIdentifier("goToTableView", sender: sender)
}
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
let destViewController = segue.destinationViewController as videosTableView
if let buttonTitle = (sender as? UIButton)?.titleLabel?.text {
destViewController.title = buttonTitle
}
}