iPhone Button Image


 

iPhone Button Image

In this tutorial will learn how to Create iPhone Buttons image and also how to set the image on button click on the view, this will be done using code. We are not going to using Interface Builder to add Button, whole thing is done by coding.

In this tutorial will learn how to Create iPhone Buttons image and also how to set the image on button click on the view, this will be done using code. We are not going to using Interface Builder to add Button, whole thing is done by coding.

iPhone Button Image

In this tutorial will learn how to Create iPhone Button image and also how to set the image on button click on the view, this will be done using code. We are not going to using Interface Builder to add Button, whole thing is done by coding. The Application is Based on Window and will write code for button action and also for button and then will load or set the image when button is clicked, so to do this we need to have on image and that image must be added into project file and then we can set the image on window.

Final View is:

iPhone Button image

My project name is ButtonImage, is Window Based Application.

In .h file there is no need for any change, will do coding only in .m file 

.h file will look like this:

#import <UIKit/UIKit.h>

@interface ButtonImageAppDelegate : NSObject <UIApplicationDelegate> {

    UIWindow *window;

}

@property (nonatomic, retain) IBOutlet UIWindow *window;

@end

First will create button through which will set image on window when button is clicked. so to create iphone buttons on window will write the code into DidFinishLaunching, will create button and also will set the size of button with but.frame = CGRectMake(105,180, 115, 35); and setting the title of button by using set Title method and button background is white or clear and then this button which is created is added to window and the variable created for button is released as shown. 

then Next step is to write the action for the button and also the method so that when we click button image is shown.so to do this will create a view with respective size and then will create a variable for an image and also will give the file name for the image so that the image variable can call that image, will add that subview to window and also the variable for that view is released. 

.h file will look like this:

#import "ButtonImageAppDelegate.h"

 @implementation ButtonImageAppDelegate

@synthesize window;

- (void)applicationDidFinishLaunching:(UIApplication *)application {   

    UIButton *but = [[UIButton alloc] init];

    but = [[UIButton buttonWithType:UIButtonTypeRoundedRect] retain];

    but.frame = CGRectMake(105,180, 115, 35);

    [but setTitle:@"MaGic" forState:UIControlStateNormal];

    but.backgroundColor = [UIColor clearColor];

    [but addTarget:self action:@selector(ApplyImage:) forControlEvents:UIControlEventTouchUpInside];

    [window addSubview:but];

    [window makeKeyAndVisible];

    [but release];

}

-(void)ApplyImage:(id)sender

{

    UIView *mview= [[UIView alloc] initWithFrame:CGRectMake(0, 0, 5, 60)];

0

    UIImageView *img=[[UIImageView alloc]initWithImage:[UIImage imageNamed:@"png.jpg"]];

    img.frame=CGRectMake(0, 0, 320, 480);

    [mview addSubview:img];

1

    [img release];

    [window addSubview:mview];

    [mview release];

2

}

- (void)dealloc {

    [window release];

3

    [super dealloc];

}

@end

4

FInally press Build And Go button

Download the Source Code

 

5

Ads