2013-01-18 14 views
15

Metin kutusuna tıklandığında, aşağıdaki ekranda gösterildiği gibi UITextField'a nasıl artı düğmesi ekleyebilirim? Bu düğmeye tıklandığında iPhone iletişim uygulamasını başlatırdı. Herhangi bir kod örneğini çok takdir ediyorum. Teşekküruitextfield'a ekle düğmesi

enter image description here

+3

neden aşağı oy? –

cevap

8

kullanın aşağıdaki yöntemi 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; 
} 

eklemek Veya yaz için

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

neden self.scrollView.frame = CGRectMake kullanıyor (0, 35, self.view.bounds.size.width, self.view.bounds.size.height - 250); – pengwang

+0

@ pengwang- sory Projeden kod kopyalayın, böylece bu satırı kaldırmak istiyorum :) – iPatel

+4

Kendi UIButton'unuzu manuel olarak eklemek kabul edilemez, metin alanı aktif hale geldiğinde düğme arkaya gönderilir ve geri alınamaz! En iyi çözüm 'rightView' özelliğini kullanmaktır –

8

UITextField düğmeyi eklemek için kod kullanımını takiben.

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

Kullanım UITextField ait rightView özelliği. & öğesinin rightView olarak ayarlanmasını istediğiniz eylülle eylemi oluştur düğmesiyle textFieldDidBeginEditing: delege oluşturun.Ve textFieldDidEndEditing:'da rightView

1

'a ayarlayın, özel görünüm karması için gidip alt görünümü ekleyin. o zaman gizlersin. Eğer IB alanını eklediyseniz

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

Yukarıdaki yöntemi kullanabilirsiniz: kullanıcı Bunun için aşağıdaki kodu kullanabilirsiniz o

9

musluklar sadece zaman görünür olacaktır. kod aracılığıyla oluşturulursa oluştururken

, aşağıdaki kodu eklemek gerekir:

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; 

ve sonra bu yöntemi çağırın:

-(IBAction)goButtonClicked:(id)sender{ 

} 

self.locationName -------> UITextField Başvuru (viewDidLoad yazabilir bu kodu)