二十六天( UI开始)

button,textfield,label

主要代码

#import "ViewController.h"

@interface ViewController ()<UITextFieldDelegate>

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    CGSize size=[UIScreen mainScreen].bounds.size;//获取当前设备的屏幕设计分辨率
    NSLog(@"%f %f",size.width,size.height);
    // Do any additional setup after loading the view, typically from a nib.
    

    textfieldfirst=[[UITextField alloc]initWithFrame:CGRectMake(0*size.width/5+5, 200, size.width/5, 40)];//初始化控件
   // textfieldfirst.backgroundColor=[UIColor redColor];
    
    textfieldsecond=[[UITextField alloc]initWithFrame:CGRectMake(2*size.width/5+5,200, size.width/5, 40)];
   // textfieldsecond.backgroundColor=[UIColor redColor];
    
    textfieldfirst.clearButtonMode=UITextFieldViewModeAlways;// 添加清除按钮
    textfieldsecond.clearButtonMode=UITextFieldViewModeAlways;
    textfieldfirst.keyboardType=UIKeyboardTypeNumbersAndPunctuation;
    textfieldsecond.keyboardType=UIKeyboardTypeNumbersAndPunctuation;//设置键盘类型
    [textfieldfirst setBorderStyle:UITextBorderStyleLine];
    [textfieldsecond setBorderStyle:UITextBorderStyleLine];//添加线框
    [textfieldfirst setFont:[UIFont systemFontOfSize:24]];//设置字体大小
    textfieldfirst.delegate=self;
    textfieldsecond.delegate=self;
    
    [self.view addSubview:textfieldfirst];//添加到视图
    [self.view addSubview:textfieldsecond];
    
    label=[[UILabel alloc]initWithFrame:CGRectMake(1*size.width/5+5, 200, size.width/5, 30)];
    label.text=@"+";
    [label setTextAlignment:NSTextAlignmentCenter];//设置居中
    [self.view addSubview:label];
    
    UIButton *btn1=[[UIButton alloc]initWithFrame:CGRectMake(3*size.width/5+5,200, size.width/5, 40)];
   // btn1.backgroundColor=[UIColor redColor];
    [btn1 setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];// 设置字体颜色
    [btn1 setTitle:@"=" forState:UIControlStateNormal];
    [btn1 setTitleColor:[UIColor redColor] forState:UIControlStateHighlighted];
    //添加事件
    [btn1 addTarget:self action:@selector(resultoutput:) forControlEvents:UIControlEventTouchDown];
    
    [self.view addSubview:btn1];
    
    resultlabel=[[UILabel alloc]initWithFrame:CGRectMake(4*size.width/5+5, 200, size.width/5, 40)];

    [self.view addSubview:resultlabel];
    
    

    
}
-(void)resultoutput:(id)sender{
    resultlabel.text=@"0";
    resultlabel.text=[NSString stringWithFormat:@"%d",[textfieldfirst.text intValue]+[textfieldsecond.text intValue]];
}
//键盘退出
-(BOOL)textFieldShouldReturn:(UITextField *)textField{
    [textField resignFirstResponder];
    return  YES;
}




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值