代码创建Label和Button
- (void)viewDidLoad {
[super viewDidLoad];
// label
CGRect screen = [[UIScreen mainScreen]bounds];
CGFloat labelWidth = 90;
CGFloat labelHeight = 50;
CGFloat labelTopView = 150;
CGRect labelFrame = CGRectMake((screen.size.width - labelWidth)/2, labelTopView, labelWidth, labelHeight);
UILabel * label = [[UILabel alloc]initWithFrame:labelFrame];
label.text = @"testhehehe";
label.textAlignment = NSTextAlignmentCenter;
[self.view addSubview:label];
// button
UIButton * button = [UIButton buttonWithType:UIButtonTypeSystem];
[button setTitle:@"titt" forState:UIControlStateNormal];
CGFloat btnWith = 90;
CGFloat btnHeight = 50;
CGFloat btnTopView = 600;
button.frame = CGRectMake((screen.size.width - btnWith)/2, btnTopView, btnWith, btnHeight);
[self.view addSubview:button];
// button 点击事件
[button addTarget:self action:@selector(onclick:) forControlEvents:UIControlEventTouchDown];
}
/**
*点击事件响应的方法
*/
-(void)onclick:(id)sender{
NSLog(@"click button");
}
此处创建button采用的是静态工厂方法。
UIButtonType枚举成员:
UIButtonTypeCustom 自定义按钮
UIButtonTypeSystem 系统默认
UIButtonTypeDetailDisclosure 细节展示
UIButtonTypeInfoLight light风格信息按钮
UIButtonTypeInfoDark dark风格信息按钮
UIButtonTypeContactAdd 添加联系人按钮
forState按钮状态:
- UIControlStateNormal 默认
- UIControlStateHightLighted 高亮
- UIControlStateDisabled 不可用
- UIControlStateSelected 选择状态
访问视图
故事版拖动:选择outlet
#import "SecondViewController.h"
@interface SecondViewController ()
- (IBAction)test:(id)sender;
@property (weak, nonatomic) IBOutlet UILabel *bigLabel;
@property (weak, nonatomic) IBOutlet UILabel *label;
@end
@implementation SecondViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (IBAction)test:(id)sender {
self.label.text = @"woshilabel";
}
@end
代码实现:
#import "FirstViewController.h"
@interface FirstViewController ()
@property (strong,nonatomic)UILabel * label;
@end
@implementation FirstViewController
- (void)viewDidLoad {
[super viewDidLoad];
// label
CGRect screen = [[UIScreen mainScreen]bounds];
CGFloat labelWidth = 90;
CGFloat labelHeight = 50;
CGFloat labelTopView = 150;
CGRect labelFrame = CGRectMake((screen.size.width - labelWidth)/2, labelTopView, labelWidth, labelHeight);
self.label = [[UILabel alloc]initWithFrame:labelFrame];
self.label.text = @"testhehehe";
self.label.textAlignment = NSTextAlignmentCenter;
[self.view addSubview:self.label];
// button
UIButton * button = [UIButton buttonWithType:UIButtonTypeSystem];
[button setTitle:@"titt" forState:UIControlStateNormal];
CGFloat btnWith = 90;
CGFloat btnHeight = 50;
CGFloat btnTopView = 600;
button.frame = CGRectMake((screen.size.width - btnWith)/2, btnTopView, btnWith, btnHeight);
[self.view addSubview:button];
// button 点击事件
[button addTarget:self action:@selector(onclick:) forControlEvents:UIControlEventTouchDown];
}
/**
*点击事件响应的方法
*/
-(void)onclick:(id)sender{
NSLog(@"click button");
self.label.text = @"xixi";
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
注意:此处的label对象是weak属性,因为所有权是故事板,而在代码定义中是strong,若代码定义时属性也设置为weak,则label对象创建后就会被释放,strong保证了不被释放,所有权属于视图控制器
键盘相关
/**
textfield响应键盘return方法
*/
- (BOOL)textFieldShouldReturn:(UITextField *)textField{
NSLog(@"textField get focus and click return");
// 调用此方法关闭键盘
[textField resignFirstResponder];
return YES;
}
- (void)viewWillAppear:(BOOL)animated{
// 注册键盘打开通知
[[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(keyboradDidShow) name:UIKeyboardDidShowNotification object:nil];
// 注册键盘关闭通知
[[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(keyboradDidHide) name:UIKeyboardDidHideNotification object:nil];
}
-(void) keyboradDidShow{
NSLog(@"键盘打开");
}
-(void) keyboradDidHide{
NSLog(@"键盘关闭");
}
- (void)viewWillDisappear:(BOOL)animated{
// 注销广播操作
[[NSNotificationCenter defaultCenter]removeObserver:self name:UIKeyboardDidShowNotification object:nil];
[[NSNotificationCenter defaultCenter]removeObserver:self name:UIKeyboardDidHideNotification object:nil];
}