A.storyboard 控件版
1.label
2.textfield
a.Keyboard Type
账号:Number Pad
密码:Number and Punctuation
b.Placeholder:提示文字
c.Clear Button: Appears with editing 清除按钮
d.Secure Text Entry:密码格式输入
3.button
1 @interface ViewController () 2 @property (weak, nonatomic) IBOutlet UITextField *qqField; 3 @property (weak, nonatomic) IBOutlet UITextField *pwdField; 4 5 - (IBAction)login; 6 7 @end 8 9 @implementation ViewController 10 11 - (void)viewDidLoad { 12 [super viewDidLoad]; 13 // Do any additional setup after loading the view, typically from a nib. 14 } 15 16 - (void)didReceiveMemoryWarning { 17 [super didReceiveMemoryWarning]; 18 // Dispose of any resources that can be recreated. 19 } 20 21 - (IBAction)login { 22 NSLog(@"%@ - %@", self.qqField.text, self.pwdField.text); 23 } 24 @end
B.通过代码创建按钮
1 @interface ViewController () 2 3 - (void) addLabel; 4 - (void) addTextField; 5 - (void) addLoginButton; 6 - (void) login; 7 8 @end 9 10 @implementation ViewController 11 12 - (void)viewDidLoad { 13 [super viewDidLoad]; 14 // Do any additional setup after loading the view, typically from a nib. 15 16 [self addLabel]; 17 [self addTextField]; 18 [self addLoginButton]; 19 } 20 21 - (void)didReceiveMemoryWarning { 22 [super didReceiveMemoryWarning]; 23 // Dispose of any resources that can be recreated. 24 } 25 26 - (void) addLabel { 27 UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 45, self.view.frame.size.width, 21)]; 28 [titleLabel setTextAlignment:NSTextAlignmentCenter]; 29 titleLabel.text = @"QQ登陆界面2"; 30 [self.view addSubview:titleLabel]; 31 32 UILabel *qqLabel = [[UILabel alloc] initWithFrame:CGRectMake(60, 91, 26, 21)]; 33 qqLabel.text = @"QQ"; 34 [self.view addSubview:qqLabel]; 35 36 UILabel *pwdLabel = [[UILabel alloc] initWithFrame:CGRectMake(60, 131, 34, 21)]; 37 pwdLabel.text = @"密码"; 38 [self.view addSubview:pwdLabel]; 39 } 40 41 - (void) addTextField { 42 UITextField *qqField = [[UITextField alloc] initWithFrame:CGRectMake(109, 87, 151, 30)]; 43 qqField.placeholder = @"请输入QQ账号"; 44 [qqField setClearButtonMode:UITextFieldViewModeWhileEditing]; 45 [qqField setKeyboardType:UIKeyboardTypeNumberPad]; 46 [qqField setBorderStyle:UITextBorderStyleRoundedRect]; 47 [qqField setTag:1]; 48 [self.view addSubview:qqField]; 49 50 UITextField *pwdField = [[UITextField alloc] initWithFrame:CGRectMake(109, 127, 151, 30)]; 51 pwdField.placeholder = @"请输入QQ密码"; 52 [pwdField setSecureTextEntry:YES]; 53 [pwdField setKeyboardType:UIKeyboardTypeNumbersAndPunctuation]; 54 [pwdField setClearButtonMode:UITextFieldViewModeWhileEditing]; 55 [pwdField setBorderStyle:UITextBorderStyleRoundedRect]; 56 [pwdField setTag:2]; 57 [self.view addSubview:pwdField]; 58 } 59 60 - (void) addLoginButton { 61 // 注意如果使用UIButtonTypeCustom,默认背景色和titleColor都是是白色,显示不出来 62 UIButton *loginButton = [UIButton buttonWithType:UIButtonTypeSystem]; 63 64 CGRect loginRect = CGRectMake(145, 177, 30, 30); 65 loginButton.frame = loginRect; 66 [loginButton setTitle:@"登陆" forState:UIControlStateNormal]; 67 [loginButton addTarget:self action:@selector(login) forControlEvents:UIControlEventTouchUpInside]; 68 69 [self.view addSubview:loginButton]; 70 } 71 72 #pragma mark - action 73 - (void) login { 74 UITextField *qqField = [self.view viewWithTag:1]; 75 UITextField *pwdField = [self.view viewWithTag:2]; 76 77 NSLog(@"登陆---->%@ - %@", qqField.text, pwdField.text); 78 } 79 @end
时间: 2024-09-28 21:24:17