2015-06-16 35 views
0

這裏是我的代碼,UITextField是在UIButton上單擊時以編程方式創建的。但是爲了增強,我想在創建UITextField時將焦點(並顯示鍵盤)設置爲新的UITextField。設置焦點並在xcode中以編程方式顯示鍵盤?

UITextField * txtComp=[[UITextField alloc]initWithFrame:CGRectMake(20,y, 450, 50)]; 
txtComp.borderStyle=UITextBorderStyleNone; 
txtComp.delegate = self; 
txtComp.tag=i+1; 
txtComp.section=index; 
txtComp.placeholder = @"New UITextField"; 
[txtComp becomeFirstResponder]; 

以上的編碼工作,創建新的UITextField,但不能將焦點設置新的UITextField即使becomeFirstResponder設置的。

+0

我檢查你的代碼的工作在這裏很好,我覺得在txtcom.section =指標線的一些問題只是將其刪除,並檢查它 –

+0

txtcom。 section = index用於分組,因此無法刪除。在我身邊,爲什麼鍵盤不能出現。 – ppshein

+0

我已經使用你的代碼只是刪除該部分行,所以它會完美工作後點擊按鈕 –

回答

0
-(IBAction)buttonClicked:(id)sender 

{

UITextField * txtComp=[[UITextField alloc]initWithFrame:CGRectMake(20,20, 450, 50)]; 
txtComp.borderStyle=UITextBorderStyleRoundedRect; 
txtComp.delegate = self; 


txtComp.placeholder = @"New UITextField"; 
[txtComp becomeFirstResponder]; 
[self.view addSubview:txtComp]; 
[self.view bringSubviewToFront:txtComp]; 

}

+0

它在我身邊不起作用。 – ppshein

+0

僅在單擊按鈕後顯示文本字段。你點擊按鈕? –

+0

yap,textfield正在顯示,但光標未聚焦在其框和鍵盤未顯示。 – ppshein

相關問題