I had wrote a block of code which made a UIButtons in a UIView with a segue action all in the ViewController file. The segue action allows the user to press on the a UIButton and pushes a new UIView with the title of that button on it. This worked well.
Then I learned about MVC and decided to split up the code into their appropriate places. So I moved the code which created the UIView and UIButtons into a new file called ButtonView.
Now I am confused where to get access to the segue functions, since the segue code is defined in the ViewController file using @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
}
}
}
When I created the buttons I don't know how to hook the buttons up to the segue functions. Before I defined the buttons as follows:
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)
Is the practice to move the segue code into the view...(which I didn't think will work and looks just wrong) or is there a way to gain access to the segue functions through a call?
Thanks for any help! Cheers,