non funzionano quando un UIButton è impostato in modalità UIButtonTypeCustom ! The states UIControlStateSelected or UIControlStateHighlighted do not work when a UIButton mode is set UIButtonTypeCustom ! Or rather, do not work (because reserved for other types of button), for example to create a button with two states: toggle note. If we have created two images (stato1.png and stato2.png) for our button, we can proceed as follows:
1 2 3 | / / The header file that we create a global variable for usaremo / / Toggle the check was BOOL toggleFlag; |
Now we create our button:
1 2 3 4 5 6 7 8 9 | / / We create a button and we place it initially in the state "stato1.png" / / Edit initWithFrame: (CGRect)} {100,100,50,50 with the position and / / Size of your image has ; toggleFlag = YES; [ [ UIButton buttonWithType : UIButtonTypeCustom ] initWithFrame : ( CGRect ) { 100 , 100 , 50 , 50 } ] ; UIButton ToggleButton * = [[UIButton buttonWithType: UIButtonTypeCustom] initWithFrame: (CGRect) {100, 100, 50, 50}]; @ "" forState : UIControlStateNormal ] ; [ToggleButton setTitle: @ "" forState: UIControlStateNormal]; [ UIImage imageNamed : @ "stato1.png" ] forState : UIControlStateNormal ] ; [ToggleButton setBackgroundImage: [UIImage imageNamed: @ "stato1.png"] forState: UIControlStateNormal]; self action : @selector ( onToggle : ) forControlEvents : UIControlEventTouchUpInside ] ; [ToggleButton AddTarget: self action: @ selector (onToggle:) forControlEvents: UIControlEventTouchUpInside]; toggleButton ] ; [Self.view addSubview: ToggleButton]; |
When you click on the button will send a message to be managed onToggle :
1 2 3 4 5 6 7 | void ) onToggle : ( id ) sender { - (Void) onToggle: (id) sender { / / Retrieve pointer to UIButton ( UIButton * ) sender; UIButton ButtonClicked * = (UIButton *) sender; / / Executing the Toogle toggleFlag =! toggleFlag; [ UIImage imageNamed : ( toggleFlag ) ? @ "stato1.png" : @ "stato2.png" ] forState : UIControlStateNormal ] ; [ButtonClicked setBackgroundImage: [UIImage imageNamed: (toggleFlag)? @ "Stato1.png" @ "stato2.png"] forState: UIControlStateNormal]; } |






Latest Comments
vik : With strategic help!
Pepper : Hi there, I do not know if you're one of the creators of the WP plugin Bannerize. I have spotted a ...
Rosanna : Can anyone tell me how do I delete the Snap Shots window that opens automatically when I ...
blessed Maresca : I can not download any skypemote me spiegaaa
Matthew : Hello everyone, unfortunately Google does not allow the use of the API for Flash, but it will ...