ThinkChat2.0新版上线,更智能更精彩,支持会话、画图、阅读、搜索等,送10W Token,即刻开启你的AI之旅 广告
~~~ #import "ViewController.h" @interface ViewController () @end @implementation ViewController UITextField *textFiled; - (void)viewDidLoad { [super viewDidLoad]; [self addTextField]; // Do any additional setup after loading the view, typically from a nib. } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } -(void) addTextField { UILabel *prefixLabel = [[UILabel alloc]initWithFrame:CGRectZero]; prefixLabel.text=@"输入用户名:"; [prefixLabel setFont:[UIFont boldSystemFontOfSize:14]]; [prefixLabel sizeToFit]; textFiled = [[UITextField alloc] initWithFrame:CGRectMake(20, 50, 280, 30)]; textFiled.borderStyle = UITextBorderStyleRoundedRect; textFiled.contentVerticalAlignment=UIControlContentVerticalAlignmentCenter; [textFiled setFont:[UIFont boldSystemFontOfSize:12]]; textFiled.placeholder = @"Simple Text filed"; [textFiled setTextColor:[UIColor blueColor]]; textFiled.leftView = prefixLabel; textFiled.leftViewMode = UITextFieldViewModeAlways; UIButton *btn = [UIButton buttonWithType:UIButtonTypeRoundedRect]; [btn setTitle:@"提交" forState:UIControlStateNormal]; [btn sizeToFit]; textFiled.rightView=btn; textFiled.rightViewMode = UITextFieldViewModeAlways; [btn addTarget:self action:@selector(onClick) forControlEvents:UIControlEventTouchUpInside] ; [self.view addSubview:textFiled]; } -(void)onClick { NSLog(@"获取输入框值:%@",textFiled.text); } @end ~~~