// Created by mac on 2021/11/12.
//
#import "ViewController.h"
@interface ViewController ()
{
UILabel*lb;
int i;
}
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
self.view.backgroundColor=[UIColor orangeColor]; //背景颜色;
lb=[[UILabel alloc]init]; //初始化label ,label要全局,是一个显示字符串的控件;
lb.backgroundColor=[UIColor greenColor]; //label颜色;
lb.frame=CGRectMake(25, 80, 200, 50);//label位置;
lb.text=@"kaishi";//显示的字符;
lb.textColor=[UIColor blueColor];//显示的字符的颜色;
[self.view addSubview:lb];//添加到自身;
UIButton*btn=[UIButton buttonWithType:UIButtonTypeSystem];//创建一个button;
btn.frame=CGRectMake(50, 200, 70, 70);//btn位置;
btn.backgroundColor=[UIColor redColor];//btn颜色;
[btn setTitle:@"按钮" forState:0];//btn上的字符;
[btn addTarget:self action:@selector(btnfangfa) forControlEvents:UIControlEventTouchUpInside];//点击btn调用方法;
[self.view addSubview:btn];
i=0;//设置初始值为0,系统默认0;
}
-(void)btnfangfa{
i++;//全局的int类型的变量i,做自加运算;
switch (i)//根据i的值去循化,i是几就执行情况几;
{
case 1:
lb.text=@"石头";
break;//固定格式;
case 2:
lb.text=@"剪刀";
break;
case 3:
lb.text=@"布";
i=0;//执行完情况3,让i的值变为0,再次执行就是i+1=1,继续执行情况1,以此循化;
break;
default:
break;
}
}
@end
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)