如下代码包括标签UILable,文本框UITextField, 开关UISwitch,滑动调节器UISlide,按钮UIButton 。
响应方法:
[backgroundButtonaddTarget:self action:@selector(backgroundButtonClicked:)
forControlEvents:UIControlEventTouchUpInside];
[self.aSwitchaddTarget:self action:@selector(switchValueChanged:)
forControlEvents:UIControlEventValueChanged];
[self.aSlideraddTarget:self action:@selector(sliderValueChanged:)
forControlEvents:UIControlEventValueChanged];
#import
@interface AppDelegate : UIResponder <UIApplicationDelegate, UITextFieldDelegate>
@property (nonatomic, retain) UIWindow* window;
@property (nonatomic, retain) UITextField* textName;
@property (nonatomic, retain) UITextField* textAge;
@property (nonatomic, retain) UISwitch* aSwitch;
@property (nonatomic, retain) UISlider* aSlider;
@property (nonatomic, retain) NSTimer* aTimer;
@end
#import "AppDelegate.h"
@implementationAppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary*)launchOptions
{
_window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
UIView* view = [[UIView alloc] initWithFrame:[UIScreen mainScreen].applicationFrame];
view.backgroundColor = [UIColor orangeColor];
[self.window addSubview:view];
//创建文本标签
UILabel* label = [[UILabel alloc] initWithFrame:CGRectMake(10, 10, 70, 30)];
label.text = @"Name:";
label.textColor = [UIColor whiteColor];
label.backgroundColor = [UIColor clearColor];
[view addSubview:label];
[label release];
label = [[UILabel alloc] initWithFrame:CGRectMake(10, 50, 70, 30)];
label.text = @"Age:";
label.textColor = [UIColor whiteColor];
label.backgroundColor = [UIColor clearColor];
[view addSubview:label];
[label release];
//创建按钮// 作用取消键盘焦点
UIButton* backgroundButton = [UIButton buttonWithType:UIButtonTypeCustom];
backgroundButton.frame =[UIScreen mainScreen].applicationFrame;
backgroundButton.backgroundColor = [UIColor clearColor];
[backgroundButton addTarget:self action:@selector(backgroundButtonClicked:)
forControlEvents:UIControlEventTouchUpInside];
[view addSubview:backgroundButton];
//创建文本框
_textName = [[UITextField alloc] initWithFrame:CGRectMake(70,10, 230, 30)];
self.textName.textColor = [UIColor blackColor];
//设置字体为系统字体,大小为17点
self.textName.font = [UIFont systemFontOfSize:17.0];
//设置占位字符串,它会在文本框没有任何内容时显示提示信息
self.textName.placeholder = @"Please input yourname.";
self.textName.backgroundColor = [UIColor whiteColor];
//定义边框样式
self.textName.borderStyle = UITextBorderStyleRoundedRect;
//定义文本框的键盘类型
self.textName.keyboardType = UIKeyboardTypeDefault;
//设置键盘的确认键类型
self.textName.returnKeyType = UIReturnKeyDone;
//设置自动转换大小写的方式
self.textName.autocapitalizationType = UITextAutocapitalizationTypeWords;
//定义清除按钮的模式
self.textName.clearButtonMode = UITextFieldViewModeAlways;
//将应用程序委托对象作为文本框的委托对象
self.textName.delegate = self;
[view addSubview:self.textName];
_textAge = [[UITextField alloc] initWithFrame:CGRectMake(70,50, 230, 30)];
self.textAge.textColor = [UIColor blackColor];
self.textAge.font = [UIFont systemFontOfSize:17.0];
self.textAge.placeholder = @"Please input yourage.";
self.textAge.backgroundColor = [UIColor whiteColor];
self.textAge.borderStyle = UITextBorderStyleBezel;
self.textAge.keyboardType = UIKeyboardTypeNumberPad;
self.textAge.clearButtonMode = UITextFieldViewModeAlways;
[view addSubview:self.textAge];
UIButton* button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
button.frame = CGRectMake(220, 100, 80, 30);
[button setTitle:@"Submit" forState:UIControlStateNormal];
[button addTarget:self action:@selector(buttonClicked:)
forControlEvents:UIControlEventTouchUpInside];
[view addSubview:button];
//创建开关,指定的位置有效,尺寸无效
_aSwitch = [[UISwitch alloc] initWithFrame:CGRectMake(113, 226, 0, 0)];
[self.aSwitch addTarget:self action:@selector(switchValueChanged:)
forControlEvents:UIControlEventValueChanged];
[view addSubview:self.aSwitch];
_aSlider = [[UISlider alloc] initWithFrame:CGRectMake(18, 199, 284, 23)];
[self.aSlider addTarget:self action:@selector(sliderValueChanged:)
forControlEvents:UIControlEventValueChanged];
[view addSubview:self.aSlider];
//创建时钟对象,每隔1秒调用一次changeSliderValueByTimer方法
self.aTimer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self
selector:@selector(changeSliderValueByTimer) userInfo:nil repeats:YES];
[view release];
[self.window makeKeyAndVisible];
return YES;
}
- (void)changeSliderValueByTimer
{
NSLog(@"changeSliderValueByTimer");
[self.aSlider setValue:(self.aSlider.value + 0.05) animated:YES];
if(self.aSlider.value == self.aSlider.maximumValue)
{
//停止时钟
[self.aTimer invalidate];
}
}
- (void)sliderValueChanged:(id)sender
{
NSLog(@"Current slidervalue is: %f", self.aSlider.value);
}
- (void)switchValueChanged:(id)sender
{
NSLog(@"Change switch valueto: %@", self.aSwitch.on? @"ON": @"OFF");
}
- (BOOL)textFieldShouldReturn:(UITextField*)textField
{
[self.textName resignFirstResponder];
return YES;
}
- (void)backgroundButtonClicked:(id)sender
{
//取消文本框的第一响应状态,即取消输入焦点
[self.textName resignFirstResponder];
[self.textAge resignFirstResponder];
}
- (void)buttonClicked:(id)sender
{
NSLog(@"Name: %@, age:%@", self.textName.text, self.textAge.text);
[self.aSwitch setOn:!self.aSwitch.on animated:YES];
}
@end