Tuesday, April 19, 2011

How To Switch Views using Multiple Viewcontrollers

1) Open up XCode > New Project > View Based Application

2) Call it SwitchViews and hit Save If you look under your resources folder, you can see Xcode already gives you new NIB (.xib) files, and already interconnects them New Project

3) Now we need to add another NIB file, but first lets add the class files first. Click File > New File UIViewController subclass. Call it NextView.m and hit Save. (check the box that says create .h too)

Add Subclass

4) It will be added under your classes folder. Now, under your Classes folder, click on SwitchViewsViewController.h to open up the header file.

5. Type this -(IBAction)next:(id)sender;

6) Double click on SwitchViewsViewController.xib

7) Then click Tools > Library to open up your library

8) In the Library type in: “button” [with out the quotes]

9) Drag over a Round Rect Button, double click it and title it “Next View”

10) Click Tools > Inspector to open the inspector, now click Window > Document (to open the document)

11) In the Document window select File’s Owner, in the inspector select the second tab from the left (with an arrow sign next to it)

12) Under Received Actions you should see this

next: with a circle next to it

13) Click on that circle next to it and drag it over to the button-there should a blue line while dragging

A pop-up menu will appear from there select “Touch Up Inside”

14) File > Save and then go back to Xcode

15) Go to the SwitchViewsViewController.m and on the top where it says #import…

type in #import “NextView.h”

then after @implementation


-(IBAction)next:(id)sender {

NextView *NView = [[NextView alloc] initWithNibName:nil bundle:nil];
[self presentModalViewController:NView animated:YES];


Note:If you don’t want it animated you can change the YES to NO

15) Build the project.. When you click NextView it should open the otherview

16) Now go into the NextView.h file and create another action


17) Repeat steps 6-14 (using the new action)

18) Go to the NextView.m

and type this

-(IBAction)back:(id)sender {

[self dismissModalViewControllerAnimated:YES];


19)Build and Go, you should have NO warning or errors and the app should work perfectly

No comments:

Post a Comment