Swift - 如何在 MVC 中正确构建按钮?
Swift - How to properly architect buttons in MVC?
我写了一段代码,在 ViewController 文件中用一个 segue 动作在 UIView 中制作了一个 UIButtons。 segue 操作允许用户按下 UIButton 并推送一个带有该按钮标题的新 UIView。这很好用。
然后我了解了 MVC 并决定将代码拆分到适当的位置。所以我将创建 UIView 和 UIButtons 的代码移到了一个名为 ButtonView 的新文件中。
现在我很困惑在哪里可以访问 segue 函数,因为 segue 代码是在 ViewController 文件中使用 @IBAction 定义的。
// 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 函数的访问权限?
感谢您的帮助!
干杯,
button.addTarget(self, action: "buttonAction:", forControlEvents: UIControlEvents.TouchUpInside)
button.addTarget(self, action: "saveButton:", forControlEvents: UIControlEvents.TouchDown)
正常情况是按钮应该向其 视图控制器 发送消息。这是标准的 MVC 要做的事情——实际上,就是 MVC 中的 VC(View 让 Controller 知道有用户交互,而 Controller 是负责后续逻辑)。
因此,在您的代码中,我认为 self
不是视图控制器而是视图。在我看来,你真正想知道的是:如何将这里的 self
变成 "my view controller, where the segue code is"?
答案是:遍历 UIResponder 链 (nextResponder()
),直到到达它为止。假设 self
是界面中的一个 UIView。那么:
var resp : UIResponder! = self
while !(resp is UIViewController) { resp = resp.nextResponder() }
let vc = resp as! UIViewController
现在 vc
是拥有视图控制器,您现在可以将他设置为这些按钮操作的目标。
我写了一段代码,在 ViewController 文件中用一个 segue 动作在 UIView 中制作了一个 UIButtons。 segue 操作允许用户按下 UIButton 并推送一个带有该按钮标题的新 UIView。这很好用。
然后我了解了 MVC 并决定将代码拆分到适当的位置。所以我将创建 UIView 和 UIButtons 的代码移到了一个名为 ButtonView 的新文件中。
现在我很困惑在哪里可以访问 segue 函数,因为 segue 代码是在 ViewController 文件中使用 @IBAction 定义的。
// 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 函数的访问权限?
感谢您的帮助! 干杯,
button.addTarget(self, action: "buttonAction:", forControlEvents: UIControlEvents.TouchUpInside)
button.addTarget(self, action: "saveButton:", forControlEvents: UIControlEvents.TouchDown)
正常情况是按钮应该向其 视图控制器 发送消息。这是标准的 MVC 要做的事情——实际上,就是 MVC 中的 VC(View 让 Controller 知道有用户交互,而 Controller 是负责后续逻辑)。
因此,在您的代码中,我认为 self
不是视图控制器而是视图。在我看来,你真正想知道的是:如何将这里的 self
变成 "my view controller, where the segue code is"?
答案是:遍历 UIResponder 链 (nextResponder()
),直到到达它为止。假设 self
是界面中的一个 UIView。那么:
var resp : UIResponder! = self
while !(resp is UIViewController) { resp = resp.nextResponder() }
let vc = resp as! UIViewController
现在 vc
是拥有视图控制器,您现在可以将他设置为这些按钮操作的目标。