In this tutorial will learn how to set two image button options i.e. Select Button Image and deselect Image button. When the Select button is clicked or selected the image of the button changes and deselect the button image also change so that you can know when button selected. This action trigger is done using Interface Builder, will add button and on that button will set image on default and also on selected and will write the code for that. So, we will create variable and also the action for the button. This project is based on View Based Application, and coding will be done in view Controller file. After making variables and the action for the button, connect it in Interface Builder.
Final Image will look like this:
My project name is selectAndDeselect and is based on view application. In view controller we will create variable for the button and will write the action for that so that when the button is clicked or pressed will change the image of the button. For that purpose we have to take on Boolean variable to check the condition.
Add this to .h file:
IBOutlet UIButton *selectButton;
In .m file will write the method for the action and in that check the condition based on the boolean variable if equal to zero then select button otherwise default button or deselect the button and after this will add boolean in view did load method.
Add this to .m file:
if (ButtonSelected == 0)
ButtonSelected = 1;
ButtonSelected = 0;
ButtonSelected == 0;
After Creating the variable and writing the action of the button in .h file, we have to connect the variable into the Interface Builder and also set the Image on the button before doing this set the button as custom and then set the image of the button (select and default) as shown below.
Make connections as shown:
Finally press Build And Go button.
Posted on: September 25, 2009 If you enjoyed this post then why not add us on Google+? Add us to your Circles