2013-01-18 24 views
15

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

enter image description here

+3

なぜダウン投票

-(IBAction)goButtonClicked:(id)sender{ } 

self.locationNameを? –

答えて

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

@ pengwangsory私のプロジェクトからコードをコピーするので、この行を削除します。 – iPatel

+4

手動で独自のUIButtonを追加することはできません。テキストフィールドがアクティブになると、ボタンは背面に送信され、タップ可能になりません!最適な解決法は 'rightView'プロパティを使うことです –

8

-(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

UITextFieldのプロパティを使用してください。 textFieldDidBeginEditing:では、&をrightViewに設定したいアクションでボタンを作成します。 textFieldDidEndEditing:には、rightView

1

に設定することもできます。また、カスタム表示のantを表示してサブビューを追加することもできます。あなたはそれを隠す。あなたはIB内のフィールドを追加した場合は、上記の方法を使用することができます

- (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; 
    } 
} 

:ユーザーが必要ですこのために以下のコードを使用することができ、それを

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; 
その後、

と、このメソッドを呼び出す:-------> UITextFieldのリファレンス(のviewDidLoadに、あなたがこのコードを書くことができます)

関連する問題