单行文本框和多行文本框UiTextfield UiTextView

这篇博客介绍了如何在iOS应用中设置单行文本框(UITextField)和多行文本框(UITextView),包括文字设置、对齐方式、占位符、键盘类型以及响应事件的添加。通过示例代码展示了如何创建按钮,并在用户点击按钮时进行文本验证和计算,同时展示了使用UIAlertView显示警告对话框。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

按钮设置标题: [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;

    

}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值