【发布时间】:2015-06-17 23:21:22
【问题描述】:
我编写了一段代码,在 UIView 中创建了一个 UIButtons,并在 ViewController 文件中执行了 segue 操作。 segue 操作允许用户按下 UIButton 并推送一个带有该按钮标题的新 UIView。这很好用。
然后我了解了 MVC 并决定将代码拆分到适当的位置。所以我将创建 UIView 和 UIButtons 的代码移到了一个名为 ButtonView 的新文件中。
现在我很困惑在哪里可以访问 segue 函数,因为 segue 代码是使用 @IBAction 在 ViewController 文件中定义的。
// Segue action - pushing to the new view
@IBAction func buttonAction(sender: UIButton!) {
performSegueWithIdentifier("saveButton", sender: sender)
}
// Button action - saves the title of the button pressed into var buttonPressed
@IBAction func saveButton(sender: UIButton!) {
buttonPressed = sender.currentTitle
}
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "saveButton"
{
// pass the button title string into var buttonPressed of ViewController
if let destinationVC = segue.destinationViewController as? ViewController{
destinationVC.buttonPressed = buttonPressed
}
}
}
当我创建按钮时,我不知道如何将按钮连接到 segue 函数。在我定义按钮之前如下:
var button = UIButton.buttonWithType(UIButtonType.System) as! UIButton
button.addTarget(self, action: "buttonAction:", forControlEvents: UIControlEvents.TouchUpInside)
button.addTarget(self, action: "saveButton:", forControlEvents: UIControlEvents.TouchDown)
addSubview(button)
是将 segue 代码移动到视图中的做法...(我认为这不会起作用,而且看起来很不对劲)还是有办法通过调用来访问 segue 函数?
感谢您的帮助! 干杯,
【问题讨论】:
标签: ios swift uiview uibutton segue