iOS UI入门——使用Objective-C和Swift实现UIButton的显示和点击事件

  • Objective-C实现UIButton的显示和点击事件
-(void)setupButton{
    //初始化button
    UIButton * testButton = [UIButton buttonWithType:UIButtonTypeCustom];
    //设置位置和大小
    testButton.frame = CGRectMake(20, 300, self.view.frame.size.width - 40, 40);
    //设置背景色
    testButton.backgroundColor = [UIColor orangeColor];
    //设置标题
    [testButton setTitle:@"点我点我" forState:UIControlStateNormal];
    //设置标题颜色
    [testButton setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
    //设置标题字体大小
    testButton.titleLabel.font = [UIFont systemFontOfSize:15];
    //设置点击事件
    [testButton addTarget:self action:@selector(testButtonClick) forControlEvents:UIControlEventTouchUpInside];
    //将button添加到父self.view上来做显示
    [self.view addSubview:testButton];
}

-(void)testButtonClick{
    NSLog(@"点啦点啦");
}
  • Swift实现UIButton的显示和点击事件
    func setupButton() {
        //初始化button
        let testButton = UIButton.init(type: UIButtonType.custom)
        //设置位置和大小
        testButton.frame = .init(x: 20, y: 300, width: self.view.frame.size.width - 40, height: 40)
        //设置背景色
        testButton.backgroundColor = UIColor.orange
        //设置标题
        testButton.setTitle("点我点我", for: UIControlState.normal)
        //设置标题颜色
        testButton.setTitleColor(UIColor.red, for: UIControlState.normal)
        //设置标题字体大小
        testButton.titleLabel?.font = UIFont.boldSystemFont(ofSize: 15)
        //设置点击事件,不带参数和带参数的点击方法
        //testButton.addTarget(self,action:#selector(testButtonClick), for: .touchUpInside)  不带参数
        testButton.addTarget(self, action:#selector(buttonClick(_button:)), for: .touchUpInside)//带参数
        //将button添加到父self.view上来做显示
        self.view.addSubview(testButton)
    }

    @objc func testButtonClick(){
        print("点啦点啦")
    }

    @objc func buttonClick(_button:UIButton) {
        print(String.init(format: "点了:%@", _button))
    }

与Objective-C相比就是添加点击事件差的比较多,写的时候很容易报错,建议先写好点击方法,再去写添加点击事件的语句。
- 效果展示:
这里写图片描述

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是Objective-C纯代码实现签到界面的示例代码: ```objective-c #import "CheckInViewController.h" @interface CheckInViewController () @property (nonatomic, strong) UILabel *titleLabel; @property (nonatomic, strong) UIButton *checkInButton; @end @implementation CheckInViewController - (void)viewDidLoad { [super viewDidLoad]; // 设置背景颜色 self.view.backgroundColor = [UIColor whiteColor]; // 添加标题 self.titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 100, self.view.frame.size.width, 30)]; self.titleLabel.text = @"今日签到"; self.titleLabel.textAlignment = NSTextAlignmentCenter; [self.view addSubview:self.titleLabel]; // 添加签到按钮 self.checkInButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 150, 50)]; self.checkInButton.center = self.view.center; [self.checkInButton setTitle:@"签到" forState:UIControlStateNormal]; [self.checkInButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal]; [self.checkInButton setBackgroundColor:[UIColor blueColor]]; [self.checkInButton addTarget:self action:@selector(checkInButtonClicked) forControlEvents:UIControlEventTouchUpInside]; [self.view addSubview:self.checkInButton]; } - (void)checkInButtonClicked { // 处理签到逻辑 NSLog(@"签到成功!"); } @end ``` 这段代码中,我们创建了一个`CheckInViewController`类,其中包括`titleLabel`和`checkInButton`两个控件。在`viewDidLoad`方法中,我们设置了背景颜色,并添加了标题和签到按钮。当用户点签到按钮时,我们在`checkInButtonClicked`方法中处理签到逻辑,并在控制台输出签到成功的日志。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值