0

我试图弄清楚如何通过单击按钮在我的 iphone 应用程序的视图中添加文本字段。可能吗?

我想应该就这么简单。但它不起作用。

- (IBAction)addText:(id)sender

CGRect textField = CGRectMake(0.0, 0.0, 100.0, 30.0);
UITextField *textField = [[UITextField alloc] initWithFrame:textField];
4

2 回答 2

0

如果此代码不起作用,则意味着您的 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];
}
于 2013-04-15T03:38:13.757 回答
0

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];



}
于 2013-04-15T03:42:05.547 回答