【UITextField与UITextView】
2015-08-12 23:22
597 查看
//创建文本框对象及大小 只能是单行的 (已在AppDelegate.h中对t提升权限UITextField *t)
t=[[UITextField alloc]initWithFrame:CGRectMake(85, 70, 170,40)];
t.backgroundColor=[UIColor greenColor];
//设置文字方向
t.textAlignment=NSTextAlignmentCenter;
//设置键盘的类型
t.keyboardType=UIKeyboardTypeNumberPad;
//设定某一键的类型
t.returnKeyType=UIReturnKeyDone;
UIButton *btn=[[UIButton alloc]initWithFrame:CGRectMake(85, 200, 170, 40)];
btn.backgroundColor=[UIColor blueColor];
[btn setTitle:@"相加" forState:UIControlStateNormal];
[btn addTarget:self action:@selector(sumTap:) forControlEvents:UIControlEventTouchUpInside];
//创建文本视图 与UITextField的区别是可多行可换行
UITextView *many=[[UITextView alloc]initWithFrame:CGRectMake(85, 120, 170, 60)];
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:nil, nil];
//显示警报
[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];
}
t=[[UITextField alloc]initWithFrame:CGRectMake(85, 70, 170,40)];
t.backgroundColor=[UIColor greenColor];
//设置文字方向
t.textAlignment=NSTextAlignmentCenter;
//设置键盘的类型
t.keyboardType=UIKeyboardTypeNumberPad;
//设定某一键的类型
t.returnKeyType=UIReturnKeyDone;
UIButton *btn=[[UIButton alloc]initWithFrame:CGRectMake(85, 200, 170, 40)];
btn.backgroundColor=[UIColor blueColor];
[btn setTitle:@"相加" forState:UIControlStateNormal];
[btn addTarget:self action:@selector(sumTap:) forControlEvents:UIControlEventTouchUpInside];
//创建文本视图 与UITextField的区别是可多行可换行
UITextView *many=[[UITextView alloc]initWithFrame:CGRectMake(85, 120, 170, 60)];
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:nil, nil];
//显示警报
[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];
}
相关文章推荐
- 【UISegmentedControl】
- HDU 3397 Sequence operation(区间合并 + 区间更新)
- Request Parameters and Header Values
- [LeetCode]Permutation Sequence
- OC-UI部分,简单的登陆界面简单代码
- Domino Angular 前端UI开发
- arm交叉编译器gnueabi、none-eabi、arm-eabi、gnueabihf、gnueabi区别
- DCIM 用 UIScrollView 和UIPageControl
- js实现EasyUI-datagrid前台分页
- Leetcode63 Unique Paths II
- UIPageControl
- UI 06 _ 7种手势
- 消息队列(message queue)产生的背景、常用的技术
- hdu 1297 Children’s Queue(递推和高精)
- IOS--UI--NSThread 和GCD 总结
- Design Pattern -- Builder
- UI05 _ 分段控件 -- UISegmentedControl
- UIImageView+GestureRecognizer
- prim Building a Space Station
- iOS开发-UIScreenEdgePanGestureRecognizer实战