按钮设置标题: [but setTitle:@"确定" forState:UIControlStateNormal];
文本框等其他设置标题都是@“” te.text=@"你看见我了?"
按钮中增加事件: [but addTarget:self action:@selector(ok:) forControlEvents:UIControlEventTouchUpInside];
#import <UIKit/UIKit.h>
警报: UIAlertView
@interface AppDelegate : UIResponder <UIApplicationDelegate>
{
UITextView *tv ;
UITextField * te;
NSString *str;
}
@property (strong,nonatomic) UIWindow *window;
@end
.m文件:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
self.window = [[UIWindowalloc]initWithFrame:[[UIScreenmainScreen] bounds]];
self.window.backgroundColor=[UIColorwhiteColor];
//初始化单行文本UIText
te = [[UITextFieldalloc]initWithFrame:CGRectMake(80,50,160,50)];
te.backgroundColor=[UIColorgreenColor];
te.textColor = [UIColorblackColor];
//te.text=@"你看见我了?";
//右对齐
//te.textAlignment=NSTextAlignmentRight;
//中心对齐
te.textAlignment=NSTextAlignmentCenter ;
//占位符placeholder
te.placeholder =@"请输入数字";
//te.textColor=[UIColor whiteColor];
// te.secureTextEntry=YES;
//键盘模式为数字模式
te.keyboardType=UIKeyboardTypeNumberPad;
//多行文本框TextView
// tv = [[UITextView alloc]initWithFrame:CGRectMake(30, 100, 200, 100)];
// tv.text=@"123\n555\nrr";
// tv.backgroundColor= [UIColor redColor];
// [self.window addSubview:tv];
//按钮的创建
UIButton * but = [[UIButtonalloc]initWithFrame:CGRectMake(80,120 , 160, 50 )];
//按钮的标题
[but setTitle:@"确定"forState:UIControlStateNormal];
//按钮背景色
but.backgroundColor=[UIColorblackColor];
//按钮上的字体颜色
[but setTitleColor:[UIColorgreenColor] forState:UIControlStateNormal];
//增加事件
[but addTarget:selfaction:@selector(ok:)forControlEvents:UIControlEventTouchUpInside];
[self.windowaddSubview:but];
//设定文本框输入的键盘
//te.keyboardAppearance=UIKeyboardTypeEmailAddress;
[self.windowaddSubview:te];
[self.windowmakeKeyAndVisible];
// Override point for customization after application launch.
returnYES;
}
-(void)ok:(id)sender
{
//te.text是获取用户输入的字符串,为了计算,需要转化为数字
int e =[te.textintegerValue];
if (te.text.length==0) {
//警报
UIAlertView * al = [[UIAlertViewalloc]initWithTitle:@"友情提示"message:@"不能为空"delegate:nilcancelButtonTitle:@"我知道了"otherButtonTitles:nil,nil];
[al show];
return;
}
NSLog(@"文本内容:%@",te.text);
int sum =0 ;
for (int i =1; i<=e; i++) {
sum = i+sum;
}
NSLog(@"sum is %d",sum);
str = [NSStringstringWithFormat:@"%d",sum];
te.text =str;
}