我想弄清楚如何通過單擊按鈕在我的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];
約翰你好是你可以點擊按鈕,你必須編寫代碼,這樣下面
- (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];
}
這看起來應該可以工作,但事實並非如此。除此之外,還有更多的東西嗎? –
嗨約翰我編輯我的答案請使用此代碼它將工作defenetly。 – Naresh
謝謝!謹慎解釋問題是什麼,所以我可以從中學到一些東西? :) –
如果此代碼不能正常工作,意味着你的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];
}
不工作,有什麼我應該做的,讓這個工作? –
看到我編輯的答案 –
發表您的工作之後創建的UITextField ... –
當然有可能!讓視圖來來去去是iOS的精髓。你有沒有嘗試過任何東西? – matt
我改變了我的問題,併發布了我的嘗試 –