Pages

Labels

Tuesday, April 10, 2012

Views, Outlets and Actions (Simple Calculator Program)


2.1  Declaring Actions And Outlets
           
            Actions and outlets are declare in the Header section and they are define in the Implementation section. Developer should connect appropriate actions and outlet to Xib (Interface Builder) to execute particular functionality.

            Following figure shows the declaration of Actions and Outlets in Header Section.

       Figure Shows the Declaration of Actions and Outlets



            We have to set property for the objects that we are going to use i our application. Retain will have the count for each object. If we release the memory for particular object, then retain count will be decreased . Property is mainly used for memory management purpose.

          Once we set property to particular object, we have to synthesize in implementation file. We have to synthesize  using the key @synthesize . Synthesizing object is shown in the figure 2.1.2. Definition of Actions that we have declare in header section is also done in implementation file as  shown in figure 2.1.2




           
           
            Figure 2.1.2
  




           

            In this program we are going to get two textfield values and  add them and display the result in a label.
            In action addValue, first we have to get the text value from the TextField which we have declare in the header section. For that, we can use TextField.text property to get the text value. But we will get this value as string type, we have to convert this in to integer , for that purpose we can use the property “intValue”. [tfFirst.text intValue]  means, it will return integer value for the text that we enter in the textfield tfFirst.
            Similarly get the second text value as integer and sum the result using “+” operator . Result is kept temporarily in an integer . Here “result” is the integer value in which sum result is stored.
We can't directly assign the text as integer in the label. So , first we have to convert the result as string. For that , we are declaring resultString and covert the integer as string value. Finally we are assign the resultString as text for the label.
            After Define the actions , we have to release the memory for the object that we are used in our application. Figure 2.1.3 shows how to release memory for objects that we are used in our application.
            We can release the object anywhere of the program after it's use. Here, the objects are released in viewDidUnLoad  method.
            To hide the key pad, we have to use “resignFirstResponder” method in an IBAction and connect that action with text field. Choose DidEndOnExit as the event.
             
            Figure 2.1.3




    Thats all we have to do in the coding section. Finally we have to connect Actions and outlets in  interface file (xib) and build and run the application. Connecting actions and outlets shown in figure
2.1.4.

            Figure 2.1.4







            Following figure shows the output for above program in iPhone simulator




2.2  Alert view and its delegates.

            Alert View is the popup  which is used to show some message in the iPhone application. The message may be a warning one, a hint or may be a yes or no question. Alert view delegates can be used in our application by using UIAlertViewDelegate in the header . We can declare alertView in the header section as “ UIAlertView *alert “. Here, alert is the object for UIAlertView. We can define alert view within an action, or it can also define within viewDidLoad function and so on.
           
            We can use the alert view in the above program to show the result as alert one. The figure 2.2.1 and 2.2.2 shows the declaration and definition of  alert view with its syntax.
           
            Alert view having certain delegates include clickButtonAtIndex,etc. Which will be used to find which button of alertView is clicked and so on. We can add text field, label, etc in to an alert view.We can customize our own alert view.



            Figure 2.2.1




            Figure 2.2.1







    
            Alert View having certain property such as title, message, delegate , cancel button title and other button title. If we need we can define other buttons as per our requirements. Following figure shows the output for the Alert View that we have created.






            By using alertView delegate such as   -(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex” ,  we can define different actions for different button click in an alert view.

2.3  Action Sheet and  its Usability

            Although the Alert view can display multiple buttons, its primary use is still as a tool to alert users when something happens. If you need to display a message with multiple choices for the user to select, you should use an Action Sheet rather than the Alert view. An action sheet displays a collection of but- tons among which the user can select one.
To use an action sheet, use the code snippet below:
            UIActionSheet *action = [[UIActionSheet alloc] initWithTitle:@”Title of Action Sheet”
            delegate:self cancelButtonTitle:@”OK”
            destructiveButtonTitle:@”Delete Message” otherButtonTitles:@”Option 1”, @”Option 2”,         nil];
            [action showInView:self.view];
            [action release];
            We can create an application with arithmetic basic operations such as addition, subtraction, multiplication and division. For that We have to include action sheet delegate  in the header file and declare action sheet as “ UIActionSheet *action “ . You can give any name as per your convenience.Button action can be define in action sheet delegate named 
-(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex” .
            Figure 2.3.1 and 2.3.2 shows the header and implementation file coding for action sheet.
           
            Figure 2.3.1




            Figure 2.3.2




            Output for the above program is as shown below



           



0 comments:

Post a Comment

 
Loading