ios学习第五天(五)UIButton的简单用法

本文介绍了一个简单的iOS应用示例,展示了如何通过点击按钮更新Label上的文本内容。通过设置按钮点击事件,实现了对Label文本的动态更新。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

这一篇是在上一篇代码的基础上对代码进行的修改,先看效果吧



左上角那个控件是一个按钮,当点击的时候,触发点击事件,对中间的label上的文字进行修改。下面是部分代码:

//
//  ViewController.m
//  ViewTest
//
//  Created by Moluth on 17/4/11.
//  Copyright (c) 2017年 Moluth. All rights reserved.
//

#import "ViewController.h"

@interface ViewController (){
    int count;
}

@end

@implementation ViewController


- (void)viewDidLoad {
    [super viewDidLoad];
    
    UILabel *label=[[UILabel alloc] initWithFrame:CGRectMake(0, 0, 250, 60)];//创建一个UILabel
    [self.view addSubview:label];//添加label为子view
    
    
    label.backgroundColor=[UIColor redColor];
    label.text=@"召唤效果";
    label.textColor=[UIColor whiteColor];//设置文字颜色为白色
    label.textAlignment=NSTextAlignmentCenter;//居中显示
    label.font=[UIFont systemFontOfSize:40 weight:60];//设置字体
    
    label.shadowColor=[[UIColor alloc] initWithRed:0.7 green:0.7 blue:0.7 alpha:0.7];//设置阴影灰色
    label.shadowOffset=CGSizeMake(2, 2);//设置阴影偏移值
    
    
    //设置圆角
    label.layer.cornerRadius=8;
    label.layer.masksToBounds=YES;
    
    //设置位置
    label.center=self.view.center;
    //设置标签
    label.tag=123;
    
    UIButton *button=[UIButton buttonWithType:UIButtonTypeCustom];//创建button
    button.frame=CGRectMake(0, 0, 100, 40);//设置按钮位置和大小
    [self.view addSubview:button];//添加button
    [button setTitle:@"按钮" forState:UIControlStateNormal];//设置标题
    [button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];//设置标题颜色
    [button setBackgroundColor:[UIColor orangeColor]];//设置背景颜色
    //设置圆角
    button.layer.cornerRadius=8;
    button.layer.masksToBounds=YES;
    //设置点击事件UIControlEventTouchUpInside,处理该事件的对象是当前对象,使用click方法进行处理,于是在下面定义了click方法
    [button addTarget:self action:@selector(click) forControlEvents:UIControlEventTouchUpInside];
    
    
}
-(void)click{
    //之前给label设置了一个标签123,这里根据这个标签获取label对象,并对其进行修改
    UILabel *label=[self.view viewWithTag:123];
    label.text=[[NSString alloc] initWithFormat:@"点击%d",count++ ];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值