我试图弄清楚如何通过单击按钮在我的 iphone 应用程序的视图中添加文本字段。可能吗?
我想应该就这么简单。但它不起作用。
- (IBAction)addText:(id)sender
CGRect textField = CGRectMake(0.0, 0.0, 100.0, 30.0);
UITextField *textField = [[UITextField alloc] initWithFrame:textField];
我试图弄清楚如何通过单击按钮在我的 iphone 应用程序的视图中添加文本字段。可能吗?
我想应该就这么简单。但它不起作用。
- (IBAction)addText:(id)sender
CGRect textField = CGRectMake(0.0, 0.0, 100.0, 30.0);
UITextField *textField = [[UITextField alloc] initWithFrame:textField];
如果此代码不起作用,则意味着您的 UIButton 的 IBAction 不起作用将 NSLog 放入 IBAction 或编写 UITextView 全局变量,因为它可能是超级调用 dealloc 您的 UITextView。或者,您视野之外的 CGRectMake(0.0, 0.0, 100.0, 30.0) 框架可能超过了最大高度或最大高度,而您看不到它。
- (IBAction)addText:(id)sender {
CGRect textFieldFrame = CGRectMake(0.0, 0.0, 100.0, 30.0);
UITextField *textField = [[UITextField alloc] initWithFrame:textFieldFrame];
textField.delegate = self;
[self.view addSubview:textField];
// or add to you wanted view
[yourView addSubview:textField];
}
Hi John yes you can create uitextfield after clicking the button for that you have to write code as such below
- (IBAction)addText:(id)sender
{
CGRect textfieldFrame = CGRectMake(0.0, 0.0, 100.0, 30.0);
UITextField *textField = [[UITextField alloc] initWithFrame:textfieldFrame];
textField.borderStyle = UITextBorderStyleRoundedRect;
textField.font = [UIFont systemFontOfSize:15];
textField.placeholder = @"enter text";
textField.autocorrectionType = UITextAutocorrectionTypeNo;
textField.keyboardType = UIKeyboardTypeDefault;
textField.returnKeyType = UIReturnKeyDone;
textField.clearButtonMode = UITextFieldViewModeWhileEditing;
textField.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
textField.delegate = self;
[self.view addSubview:textField];
}