2013-01-18 119 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中你可以这样写代码)