non funzionano quando un UIButton è impostato in modalità UIButtonTypeCustom ! The states UIControlStateSelected or UIControlStateHighlighted 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 two-state button: toggle note. If we have created two images (and stato1.png stato2.png) for our button, we can proceed as follows:
1 2 3 | / / The header files that we create a global variable for usaremo / / Check the toggle were ToggleFlag BOOL; |
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 location 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: @ "" Forst: UIControlStateNormal]; [ UIImage imageNamed : @ "stato1.png" ] forState : UIControlStateNormal ] ; [ToggleButton setBackgroundImage: [UIImage imageNamed: @ "stato1.png"] Forst: 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; / / Execute Toogle toggleFlag =! toggleFlag; [ UIImage imageNamed : ( toggleFlag ) ? @ "stato1.png" : @ "stato2.png" ] forState : UIControlStateNormal ] ; [ButtonClicked setBackgroundImage: [UIImage imageNamed: (toggleFlag)? @ "Stato1.png" @ "stato2.png"] Forst: UIControlStateNormal]; } |






Latest Comments
Mark : @ Mark: Thanks for the answer. For example, if you have a code to save / use the folder ...
Mark : Thanks for the answer. For example, if you have a code to save / use the folder ...
Giovambattista Fazioli : @ Mark: in principle, absolutely, a lot depends on how and what to download. La. ..
Mark : Hello, good guide! I wanted to ask, you can use the same code to run a sql database? Instead of ...
Joseph : Hi I wanted to ask you how you can do in the post I uploaded a gallery of 50 images, for example, ...