ButtonHide Example in iPhone

In this application we will see how to hide button from the iPhone application. So let see how it will worked.

Step 1: Open the Xcode, Create a new project using Single View Application. Give the application
“ButtonHide”.

Step 2: Need to add new viewController class in the project. Select project -> New file ->
UIViewController subclass -> next -> Give the application name “ButtonHideView”.

Step 3: Now open the AppDelegate.h file and make the following changes:

#import <UIKit/UIKit.h>

@class ButtonHideView;

@interface AppDelegate : UIResponder <UIApplicationDelegate>
{
ButtonHideView *buttonHideView;
}
@property (nonatomic, retain)IBOutlet ButtonHideView *buttonHideView;
@property (strong, nonatomic) UIWindow *window;

@end

Step 4: In the AppDelegate.m file make the following changes:

#import "AppDelegate.h"
#import "ButtonHideView.h"

@implementation AppDelegate

@synthesize window = _window,buttonHideView;

(BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary
*)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];

buttonHideView = [[ButtonHideView alloc]
initWithNibName:@"ButtonHideView"
bundle:nil];
[_window addSubview:buttonHideView.view];

return YES;
}

@end

Step 5: Now open the “ButtonHideView.h” file and make the following changes:

#import <UIKit/UIKit.h>

@interface ButtonHideView : UIViewController
{
IBOutlet UIButton *button1;
IBOutlet UITextField *textfield1;

}
@property (nonatomic, retain) IBOutlet UIButton *button1;
@property (nonatomic, retain) IBOutlet UITextField *textfield1;

(IBAction) ButtonHide:(id)sender;
//}

@end

Step 6: Double click the ButtonHideView.xib file and open it to the Interface Builder. First drag the
UIButton and UITextField from the library and place it to the view window. Select the button from
the view window and bring up connection inspector and connect touch up inside to the File’s Owner
icon and select “ButtonHide:” method and connect the File’s owner icon to the textfield and select
“textfield1”. Now save the .xib file, close it and go back to the Xcode.

Step 7: In the ButtonHideView.m file make the following changes:

#import "ButtonHideView.h"

@implementation ButtonHideView

@synthesize button1,textfield1;

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

(IBAction) ButtonHide:(id)sender
{
if([textfield1.text isEqualToString: @""]){
button1.hidden = YES;

} else {
button1.hidden = NO;

}
NSLog( @"%@", textfield1.text );
}

@end

Step 8: Now Compile and run the application on the Simulator

Leave a Reply

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