Pages

Labels

Wednesday, April 11, 2012

Screen Rotation, Web Views and Dynamic Views


3.1  Landscape and portrait mode rotation and view Alignment

            iPhone/iPad  Application can be view in both portrait and landscape mode. Developers can set the view as portrait or landscape as per the requirement of project. User can enable the landscape mode functionality by using the following function

  - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
  {
      // Return YES for supported orientations
      return (interfaceOrientation == UIInterfaceOrientationPortrait);
  }
   If the user wants to enable the landscape function, just write the code as return YES as follows,

  - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
  {
       return YES;
  }

            User have to align the objects position in the landscape mode via coding.

3.2  Dealing with web view

            If the user need to open any http link in his application, then UIWebView is the object to provide that functionality. UIWebView help the user to load any web pages in his application. The web view can also be used to create your own browsers in iPhone application.
           
            In the following example, web view is used to load the google web page on the iPhone application.

            In the header section first we have to create object for web view as UIWebView *googleView. We have to add UIWebViewDelegate file  in the header section so that we can use its delegate function in the implementation section. Header section file will be look like as shown in Figure 3.2.1. In the header file, we have to add one more object called UIActivityIndicatorView. The activity indicator view will indicates that the web page is loading now

            In the implementation section we have to write codings in ViewDidLoad function. Because, the web page should load as starting page of application. Implementation section codings are look like in figure 3.2.2





            Figure 3.2.1





            Figure 3.2.2





            After writing the codings in header and implementation files, we have to connect the outlets for web view and activity indicator view as shown in following figure




            Just drag and drop UIWebView and UIActivityIndicator view from Objects pane and connect the outlets by right clicking on files owner. Right click on the web view and drag and drop the delegate in the files's owner.

            In viewDidLoad function, first we initiate an URLRequest with url “http:\\www.google.com” and load that URL in the Web view by using the coding  [googleView loadRequest:req] .

            Here two delegates function for web view are used. In the first function, we are animating the activity view. So the activity view will start animating during the loading process and in the second delegate function , we will stop the animating process. The second delegate function indicates the process that will do after finish loading process of web view. In the finish loading process, we set the activity view as hidden by using hidden property of object .

            Build and run the application in iPhone simulator, finally we will get the out put as follows,











3.3  Generating dynamic views

            In this section we will see about dynamic views. i.e., the user can create number of views as he required for particular application. The views include UIView, UIButton, UILabel, UITextField ,etc. Now we will see an example to generate number of user define view with button and label.

            In the header section we have to declare UIView and UILabel. Declare  UITextField and UIButton as outlet . Create one IBAction for dynamic view generation.

            #import <UIKit/UIKit.h>
        
         @interface TestViewController : UIViewController
         {
         UIView *view;
         UIButton *btn;
         UILabel *lbl;
         IBOutlet UIButton *actionBtn;
         IBOutlet UITextField *tf;
         }
         -(IBAction)hideKey:(id)sender;
         -(IBAction)generateView;
         @end

            In the implementation section we have to write coding as follows,


         - (void)viewDidLoad
         {
            viewYVal=60;
            lblYVal=5;
            [super viewDidLoad];
         }


         -(IBAction)hideKey:(id)sender
         {
            [sender resignFirstResponder];
         }


         -(IBAction)generateView
         {
  
             int count=[tf.text intValue];
            for(int i=0;i<count;i++)
         {
            //Creating UIView
            view=[[UIView alloc]initWithFrame:CGRectMake(5, viewYVal, 310, 65)];
            view.backgroundColor=[UIColor whiteColor];
            //Creating UILabel
            lbl=[[UILabel alloc]initWithFrame:CGRectMake(25, lblYVal, 100, 25)];
            lbl.textColor=[UIColor blackColor];
            lbl.backgroundColor=[UIColor clearColor];
            lbl.text=[[NSString alloc]initWithFormat:@"Label %d",i];
            [view addSubview:lbl];
            [self.view addSubview:view];
            viewYVal=viewYVal+60;
             lblYVal=lblYVal+5;
          }
   
     }

         Then connect the Actions and outlets and finally run the application. The output will be as follows,









3.4   Use of NSTimers

            NSTimers are used to call a function in particular time interval. For example, if you want to print a message in 2 seconds repeatedly,  timers can be  used. Here, we can show an example for timer using a label . Label text will changes as 1,2,3... for each second.

            For this, first we have to declare a NSTimer in header section with UILabel and an intger value to increment the count value and print the result.

#import <UIKit/UIKit.h>

    @interface TestViewController : UIViewController
         {
            NSTimer *timer;
            IBOutlet UILabel *Timerlbl;
             int timerval;
   
         }
         -(void)changeLabel;
         @end

    Then we have to define the methods and timer in implementation file. We have to enter following coding in implementation file.

         - (void)viewDidLoad
         {
              timerval=0;
              timer= [NSTimer scheduledTimerWithTimeInterval:1.0 target:self                                                                                     selector:@selector(changeLabel) userInfo:nil repeats:YES];
              [super viewDidLoad];
         }

         -(void)changeLabel
         {
            Timerlbl.text=[[NSString alloc]initWithFormat:@"%d",timerval];
            timerval=timerval+1;
         }
         - (void)viewDidUnload
         {
             [timer release];
            [super viewDidUnload];
         }

         Connect outlet in xib and run the application. We will get the  output as follows,







0 comments:

Post a Comment

 
Loading