【UITextField与UITextView】

//创建文本框对象及大小 只能是单行的 (已在AppDelegate.h中对t提升权限UITextField *t)

    t=[[UITextField alloc]initWithFrame:CGRectMake(8570170,40)];

    t.backgroundColor=[UIColor greenColor];

    //设置文字方向

    t.textAlignment=NSTextAlignmentCenter;

    //设置键盘的类型

    t.keyboardType=UIKeyboardTypeNumberPad;

    //设定某一键的类型

    t.returnKeyType=UIReturnKeyDone;

    

    UIButton *btn=[[UIButton alloc]initWithFrame:CGRectMake(8520017040)];

    btn.backgroundColor=[UIColor blueColor];

    [btn setTitle:@"相加" forState:UIControlStateNormal];

    [btn addTarget:self action:@selector(sumTap:) forControlEvents:UIControlEventTouchUpInside];

    

    //创建文本视图 UITextField的区别是可多行可换行

    UITextView *many=[[UITextView alloc]initWithFrame:CGRectMake(8512017060)];

    many.backgroundColor=[UIColor redColor];

    many.text=@"1234\n5678\n9";

    

    [self.window makeKeyAndVisible];

    [self.window addSubview:t];

    [self.window addSubview:btn];

    [self.window addSubview:many];

    

    return YES;

}

//添加自加算法事件

-(void)sumTap:(UIButton *)sender

{

    //得到文本框的内容,内容是字符串

    NSString *strNumber=t.text;

    //数据的有效性验证,如果为空

    if(strNumber.length==0)

    {    //警报 消息对话框

        UIAlertView *viw=[[UIAlertView alloc]initWithTitle:@"友情提醒" message:@"输入信息不能为空" delegate:nil cancelButtonTitle:@"取消"otherButtonTitles:nilnil];

        //显示警报

        [viw show];

        return;

    }

    //把字符串转换为数值

    int num=[strNumber intValue];

    //求和,用循环产生从1到文本框里的数

    int s=0;

    for(int i=1;i<=num;i++)

    {

        s+=i;//自累加

    }

    //让文本框显示总和

    t.text=[NSString stringWithFormat:@"%d",s];

    

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值