Objective-C Learning Notes - UIButton

UIButton

    Base Attributes:

  • UIButtonType:

    • UIButtonTypeCustom

    • UIButtonTypeRoundedRect

    • UIButtonTypeDetailDisclosure

    • UIButtonTypeInfoLight

    • UIButtonTypeInfoDark

    • UIButtonTypeContactAdd


动态实现代码:

- (void)viewDidLoad {
    [super viewDidLoad];
    
    //初始化 UIButton 实例并设置样式
    UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    
    //设置按钮显示文字
    [button setTitle:@"Danger!Do not touch!" forState:UIControlStateNormal];
    
    //根据显示的文字自动调整大小
    [button sizeToFit];
    
    //设置按钮触发事件
    [button addTarget:self action:@selector(buttonDidPush) forControlEvents:UIControlEventTouchUpInside];
    
    //按钮居中
    button.center = self.view.center;
    
    //按钮大小???
    button.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin|UIViewAutoresizingFlexibleTopMargin|UIViewAutoresizingFlexibleBottomMargin;
    
    //添加按钮到 view 中
    [self.view addSubview:button];
                        
    // Do any additional setup after loading the view, typically from a nib.
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
//按下按钮后调用的函数
-(void)buttonDidPush{
    //初始化一个 UIAlertView 实例
    UIAlertView *alert = [[UIAlertView alloc]init];
    
    //赋值 UIAlertView 所显示的内容
    alert.message = @"yes! this is just a joke!";
    
    //添加一个按钮到该 UIALertView 中
    [alert addButtonWithTitle:@"OK"];
    
    //显示该 UIALertView
    [alert show];
}



标签:uibutton