Home Tutorial Iphone Examples iPhone Multiple View

 
 

Share on Google+Share on Google+
iPhone Multiple View
Posted on: September 16, 2009 at 12:00 AM
Advertisement
In this tutorial we will show you multiple view in iPhone Application, when you press button will take you to another view and also the button type will be changed according to the view, so that you can know that the view is changing.

iPhone Multiple View

In this iphone tutorial we will show you multiple view, when you press button will take you to another view and also the button type will be changed according to the view, so that you can know that the view is changing. This is done using only one view, the application which i am using is View Based Application. Everything in this is done by coding, the button used is of different type in different view. For this we will Add only the View Controller not the xib file for it, this is because we are writing all coding into the .h and .m file, without using the Interface Builder.

Final View Will Look Like This:

My project name is MultipleView and is Based on View Based Application. After that add four View Controller without Xib and add them into the classes file. Then in each view controller file will write the code for button and the action to the button. Before that will add some codes to .m and .h file of view. 

In MultipleViewViewController.h file add this:

-(void) Show:(int)intNewView;

In action will write the method, first of all will show the first view on to the view and by using the switch will shift the view whenever the button is clicked and in ViewDidLoad will fix first view as the main view i.e. when the project is compiled it will show the first view and on that you will have button, to go to next view click on button and will take you to next view.

In MultipleViewViewController.m file add this:

UIViewController  *currentView;

-(void) Show:(int)intNewView

{

    NSLog(@"%i", intNewView);

    [currentView.view removeFromSuperview];

    [currentView release];

    switch (intNewView)

    {

      case 1:

          currentView = [[First alloc] init];

          break;

      case 2:

          currentView = [[Second alloc] init];

          break;

      case 3:

          currentView =[[Thired alloc] init];

          break;

      case 4:

          currentView =[[Fourth alloc] init];

          break;

    }

    [self.view addSubview:currentView.view];

}

- (void)viewDidLoad {

    currentView = [[First alloc] init];

    [self.view addSubview:currentView.view];

    [super viewDidLoad];

}

Here am writing action named as goTwo and will call that method from another method called as viewDidload. In viewdidload method am creating button using UIButton and also the type of button, after that am fixing the size of the button with .frame = CGRectMake() and after creating, framing now setting the title for the button which is used to show next view and then the action is to go to the second view i.e. goTwo, add subview to the view.similarly add in second view controller.

In First.m file add this:

- (void)goTwo {

    MultipleViewAppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];

    [appDelegate Show:2];}

- (void)viewDidLoad {

    NSLog(@"First 1");

    UIButton *btnOne = [UIButton buttonWithType:UIButtonTypeInfoLight];

    btnOne.frame = CGRectMake(40, 40, 240, 30);

    [btnOne setTitle:@"InfoLight" forState:UIControlStateNormal];

    [btnOne addTarget:self action:@selector(goTwo) forControlEvents:UIControlEventTouchUpInside];

    [self.view addSubview:btnOne];

    [super viewDidLoad];

}

In Second.m file add this:

- (void)goThree

{

    MultipleViewAppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];

    [appDelegate Show:3];

}

- (void)viewDidLoad

{

    NSLog(@"Second 2");

    UIButton *btnTwo = [UIButton buttonWithType:UIButtonTypeInfoDark];

    btnTwo.frame = CGRectMake(40, 40, 240, 30);

    [btnTwo setTitle:@"InfoDark" forState:UIControlStateNormal];

    [btnTwo addTarget:self action:@selector(goThree) forControlEvents:UIControlEventTouchUpInside];

    [self.view addSubview:btnTwo];

    [super viewDidLoad];

}

After adding everything press Build And Go Button

Download Here

Advertisement

Related Tags for iPhone Multiple View:


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: September 16, 2009

Recommend the tutorial

Advertisements Advertisements
 

 

 

DMCA.com