ios开发基础1

代码创建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];
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值