2013-06-26 27 views
1

我創建了uitextfield作爲uiimageview的子視圖& textfield和uiimageview都是scrollview的子視圖。在編輯文本框時隱藏鍵盤的方法是什麼。 在這裏我的代碼,它不工作。如何解除ios中的自定義文本字段的鍵盤

{ 
    img1=[[UIImageView alloc]initWithFrame:CGRectMake(0, 1, 320, 60)]; 
    img1.userInteractionEnabled=YES; 
    NSString *imgfilepath=[[NSBundle mainBundle]pathForResource:@"123" ofType:@"png"]; 
    UIImage *imo=[[UIImage alloc]initWithContentsOfFile:imgfilepath]; 
    [img1 setImage:imo]; 

    UILabel *label1; 
    label1=[[UILabel alloc]init]; 
    label1.frame=CGRectMake(60, 0, 250, 30); 
    [email protected]"ENTER ANNUAL INCOME"; 
    label1.textColor=[UIColor blackColor]; 
    label1.font=[UIFont italicSystemFontOfSize:16.0f]; 

    label1.backgroundColor=[UIColor clearColor]; 
    [img1 addSubview:label1]; 


    principal = [[UITextField alloc] initWithFrame:CGRectMake(85,30, 156, 40)]; 
    principal.backgroundColor = [UIColor clearColor]; 
    principal.clearButtonMode = UITextFieldViewModeWhileEditing; 
    principal.font = [UIFont systemFontOfSize:15.0f]; 
    [email protected]"ENTER"; 
    [principal setKeyboardType:UIKeyboardTypeNumberPad]; 
    //principal.textAlignment=UITextAlignmentCenter; 

    [img1 addSubview:principal]; 

    [img1 respondsToSelector:[principal resignFirstResponder]]; 

    [scrollview addSubview:img1]; 
} 

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event 
{ 

    [principal resignFirstResponder]; 

} 
+0

這些方法在哪些類中? –

回答

1

[img1 respondsToSelector:[principal resignFirstResponder]];

首先,respondsToSelector返回你用它來確定是否可以調用一個選擇一個BOOL。這不是void方法,看來你誤解了這一點。

二,UIImageView不響應resignFirstResponder

三,你是「傳入」voidrespondsToSelector:,我很驚訝編譯?

最終你想要做的就是撥打resignFirstResponder你的UITextField這可以通過調用[principal resignFirstResponder]來實現。

但是,對消除鍵盤是通過調用其描述做[self.view endEditing:YES]最簡單的方法之一...

使視圖(或嵌入文本字段中的一個)辭職第一響應狀態。