Display the NewView after pressing Button

This is the NewView application. In this application we will see after lunch the application first, splash screen will come, later after MainScreen appears, in this screen we will see two buttons are there, after pressing first button “First View” will come , there is one back button and pressing back button the page return to MainScreen. So let see how it will work.

Step 1: Open a Xcode, Create a Window base application. Give the application name “Splash_Xcode”.

Step 2: Xcode automatically creates the directory structure and adds essential frameworks to it. You can explore the directory structure to check out the content of the directory.

Step 3: We need to add three ViewController class in the project. So select the project -> New File ->UIViewController subclass -> select with XIB for user interface -> then give file name “MainControllerView” . Do the same thing two more times and give the file name “FirstView” and “SecondView”.

Step 4: We need to add images for this application. Added two back groung images

i) Default.png

ii) lightingeffects.png

two buttons

i) firstview.png

ii) secondview.png

and one back button

i) backbutton.png

Step 5: Open the MainWindow.xib file , first drag the ImageView from the library and place it to the window, now select the window open the Attribute inspector and select the Default image.

Step 6: Now open the Spalsh_XcodeAppDelegate.m file and make the following changes:

#import "Spalsh_XcodeAppDelegate.h"
#import "MainControllerView.h"

@implementation Spalsh_XcodeAppDelegate

@synthesize window=_window;

(BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{

MainControllerView *mainController = [[MainControllerView alloc] init];

[_window addSubview:mainController.view];

// Override point for customization after application launch.
[self.window makeKeyAndVisible];
return YES;}

(void)applicationWillResignActive:(UIApplication *)application
{
}

(void)applicationDidEnterBackground:(UIApplication *)application
{
}

(void)applicationWillEnterForeground:(UIApplication *)application
{

}

(void)applicationDidBecomeActive:(UIApplication *)application
{

}

(void)applicationWillTerminate:(UIApplication *)application
{

}

(void)dealloc
{
[_window release];
[super dealloc];
}

@end

Step 7: In the MainControllerview.h file, we need to define FirstView and SecondView class, UiLabel for  displaying label, two buttons and two IBAction methods. So make the following changes:

#import <UIKit/UIKit.h>

@class FirstView;

@class SecondView;

@interface MainControllerView : UIViewController {

IBOutlet UILabel *label;
IBOutlet UIButton *firstView;
IBOutlet UIButton *secondView;

FirstView *firstView1;
SecondView *secondView1;

}

@property(nonatomic,retain)  IBOutlet UILabel *label;
@property(nonatomic,retain)  IBOutlet UIButton *firstView;
@property(nonatomic,retain)  IBOutlet UIButton *secondView;

(IBAction) FirstView:(id)sender;
(IBAction) SecondView:(id)sender;

@end

Step 8: Double click the MainControllerView.xib file and open it. Open the view from the .xib file and drag the ImageView from the library and place it to the view window. Select the View and open the attribute Inspector and select the lightingeffects.png. Now drag the two button and one Label from the library and place it to the View. First select the label and open the Attribute Inspector, change the text into “Please Click the Button”. Now select the button and open Attributes Inspector change the image name into “firstview.png” and change the type of the button into Custom. Do the same thing for the another button and load the “secondview.png”. Connect the File’s Owner icon to the label , FirstView button and SecondView button and select label, firstView and secondView. Now select FirstView button and open connection Inspector and connect Touch Up Inside to the File’s Owner icon and select FirstView: method. Do the same thing for the Second button and select the SecondView: method. Save the MainControllerView.xib file .

Step 9: Open the MainControllerview.m file, and make the following changes.

#import "MainControllerView.h"
#import "FirstView.h"
#import "SecondView.h"

@implementation MainControllerView
@synthesize label,firstView,secondView;

(IBAction) FirstView:(id)sender

{

firstView1 = [[FirstView alloc]
initWithNibName:@"FirstView"
bundle:nil];
[self.view addSubview:firstView1.view];

}

(IBAction) SecondView:(id)sender

{
secondView1 = [[SecondView alloc]
initWithNibName:@"SecondView"
bundle:nil];
[self.view addSubview:secondView1.view];
}

(id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {

}
return self;
}

(void)dealloc
{
[super dealloc];
}

(void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];

}

#pragma mark – View lifecycle

(void)viewDidLoad
{
[super viewDidLoad];
}

(void)viewDidUnload
{
[super viewDidUnload];

}

(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

@end

Step 10: Open the FirstView.h file and  make the following changes:

#import <UIKit/UIKit.h>
@class MainControllerView;

@interface FirstView : UIViewController {

IBOutlet UIButton *backButton;
MainControllerView *mainControllerView;
}

@property(nonatomic,retain)  IBOutlet UIButton *backButton;

@end

Step 11: Double click the FirstView.xib file and open it. First drag the label from library and place it to the view window. Select the label and open the Attribute Inspector , change the label name into “First View”. Drag the Navigation Bar from the library and place it to the top portion of the view and change the Tile of the Navigation Item into “First View”. Now save the FirstView.xib file.

Step 12: Open the FirstView.m file and  make the following changes:

#import "FirstView.h"
#import "MainControllerView.h"

@implementation FirstView

@synthesize backButton;

(id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}

(void)dealloc
{
[super dealloc];
}

(void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];

}

#pragma mark – View lifecycle

(void)viewDidLoad
{
[super viewDidLoad];

UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake(5,18,100,40);
[button setImage:[UIImage imageNamed:@"backbutton.png"] forState:UIControlStateNormal];
[button addTarget:self action:@selector(BackAction:) forControlEvents:UIControlEventTouchUpInside];
button.backgroundColor = [UIColor clearColor];

[self.view addSubview:button];

}

(IBAction) BackAction:(id)sender

{

mainControllerView = [[MainControllerView alloc]
initWithNibName:@"MainControllerView"bundle:nil];

[self.view addSubview:mainControllerView.view];
[mainControllerView.view release];

}

(void)viewDidUnload
{
[super viewDidUnload];
}

(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

@end

Step 13: Open the SecondView.h file and  make the following changes:

#import <UIKit/UIKit.h>

@class MainControllerView;

@interface SecondView : UIViewController {

IBOutlet UIButton *backButton;
MainControllerView *mainControllerView;
}

@property(nonatomic,retain)  IBOutlet UIButton *backButton;

@end

Step 14: Double click the SecondView.xib file and open it. First drag the label from library and place it to the view window. Select the label and open the Attribute Inspector , change the label name into “Second View”. Drag the Navigation Bar from the library and place it to the top portion of the view and change the Tile of the Navigation Item into “Second View”. Now save the SecondView.xib file.

Step 15: Open the SecondView.m file and  make the following changes:

#import "SecondView.h"
#import "MainControllerView.h"

@implementation SecondView

@synthesize backButton;

(id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
}
return self;
}

(void)dealloc
{
[super dealloc];
}

(void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];

}

#pragma mark – View lifecycle

(void)viewDidLoad
{
[super viewDidLoad];

UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake(5,18,100,40);
[button setImage:[UIImage imageNamed:@"backbutton.png"] forState:UIControlStateNormal];
[button addTarget:self action:@selector(BackAction:) forControlEvents:UIControlEventTouchUpInside];
button.backgroundColor = [UIColor clearColor];

[self.view addSubview:button];

}

(IBAction) BackAction:(id)sender

{

mainControllerView = [[MainControllerView alloc]
initWithNibName:@"MainControllerView"bundle:nil];

[self.view addSubview:mainControllerView.view];
[mainControllerView.view release];

}

(void)viewDidUnload
{
[super viewDidUnload];
}

(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

@end

Step 16: Now compile and run the application in the Simulator.

You can Download Source Code from here Spalsh_Xcode

Leave a Reply

Your email address will not be published. Required fields are marked *