Introduction
In this article we create a single view application. Here we add three uiviewcontroller type Objective-C classes from cocoa touch. To add a view we do the following.
Select any Objective-C class and right-click on it ->choose new file ->select Objective-C class -> click on next. By default the class name is ViewController. When you choose to subclass the UIViewController, change it's name to whatever you want, if you want to use xib then you mark on it -> click on next -> select the location to import the view controller class in the project.
To better understand use the following procedure.
Step 1
Open Xcode by double-clicking on it.
Step 2
Create a New XCode Project by clicking on it.
Step 3
Now select Single View Application and click on Next.
Step 4
Now provide Product Name.
Step 5
Select the location where you want to save your project then click on Create.
Step 6
Now we add ViewController class.
Step 7
Select the appdelegate.m class and right-click on it.
Choose New File..
Step 8
Select Subclass of UIViewController from the drop down list and provide the class name. Here I use the class name. First the view controller then click on the checkbox with xib user interface for using a label and button from outlet. Click on next.
Step 9
Now click on create, to import a class to the project.
Repeat the same steps for the rest of the views.
Step 10
After that we write code and perform linking between outlet and product.
ViewController.h
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController
- (IBAction)view1:(id)sender;
- (IBAction)view2:(id)sender;
- (IBAction)view3:(id)sender;
@end
ViewController.m
#import "ViewController.h"
#import "FirstViewController.h"
#import "SecondViewController.h"
#import "ThirdViewController.h"
@interface ViewController ()
@end
@implementation ViewController
- (IBAction)view1:(id)sender
{
FirstViewController *view1 =[[FirstViewController alloc] initWithNibName:nil bundle:nil];
[self presentViewController:view1 animated:YES completion:nil];
}
- (IBAction)view2:(id)sender
{
SecondViewController *view2 =[[SecondViewController alloc] initWithNibName:nil bundle:nil];
[self presentViewController:view2 animated:YES completion:nil];
}
- (IBAction)view3:(id)sender
{
ThirdViewController *view3 =[[ThirdViewController alloc] initWithNibName:nil bundle:nil];
[self presentViewController:view3 animated:YES completion:nil];
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
FirstViewController.h
#import <UIKit/UIKit.h>
@interface FirstViewController : UIViewController
- (IBAction)home:(id)sender;
@end
FirstViewController.m
#import "FirstViewController.h"
#import "ViewController.h"
@interface FirstViewController ()
@end
@implementation FirstViewController
- (IBAction)home:(id)sender
{
ViewController *home =[[ViewController alloc] initWithNibName:nil bundle:nil];
[self presentViewController:home animated:YES completion:nil];
}
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
SecondViewController.h
#import <UIKit/UIKit.h>
@interface SecondViewController : UIViewController
- (IBAction)home:(id)sender;
@end
SecondViewController.m
#import "SecondViewController.h"
#import "ViewController.h"
@interface SecondViewController ()
@end
@implementation SecondViewController
- (IBAction)home:(id)sender
{
ViewController *home =[[ViewController alloc] initWithNibName:nil bundle:nil];
[self presentViewController:home animated:YES completion:nil];
}
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
ThirdViewController.h
#import <UIKit/UIKit.h>
@interface ThirdViewController : UIViewController
- (IBAction)home:(id)sender;
@end
ThirdViewController.m
#import "ThirdViewController.h"
#import "ViewController.h"
@interface ThirdViewController ()
@end
@implementation ThirdViewController
- (IBAction)home:(id)sender
{
ViewController *home =[[ViewController alloc] initWithNibName:nil bundle:nil];
[self presentViewController:home animated:YES completion:nil];
}
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
Step 11
Now select which Platform you want to see the output for.
Output
Output 1 in iPhone:
Here when we click on the "View1" Button the output shows on the iPhone Screen.
Output 2 in iPhone:
Here when we click on the "Home" button the output shows on the iPhone Screen.
Output 3 in iPhone:
Here when we click on the "View2" button the output shows on the iPhone Screen.
Output 4 in iPhone:
Here when we click on the "Home" button the output shows on the iPhone Screen.
Output 5 in iPhone:
Here when we click on the "View3" button the output shows on the iPhone Screen.
Output 6 in iPhone:
Here when we click on the "Home" button the output shows on the iPhone Screen.
Output 7 in iPhone: