// Created by 妖精的尾巴 on 15-8-14.ide
// Copyright (c) 2015年 妖精的尾巴. All rights reserved.atom
//spa
#import "ViewController.h".net
@interface ViewController ()<UIAlertViewDelegate>3d
@property(nonatomic,strong)UITextField* textField;orm
@end get
@implementation ViewControllerit
- (void)viewDidLoadio
{event
[super viewDidLoad];
self.view.backgroundColor=[UIColor whiteColor];
[self createLabel];
[self createTextField];
[self createButton];
}
-(void)createLabel
{
UILabel* label=[[UILabel alloc]initWithFrame:CGRectMake(110, 80, 100, 50)];
label.text=@"妖精的尾巴";
label.textAlignment=NSTextAlignmentCenter;
label.textColor=[UIColor blueColor];
label.font=[UIFont systemFontOfSize:17];
[self.view addSubview:label];
UILabel* label2=[[UILabel alloc]initWithFrame:CGRectMake(30, 120, 80, 50)];
label2.text=@"用戶名:";
label2.textAlignment=NSTextAlignmentCenter;
label2.textColor=[UIColor greenColor];
label2.font=[UIFont systemFontOfSize:16];
[self.view addSubview:label2];
}
-(void)createTextField
{
self.textField=[[UITextField alloc]initWithFrame:CGRectMake(110, 120, 150, 35)];
self.textField.borderStyle= UITextBorderStyleRoundedRect;
self.textField.placeholder=@"請輸入用戶名";
self.textField.font=[UIFont systemFontOfSize:16];
[self.view addSubview:self.textField];
}
-(void)createButton
{
UIButton* btn=[UIButton buttonWithType:UIButtonTypeRoundedRect];
btn.layer.cornerRadius=10;
btn.showsTouchWhenHighlighted=YES;
btn.backgroundColor=[UIColor yellowColor];
btn.frame=CGRectMake(110,180,120,50);
[btn setTitle:@"認真你就贏了" forState:UIControlStateNormal];
[btn setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
[btn addTarget:self action:@selector(btnClick) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:btn];
}
-(void)btnClick
{
/**
*對UITextField調出鍵盤的退出處理方式有兩種
*
*第一種 在按鈕點擊方法中執行[self.textField resignFirstResponder];
*
*第二種 在-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event執行[self.view endEditing:YES];
*
*/
[self.textField resignFirstResponder];
if (self.textField.text.length==0) {
UIAlertView* alter=[[UIAlertView alloc]initWithTitle:@"舒適提示" message:@"你輸入的文字不能爲空" delegate:self cancelButtonTitle: @"肯定" otherButtonTitles:@"取消",nil];
[alter show];
}
else{
NSLog(@"用戶輸入了文字,文字內容是:%@",self.textField.text);
}
}
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
[self.view endEditing:YES];
}
#pragma mark-UIAlertViewDelegate方法
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
if (buttonIndex==0) {
NSLog(@"用戶開始輸入文字");
}
}
運行結果以下: