1
\$\begingroup\$

I have an iOS Swift app with 8 buttons that call the same segue, but this is very repetitive. How can I refactor so it uses the same function prepare?

@IBAction func backButtonPressed(_ sender: Any) {
 self.vcWithType = VCtype.back.rawValue
 self.postion = "back"
 self.performSegue(withIdentifier: "showCategory", sender: sender)
}
@IBAction func mountButtonPressed(_ sender: Any) {
 self.vcWithType = VCtype.mount.rawValue
 self.postion = "mount"
 self.performSegue(withIdentifier: "showCategory", sender: sender)
}
@IBAction func kneeRideButtonPressed(_ sender: Any) {
 self.vcWithType = VCtype.knee.rawValue
 self.postion = "knee"
 self.performSegue(withIdentifier: "showCategory", sender: sender)
}
@IBAction func sideKbuttonPressed(_ sender: Any) {
 self.vcWithType = VCtype.sideK.rawValue
 self.postion = "side"
 self.performSegue(withIdentifier: "showCategory", sender: sender)
}
@IBAction func northSouthBpressed(_ sender: Any) {
 self.vcWithType = VCtype.northSouth.rawValue
 self.postion = "north"
 self.performSegue(withIdentifier: "showCategory", sender: sender)
}
@IBAction func guardBpressed(_ sender: Any) {
 self.vcWithType = VCtype.guarda.rawValue
 self.postion = "guard"
 self.performSegue(withIdentifier: "showCategory", sender: sender)
}
@IBAction func halfGuardBpressed(_ sender: Any) {
 self.vcWithType = VCtype.halfGuard.rawValue
 self.postion = "half"
 self.performSegue(withIdentifier: "showCategory", sender: sender)
}
@IBAction func turtleBpressed(_ sender: Any) {
 self.vcWithType = VCtype.turtle.rawValue
 self.postion = "turtle"
 self.performSegue(withIdentifier: "showCategory", sender: sender)
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
 if (segue.identifier == "showCategory") {
 if let vc: CategoryViewController = segue.destination as? CategoryViewController {
 vc.vcWithType = self.vcWithType
 vc.chaza = self.postion
 }
 }
}
asked Feb 7, 2017 at 1:41
\$\endgroup\$
1
  • \$\begingroup\$ Create enum of your position, and instead of calling independent method on every button, call only one method. \$\endgroup\$ Commented Feb 7, 2017 at 11:38

1 Answer 1

3
\$\begingroup\$

I think easiest way is using tags. You can connect more then one button to one IBAction func. Just Make sure sender is UIButton.

enter image description here

After that change buttons tag property.

enter image description here

After changing tags. We just need to use switch-case

@IBAction func ButtonAction(_ sender: UIButton) {
 switch sender.tag {
 case 0:
 print("Button 1 Clicked")
 case 1:
 print("Button 2 Clicked")
 default: break
 }
}
answered Feb 12, 2017 at 21:29
\$\endgroup\$
1
  • \$\begingroup\$ You can set the tag programmatically if you don't want to dig around in Interface Builder setting them. To reduce the chance of typos, you might want to use an enum to store your tags and reference the enum when setting the button tags. \$\endgroup\$ Commented Feb 23, 2017 at 19:07

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.