如何通过按下按钮添加文本字段

时间:2013-04-15 03:29:40

标签: ios button uiview uitextfield add

我正在尝试通过点击按钮找出如何在我的iphone应用中添加文本字段。有可能吗?

我认为它应该像这样容易。但它不起作用。

- (IBAction)addText:(id)sender

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

2 个答案:

答案 0 :(得分:0)

如果此代码不起作用,则表示您的UIButton的IBAction无法正常工作将NSLog放入IBAction或编写UITextView全局变量,因为可能是超级调用de​​alloc您的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];
}

答案 1 :(得分:0)

嗨John是的,你可以点击按钮后创建uitextfield,你必须编写代码,如下所示

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



}