Multiple UISwitch items in one UIView [duplicate]
-
26-10-2019 - |
Question
Possible Duplicate:
Handling multiple UISwitch controls in a table view without using tag property
Well i want to ask how can i configure 7 UISwitches on one UIview? I place the switches with interface builder and then i want to manage them through code. What is the best way of managing this. Here is my .h so far
#import <UIKit/UIKit.h>
@interface searchEditViewController : UIViewController{
UISwitch *switchOne;
UISwitch *switchTwo;
UISwitch *switchFor;
UISwitch *switchFive;
UISwitch *switchSix;
UISwitch *switchSeven;
}
@property(nonatomic,retain)UISwitch *switchOne;
@property(nonatomic,retain)UISwitch *switchTwo;
@property(nonatomic,retain)UISwitch *switchThree;
@property(nonatomic,retain)UISwitch *switchFour;
@property(nonatomic,retain)UISwitch *switchFive;
@property(nonatomic,retain)UISwitch *switchSix;
@property(nonatomic,retain)UISwitch *switchSeven;
-(IBAction)toggleButtonPressed:(id)sender;
@end
I want to make one action method toggleButtonPressed that will take care the seven of them
Solution
Create one action for all UISwitch
objects (I guess this is toggleButtonPressed
). In this method now you can know which UISwitch was triggered:
-(IBAction)toggleButtonPressed:(id)sender{
UISwitch *switchObj = (UISwitch*)sender;
if (switchObj == self.switchOne){
// do stuff
}
if (switchObj == self.switchTwo){
// do stuff
}
// switch(switchObj.tag){
// case 1:
// // do stuff
// break;
// case 2:
// // do stuff
// break;
// }
}
Edit. You can set tag
property to some value (from 1 to 7) and change if-statement
to switch-case
.
OTHER TIPS
@beryllium, your way does work but you might want to try something a little more elegant.
Instead, tag each switch and then instead of if-then use switch-case as you appear to have commented out.
Here's an example, note that kUIActivityIndicatorViewStyleWhiteLarge is actually a constant I created and assigned a value #define kUIActivityIndicatorViewStyleWhiteLarge 1
- (IBAction)setSpinnerType:(UISwitch *)sender
{
// switch based on tag value
// turn other two switches off
switch (sender.tag) {
case kUIActivityIndicatorViewStyleWhiteLarge:
self.spinner.activityIndicatorViewStyle = UIActivityIndicatorViewStyleWhiteLarge;
[sender setOn:YES animated:YES];
[self.whiteSwitch setOn:NO animated:YES];
[self.graySwitch setOn:NO animated:YES];
break;
case kUIActivityIndicatorViewStyleWhite:
self.spinner.activityIndicatorViewStyle = UIActivityIndicatorViewStyleWhite;
[self.whiteLargeSwitch setOn:NO animated:YES];
[sender setOn:YES animated:YES];
[self.graySwitch setOn:NO animated:YES];
break;
case kUIActivityIndicatorViewStyleGray:
self.spinner.activityIndicatorViewStyle = UIActivityIndicatorViewStyleGray;
[self.whiteLargeSwitch setOn:NO animated:YES];
[self.whiteSwitch setOn:NO animated:YES];
[sender setOn:YES animated:YES];
break;
default:
break;
}
}
If you want to do it your way, that's OK but again less optimal. However if you do, the code:
UISwitch *switchObj = (UISwitch*)sender;
if (switchObj == self.switchOne){
is redundant. instead you can wrote the following:
-(IBAction)toggleButtonPressed:(UISwitch *)sender{
// UISwitch *switchObj = (UISwitch*)sender;
if (sender == self.switchOne){
// do stuff
}
I hope that helps.
I created an app to test UIActivityIndicatorView styles on different coloured backgrounds and it uses sliders and switches. Here the url if anyone wants to see the app with IB components:
https://github.com/asadquraishi/UIActivityIndicatorView-Test