Hello everyone!! In my previous posts i promised that i would post a tutorial on Actions and how to trigger it . So here we go !! . This tutorial explains on how to write your own method . Sometimes you want to trigger an event to occur when you click/tap a button in your application . For eg : Addition of two numbers!! you place two text boxes to take input of two two numbers, and a button is placed. when you click that button ,the arithmetic calculation happens and the output is shown in a label. Now lets hop on to my tutorial . Here am placing a text box , a label and a button . In the text box a text is entered and when i click the button the text given in the text box is produced on the label. Before we dive into the tutorial lets learn some syntaxes of Actions.
Actions or Methods are declared in .h files and outside the @interface braces as shown in the image below.
There are two types of methods user defined and pre-defined . IBAction is a predefined one. We use IBAction so that in the .xib file under file's owner if u right click you could see it,like in the image below
Under received actions you can see my method thisismyaction. But if you declare an user defined method say like ,-(void)thisismyaction ,this wont be listed in the .xib file , where ,you want to manually add the acton to the corresponding button.
You may wonder why i write the methods like this -(IBAction) , this is a format of objective c programming where we would write the method type within braces . And the symbol minus "-" you find before the (IBAction) is for a special reason ,we also use the plus symbol "+". I will discuss about this in detail in future posts . For now just take as it is . As i have already discussed(first para) about a simple program , i am going to implement that now . My .h file looks like this
Now my .m file
Here i equal the value of the text to my label .
You can see that i have equalled the label and text value in the code lbl.text=text1.text . And this code is written under the method -(IBAction)show . Now to the .xib file to connect the methods and buttons My .xib file is shown below,i have placed a label to bring out the output and a text box to receive the input.And a button. Do remember to connect every variable declared in .h files that are show under File'sOwner to the objects(textfields,labels etc) on the ViewController(the grey screen). If not sure check previous posts on how to connect it.
My method -(IBAction)show is displayed in the received actions ,now connect it to the button named show. So that when i click this button ,it executes all the codes inside it.Select the option TouchUpInside ,will explain about how these options work later.
Now i have connected everything to bring out the output. My output is show below
I typed khan in the text box and then i clicked the button show ,and brought the output in the label.
Now you know how to trigger an action . Try some programs like Addition of two numbers,BMI etc with these actions.
Feel free to comment/mail for any doubts. Cheers!!