This is the ButtonPressed example. This is the very simple example. In this example we will see after pressing button message will display and dragging in the screen message will hide. So let see how it will worked. Another ButtonPress example you can find out from here ButtonPressed
Step 1: Open the Xcode, Create a new project using View base application. Give the application “ButtonFun_View”.
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: Xpand classes and notice Interface Builder created the ButtonPressViewController class for you and generated a separate nib, ButtonFun_ViewViewController.xib for the “ButtonFun_View”.
Step 4: Open the ButtonFun_ViewViewController.h file and make the following chanages:
@interface ButtonFun_ViewViewController : UIViewController {
IBOutlet UIButton *button;
IBOutlet UILabel *label;
}
@property (nonatomic, retain) UIButton *button;
@property (nonatomic, retain) UILabel *label;
–(IBAction) buttonPressed;
@end
Step 5: Double click the ButtonFun_ViewViewController.xib file and open it to the Interface Builder. First drag the Round Rect button from the library and place it to the view window, next drag the label from the library and place it to the view window. Select the Round Rect button and bring up Connection Inspector and connect Touch up Inside to the Files Owner icon and select buttonPressed: method. and connect Files Owner icon to the label and select label. Now save the .xib file, save it and go back to the Xcode.
Step 6: In the ButtonFun_ViewViewController.m file make the following changes:
@implementation ButtonFun_ViewViewController
@synthesize button;
@synthesize label;
–(IBAction) buttonPressed {
label.text = @"Drag to hide label.";
}
–(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{
button.hidden = YES;
label.hidden = YES;
}
–(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
button.hidden = NO;
label.hidden = NO;
label.text = @"";
}
– (void)dealloc
{
[super dealloc];
}
– (void)didReceiveMemoryWarning
{
// Releases the view if it doesn’t have a superview.
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren’t in use.
}
#pragma mark – View lifecycle
/*
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
– (void)viewDidLoad
{
[super viewDidLoad];
}
*/
– (void)viewDidUnload
{
[super viewDidUnload];
}
– (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
@end
Step 7: Now compile and run the application on the Simulator.
You can Download SourceCode from here ButtonFun_View