I am developing an iOS app screen, where i’m having around 25 buttons, clicking on each button will launch a separate view controller. Basically, i have implemented like having one ‘IBAction’ and provide ‘tag id’ for each button. Under this button action, based on button tag id, i am launching each view controller. Problem here is, i’m programming via switch case for identifying this button id’s, but i want to avoid using this switch case and looking to provide more efficient programming here. How it is possible to remove ‘switch case’ but provide better way of programming for this situation.
I am developing an iOS app screen, where i’m having around 25 buttons, clicking
Share
Here a
switchstatement seems to be the cleanest and most efficient way to do things, you could have a bigif-elseifbut if you’re giving the cold shoulder toswitchthen that’s even less of an option for you I guess.If you really want to avoid a switch then what you could do is tag your buttons from 0 to 24, and have an
NSArraycontaining an array of the names of your view controller’s class, then useNSClassFromStringto get the class reference from which to instantiate an object of that class.Then you could do something like: