2013-01-18 161 views
15

當我在文本框內單擊時,如何將正按鈕添加到UITextField,如下面的屏幕所示?點擊此按鈕後,它將啓動iPhone聯繫人應用程序。我非常感謝任何代碼示例。由於將按鈕添加到uitextfield

enter image description here

+3

爲什麼向下票呢? –

回答

8

使用以下方法添加UIButton

[self.txtField addTarget:self action:@selector(textFieldDidChange:) forControlEvents:UIControlEventEditingChanged]; 

- (BOOL) textFieldDidChange:(UITextField *)textField 
{ 
    UIButton *btnColor = [UIButton buttonWithType:UIButtonTypeCustom]; 
    [btnColor addTarget:self action:@selector(btnColorPressed:) forControlEvents:UIControlEventTouchUpInside]; 
    btnColor.frame = CGRectMake(self.txtTag.bounds.size.width - 50, 5, 25, 25); 
    [btnColor setBackgroundImage:[UIImage imageNamed:@"PaintPickerButton.png"] forState:UIControlStateNormal]; 
    [self.txtTag addSubview:btnColor]; 

    return YES; 
} 

或寫

-(BOOL) textFieldShouldBeginEditing:(UITextField *)textField 
{ 
    [UIView beginAnimations:nil context:NULL]; 
    [UIView setAnimationBeginsFromCurrentState:YES]; 
    [UIView setAnimationDuration:0.3f]; 
    // self.scrollView.frame = CGRectMake(0, 35, self.view.bounds.size.width, self.view.bounds.size.height - 250); 
    [UIView commitAnimations]; 

    UIButton *btnColor = [UIButton buttonWithType:UIButtonTypeCustom]; 
    [btnColor addTarget:self action:@selector(btnColorPressed:) forControlEvents:UIControlEventTouchUpInside]; 
    btnColor.frame = CGRectMake(150, 5, 25, 25); 
    [btnColor setBackgroundImage:[UIImage imageNamed:@"PaintPickerButton.png"] forState:UIControlStateNormal]; 
    [self.txtField addSubview:btnColor]; 

    return YES; 
} 
+0

爲什麼使用self.scrollView.frame = CGRectMake(0,35,self.view.bounds.size.width,self.view.bounds.size.height - 250); – pengwang

+0

@ pengwang-sory我從我的項目複製代碼,所以這行我foragt刪除:) – iPatel

+4

手動添加自己的UIButton是不可接受的,當文本字段變爲活動時,按鈕將被髮送到後面,而不是可點擊!最好的解決方案是使用'rightView'屬性 –

8

下面的代碼使用中的UITextField添加按鈕。

-(void)ViewDidLoad{ 
    UITextField *txt = [[UITextField alloc]initWithFrame:CGRectMake(140, 120, 160, 40)]; 
    txt.returnKeyType = UIReturnKeyDone; 
    txt.placeholder = @"Enter or Mobile Number"; 
    [txt setBorderStyle:UITextBorderStyleRoundedRect]; 
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom]; 
    [button setImage:[UIImage imageNamed:@"crossImg.png"] forState:UIControlStateNormal]; 
    button.imageEdgeInsets = UIEdgeInsetsMake(0, -16, 0, 0); 
    [button addTarget:self action:@selector(clearText:) forControlEvents:UIControlEventTouchUpInside]; 
    txt.rightView = button; 
    txt.rightViewMode = UITextFieldViewModeAlways; 
    [self.view addSubview:txt]; 
} 

-(IBAction)clearText:(id)sender{ 

} 
3

使用UITextFieldrightView財產。 在textFieldDidBeginEditing:代表創建按鈕與您想要的動作&設置爲rightView。 並在textFieldDidEndEditing:設置爲零rightView

1

你也可以去自定義視圖螞蟻然後添加到它的子視圖。然後你隱藏它。

- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField 
{ 
    if(textField == yourFirstTextField) 
    { 
     UIButton *addButton = [UIButton buttonWithType:UIButtonTypeCustom]; 
     [addButton setImage:[UIImage imageNamed:@"addImage.png"] forState:UIControlStateNormal]; 
     [addButton addTarget:self action:@selector(yourActionHere) forControlEvents:UIControlEventTouchUpInside]; 
     textField.rightViewMode = UITextFieldViewModeWhileEditing; 
     textField.rightView = addButton; 
    } 
} 

你可以,如果你在IB添加字段使用上面的方法:只有當用戶點擊它

9

您可以使用下面的代碼這一點,將是可見的。

如果通過代碼創建它,你需要添加下面的代碼創建時:

UIButton *addButton = [UIButton buttonWithType:UIButtonTypeCustom]; 
[addButton setImage:[UIImage imageNamed:@"addImage.png"] forState:UIControlStateNormal]; 
[addButton addTarget:self action:@selector(yourActionHere) forControlEvents:UIControlEventTouchUpInside]; 
textField.rightViewMode = UITextFieldViewModeWhileEditing; 
textField.rightView = addButton; 
0
self.locationName.clearButtonMode = UITextFieldViewModeWhileEditing; 
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom]; 
    button.frame = CGRectMake(0, 0, 30, 30); 
    [button setImage:[UIImage imageNamed:@"goButtonIcon.png"] forState:UIControlStateNormal]; 
    [button addTarget:self action:@selector(goButtonClicked:) forControlEvents:UIControlEventTouchUpInside]; 

    self.locationName.rightView = button; 
    self.locationName.rightViewMode = UITextFieldViewModeUnlessEditing; 

,然後調用這個方法:

-(IBAction)goButtonClicked:(id)sender{ 

} 

self.locationName ------->的UITextField引用(在viewDidLoad中你可以這樣寫代碼)