15

テキスト ボックス内をクリックすると、次の画面に示すように UITextField にプラス ボタンを追加するにはどうすればよいですか? このボタンをクリックすると、iPhone 連絡先アプリケーションが起動します。コード例をいただければ幸いです。ありがとう

ここに画像の説明を入力

4

6 に答える 6

9

これには、次のコードを使用できます。

- (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にフィールドを追加した場合は、上記の方法を使用できます。

コードで作成した場合は、作成時に以下のコードを追加する必要があります。

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;
于 2013-01-18T05:22:16.480 に答える
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;
}
于 2013-01-18T05:25:15.507 に答える
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{

}
于 2013-01-18T05:18:14.477 に答える
3

rightViewのプロパティを使用しますUITextFieldtextFieldDidBeginEditing:委任作成ボタンで、必要なアクションを に設定しますrightView。そしてtextFieldDidEndEditing:セットnilにrightView

于 2013-01-18T05:18:32.030 に答える
1

カスタムビューとサブビューを追加することもできます。それからあなたはそれを隠します。ユーザーがタップしたときにのみ表示されます

于 2013-01-18T05:21:59.720 に答える
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 ではこのコードを記述できます)

于 2016-07-21T05:07:55.823 に答える