在程序代码中设定控件调用的步骤
在程序代码中设定控件调用的方法
一般情况下,我们都是通过在XIB界面中添加控件,然后在程序中编写控件方法,最后通过XIB将控件和方法进行绑定。
本文,将介绍如何直接在程序中将编写的方法与控件进行绑定。
这里的例子将继续文章《通过Window-based Application创建项目(一) 》中的示例!
操作很简单,参考下面代码中的红色部分。
FirstViewController.h 修改:
#import <UIKit/UIKit.h>
@interface FirstViewController : UIViewController {
UILabel *label;
UIButton *button;
}
@property (nonatomic,retain) UILabel *label;
@property (nonatomic,retain) UIButton *button;
-(IBAction)myButtonClicked:(id)sender;
@end
FirstViewController.m 修改:
#import "FirstViewController.h"
@implementation FirstViewController
@synthesize label,button;
-(IBAction)myButtonClicked:(id)sender{
UIAlertView *alert=[[UIAlertView alloc] initWithTitle:@"my alert"
message:@"button is clicked"
delegate:self
cancelButtonTitle:@"Ok"
otherButtonTitles: nil];
[alert show];
[alert release];
}
// The designated initializer. Override if you create the controller programmatically and want to perform customization that is not appropriate for viewDidLoad.
/*
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization.
}
return self;
}
*/
/*
// Implement loadView to create a view hierarchy programmatically, without using a nib.
- (void)loadView {
}
*/
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
//create the label
CGRect frame=CGRectMake(50, 30, 200, 45);
label=[[UILabel alloc] initWithFrame:frame];
label.text=@"This is a label";
//create the button
frame=CGRectMake(50, 100, 200, 45);
button=[UIButton buttonWithType:UIButtonTypeRoundedRect];
button.frame=frame;
[button setTitle:@"OK" forState:UIControlStateNormal];
[button addTarget:self
action:@selector(myButtonClicked:)
forControlEvents:UIControlEventTouchUpInside];
//add the label and button into current view.
[self.view addSubview:label];
[self.view addSubview:button];
[super viewDidLoad];
}
/*
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
// Return YES for supported orientations.
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
*/
- (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.
}
- (void)viewDidUnload {
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
- (void)dealloc {
[label release];
[button release];
[super dealloc];
}
@end
运行后,点击界面上的【OK】按钮,即显示效果如下图:
关于控件对应方法在系统中的常量定义如下表:
Kinds of events possible for control objects. A touch-down event in the control. Available in iOS 2.0 and later. Declared in A repeated touch-down event in the control; for this event the value of the UITouch Available in iOS 2.0 and later. Declared in An event where a finger is dragged inside the bounds of the control. Available in iOS 2.0 and later. Declared in An event where a finger is dragged just outside the bounds of the control. Available in iOS 2.0 and later. Declared in An event where a finger is dragged into the bounds of the control. Available in iOS 2.0 and later. Declared in An event where a finger is dragged from within a control to outside its bounds. Available in iOS 2.0 and later. Declared in A touch-up event in the control where the finger is inside the bounds of the control. Available in iOS 2.0 and later. Declared in A touch-up event in the control where the finger is outside the bounds of the control. Available in iOS 2.0 and later. Declared in A system event canceling the current touches for the control. Available in iOS 2.0 and later. Declared in A touch dragging or otherwise manipulating a control, causing it to emit a series of different values. Available in iOS 2.0 and later. Declared in A touch initiating an editing session in a Available in iOS 2.0 and later. Declared in A touch making an editing change in a Available in iOS 2.0 and later. Declared in A touch ending an editing session in a Available in iOS 2.0 and later. Declared in A touch ending an editing session in a Available in iOS 2.0 and later. Declared in All touch events. Available in iOS 2.0 and later. Declared in All editing touches for Available in iOS 2.0 and later. Declared in A range of control-event values available for application use. Available in iOS 2.0 and later. Declared in A range of control-event values reserved for internal framework use. Available in iOS 2.0 and later. Declared in All events, including system events. Available in iOS 2.0 and later. Declared in Control Events
enum {
UIControlEventTouchDown = 1 << 0,
UIControlEventTouchDownRepeat = 1 << 1,
UIControlEventTouchDragInside = 1 << 2,
UIControlEventTouchDragOutside = 1 << 3,
UIControlEventTouchDragEnter = 1 << 4,
UIControlEventTouchDragExit = 1 << 5,
UIControlEventTouchUpInside = 1 << 6,
UIControlEventTouchUpOutside = 1 << 7,
UIControlEventTouchCancel = 1 << 8,
UIControlEventValueChanged = 1 << 12,
UIControlEventEditingDidBegin = 1 << 16,
UIControlEventEditingChanged = 1 << 17,
UIControlEventEditingDidEnd = 1 << 18,
UIControlEventEditingDidEndOnExit = 1 << 19,
UIControlEventAllTouchEvents = 0x00000FFF,
UIControlEventAllEditingEvents = 0x000F0000,
UIControlEventApplicationReserved = 0x0F000000,
UIControlEventSystemReserved = 0xF0000000,
UIControlEventAllEvents = 0xFFFFFFFF
};
Constants
UIControlEventTouchDown
UIControl.h
.UIControlEventTouchDownRepeat
tapCount
method is greater than one.UIControl.h
.UIControlEventTouchDragInside
UIControl.h
.UIControlEventTouchDragOutside
UIControl.h
.UIControlEventTouchDragEnter
UIControl.h
.UIControlEventTouchDragExit
UIControl.h
.UIControlEventTouchUpInside
UIControl.h
.UIControlEventTouchUpOutside
UIControl.h
.UIControlEventTouchCancel
UIControl.h
.UIControlEventValueChanged
UIControl.h
.UIControlEventEditingDidBegin
UITextField
object by entering its bounds.UIControl.h
.UIControlEventEditingChanged
UITextField
objet.UIControl.h
.UIControlEventEditingDidEnd
UITextField
object by leaving its bounds.UIControl.h
.UIControlEventEditingDidEndOnExit
UITextField
object.UIControl.h
.UIControlEventAllTouchEvents
UIControl.h
.UIControlEventAllEditingEvents
UITextField
objects.UIControl.h
.UIControlEventApplicationReserved
UIControl.h
.UIControlEventSystemReserved
UIControl.h
.UIControlEventAllEvents
UIControl.h
.