ImageView Display After Pressing Button In iPhone

In this application we will see how to image display after pressing button. So let see how it will worked. Another Button Pressed example you can find out from here Change BackgroundColor

Step 1: Open the Xcode, Create a new project using View Base application. Give the application “Button_Fun”.

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 two UIViewController class in the project. So select the project -> New File -> Cocoa Touch ->ViewController class and give the class name ”ImageView” and “SecondImageView”.

Step 4: We need add also two resource in the project.

Step 5: Open the Button_FunViewController.h file and make the following changes in the file:

#import <uikit/UIKit.h>
@interface Button_FunViewController : UIViewController {

IBOutlet UIButton*button;
}
-(IBAction)ButtonPressed:(id)sender;
-(IBAction)SecondButtonPressed:(id)sender;

@end

 

 

Step 6: Double click the Button_FunViewController.xib file and open it to the Interface Builder. First drag the two Round Rect button from the library and place it to the view window and give the name of the buttons “FirstButton” and “SecondButton”. Select the FirstButton from the view window and bring up Connection Inspector and connect Touch Up Inside to the Files owner icon and select ButtonPressed: method. Select the another button “SecondButton” and bring up Connection Inspector and connect Touch Up Inside to the Files owner icon and select SecondButtonPressed: method.Now save the .xib file, close it and go back to the Xcode.

Step 7: Double click the ImageView.xib file and open it to the Interface Builder. Drag the image view from the library and place it to the view window. Select the ImageView from the view and bring up Attribute Inspector and select the “cutest-baby-5.jpg”. Now save the .xib file , close it and go back to the Xcode.

Step 8: Double click the SeconImageView.xib file and open it to the Interface Builder. Drag the image view from the library and place it to the view window. Select the ImageView from the view and bring up Attribute Inspector and select the “cutest-baby-6.jpg”. Now save the .xib file , close it and go back to the Xcode.

Step 9: In the Button_FunViewController.m file make the following changes:

 

#import "Button_FunViewController.h"
#import "ImageView.h"
#import "SecondImageview.h"

@implementation Button_FunViewController

-(IBAction)ButtonPressed:(id)sender
{
ImageView *imageView =[[ImageView alloc]
initWithNibName:@"ImageView"
bundle:nil];
[self.view addSubview:imageView.view];
}
-(IBAction)SecondButtonPressed:(id)sender
{
SecondImageview *secondImageView = [[SecondImageview alloc]
initWithNibName:@"SecondImageview"
bundle:nil];

[self.view addSubview:secondImageView.view];
}

- (void)dealloc
{
[super dealloc];
}

- (void)didReceiveMemoryWarning
{

[super didReceiveMemoryWarning];

}

#pragma mark - View lifecycle

- (void)viewDidUnload
{
[super viewDidUnload];
}

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

@end

 

 

Step 10: Now compile and run the application on the Simulator.

You can Download SourceCode from here Button_Fun

Leave a Comment: