2013-05-11 96 views
0

我在實現一個鍵盤附件時遇到了困難,該附件在按下附件上的按鍵時將文本輸入到UITextField中。這是看起來草稿,如:帶有文本輸入到UITextField的自定義鍵盤

Keyboard Accessory Picture

最後我希望當在用戶點擊「@gmail按鈕,它在當前的電子郵件地址的UITextField @ gmail.com,所以用戶沒有按輸入」不得不輸入。我遇到了問題,因爲我正在以編程方式編寫keyboardaccessoryview,我無法弄清楚keyboardaccess的密鑰輸入是否到達電子郵件地址的UITextField。我曾嘗試做一個IBAction與

NSString *appendThisText = @"@gmail.com"; 
self.textFieldOne.text=[NSString stringWithFormat:@"%@%@", self.textFieldOne.text, appendThisText]; 

但這並沒有工作。我需要幫助。下面是我對signInViewController.h

#import <UIKit/UIKit.h> 
#import <AVFoundation/AVAudioPlayer.h> 
#import <AudioToolbox/AudioToolbox.h> 

@interface signInViewController : UIViewController <UITextFieldDelegate, UITextViewDelegate> { 
    AVAudioPlayer *sound; 
    UIButton *myButton; 
    UITextField *textFieldOne; 
    UIButton *gmailButton; 
    UIButton *meButton; 
    UIButton *yahooButton; 
    UIButton *outlookButton; 
    UIView *inputAccView; 
    UIButton *aolButton; 
} 


- (IBAction)playSwoosh:(id)sender; 

- (IBAction)dismiss:(id)sender; 

- (IBAction)toggleUIButtonImage:(id)sender; 

- (IBAction)backgroundTouched:(id)sender; 



@property (weak, nonatomic) IBOutlet UITextField *textFieldOne; 
@property (weak, nonatomic) IBOutlet UITextField *textFieldTwo; 
@property (nonatomic, retain) UIButton *gmailButton; 
@property (nonatomic, retain) UIButton *meButton; 
@property (nonatomic, retain) UIButton *yahooButton; 
@property (nonatomic, retain) UIButton *outlookButton; 
@property (nonatomic, retain) UIButton *aolButton; 
@property (retain, nonatomic) UIButton *myButton; 
@property (nonatomic, retain) UIView *inputAccView; 

@end 

代碼,這裏是我的signInViewController.m

#import "signInViewController.h" 
#import <QuartzCore/QuartzCore.h> 

@interface signInViewController() 

@end 

@implementation signInViewController 

@synthesize myButton = _myButton; 
@synthesize textFieldOne = _textFieldOne; 
@synthesize textFieldTwo = _textFieldTwo; 
@synthesize inputAccView; 
@synthesize gmailButton; 
@synthesize yahooButton; 
@synthesize meButton; 
@synthesize outlookButton; 
@synthesize aolButton; 

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil 
{ 
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; 
if (self) { 
} 
return self; 
} 


- (void)createInputAccessoryView { 
inputAccView = [[UIView alloc] initWithFrame:CGRectMake(10.0, 0.0, 310.0, 30.0)]; 
[inputAccView setBackgroundColor:[UIColor clearColor]]; 
[inputAccView setAlpha: 0.8]; 

gmailButton = [UIButton buttonWithType: UIButtonTypeRoundedRect]; 
[gmailButton setBackgroundImage:[UIImage imageNamed:@"keyboardtoolbar.png"] forState:UIControlStateNormal]; 
//[gmailButton sizeToFit] 
[gmailButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal]; 
gmailButton.titleLabel.font = [UIFont fontWithName:@"Heiti TC" size:12]; 
[gmailButton setFrame: CGRectMake(0.0, 0.0, 60.0, 30.0)]; 
[gmailButton setTitle:@"@Gmail" forState:UIControlStateNormal]; 
[gmailButton setBackgroundColor:[UIColor lightGrayColor]]; 
[inputAccView addSubview:gmailButton]; 

meButton = [UIButton buttonWithType: UIButtonTypeRoundedRect]; 
[meButton setBackgroundImage:[UIImage imageNamed:@"keyboardtoolbar.png"] forState:UIControlStateNormal]; 
//[gmailButton sizeToFit] 
[meButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal]; 
meButton.titleLabel.font = [UIFont fontWithName:@"Heiti TC" size:12]; 
[meButton setFrame: CGRectMake(60.0, 0.0, 50.0, 30.0)]; 
[meButton setTitle:@"@Me" forState:UIControlStateNormal]; 
[meButton setBackgroundColor:[UIColor lightGrayColor]]; 
[inputAccView addSubview:meButton]; 

yahooButton = [UIButton buttonWithType: UIButtonTypeRoundedRect]; 
[yahooButton setBackgroundImage:[UIImage imageNamed:@"keyboardtoolbar.png"] forState:UIControlStateNormal]; 
//[gmailButton sizeToFit] 
[yahooButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal]; 
yahooButton.titleLabel.font = [UIFont fontWithName:@"Heiti TC" size:12]; 
[yahooButton setFrame: CGRectMake(110.0, 0.0, 70.0, 30.0)]; 
[yahooButton setTitle:@"@Yahoo" forState:UIControlStateNormal]; 
[yahooButton setBackgroundColor:[UIColor lightGrayColor]]; 
[inputAccView addSubview:yahooButton]; 

outlookButton = [UIButton buttonWithType: UIButtonTypeRoundedRect]; 
[outlookButton setBackgroundImage:[UIImage imageNamed:@"keyboardtoolbar.png"] forState:UIControlStateNormal]; 
//[gmailButton sizeToFit] 
[outlookButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal]; 
outlookButton.titleLabel.font = [UIFont fontWithName:@"Heiti TC" size:12]; 
[outlookButton setFrame: CGRectMake(180.0, 0.0, 70.0, 30.0)]; 
[outlookButton setTitle:@"@Outlook" forState:UIControlStateNormal]; 
[outlookButton setBackgroundColor:[UIColor lightGrayColor]]; 
[inputAccView addSubview:outlookButton]; 

aolButton = [UIButton buttonWithType: UIButtonTypeRoundedRect]; 
[aolButton setBackgroundImage:[UIImage imageNamed:@"keyboardtoolbar.png"] forState:UIControlStateNormal]; 
//[gmailButton sizeToFit] 
[aolButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal]; 
aolButton.titleLabel.font = [UIFont fontWithName:@"Heiti TC" size:12]; 
[aolButton setFrame: CGRectMake(250.0, 0.0, 50.0, 30.0)]; 
[aolButton setTitle:@"@Aol" forState:UIControlStateNormal]; 
[aolButton setBackgroundColor:[UIColor lightGrayColor]]; 
[inputAccView addSubview:aolButton]; 

} 


- (IBAction)backgroundTouched:(id)sender { 
[_textFieldOne resignFirstResponder]; 
[_textFieldTwo resignFirstResponder]; 
} 


- (void)textFieldDidBeginEditing:(UITextField *)textField { 

if (textField == self.textFieldOne) { 
    [self createInputAccessoryView]; 
    [textField setInputAccessoryView:inputAccView]; 
    [UIView beginAnimations:nil context:NULL]; 
    [UIView setAnimationDelegate:self]; 
    [UIView setAnimationDuration:0.5]; 
    [UIView setAnimationBeginsFromCurrentState:YES]; 
    self.view.frame = CGRectMake(self.view.frame.origin.x, (self.view.frame.origin.y - 95), self.view.frame.size.width, self.view.frame.size.height); 
    [UIView commitAnimations]; 
} else if (textField == self.textFieldTwo) { 
    [UIView beginAnimations:nil context:NULL]; 
    [UIView setAnimationDelegate:self]; 
    [UIView setAnimationDuration:0.5]; 
    [UIView setAnimationBeginsFromCurrentState:YES]; 
    self.view.frame = CGRectMake(self.view.frame.origin.x, (self.view.frame.origin.y - 95), self.view.frame.size.width, self.view.frame.size.height); 
    [UIView commitAnimations]; 
} 
} 

- (void)textFieldDidEndEditing:(UITextField *)textField { 
if (textField == self.textFieldOne) { 
    [UIView beginAnimations:nil context:NULL]; 
    [UIView setAnimationDelegate:self]; 
    [UIView setAnimationDuration:0.5]; 
    [UIView setAnimationBeginsFromCurrentState:YES]; 
    self.view.frame = CGRectMake(self.view.frame.origin.x, (self.view.frame.origin.y + 95), self.view.frame.size.width, self.view.frame.size.height); 
    [UIView commitAnimations]; 
} else if (textField == self.textFieldTwo) { 
    [UIView beginAnimations:nil context:NULL]; 
    [UIView setAnimationDelegate:self]; 
    [UIView setAnimationDuration:0.5]; 
    [UIView setAnimationBeginsFromCurrentState:YES]; 
    self.view.frame = CGRectMake(self.view.frame.origin.x, (self.view.frame.origin.y + 95), self.view.frame.size.width, self.view.frame.size.height); 
    [UIView commitAnimations]; 
} 
} 


- (BOOL)textFieldShouldReturn:(UITextField *)textField { 
if (textField) { 
    [textField resignFirstResponder]; 
} 
return NO; 
} 

是否有人可以幫助我得到這一個正確的道路上。我認爲這將是一個從gmailButton到textFieldOne作爲插座的IBAction,但我不確定如何以編程方式實現該插件。這給我一些問題。我沒有任何問題翻譯成UITextView,但我不知道是否需要使用UITextInput協議或什麼。

回答

2

基本上你只需要添加一些目標動作給你的按鈕(如IBAction)。

只需添加到您的按鈕:

[yourButton addTarget:self action:@selector(yourButtonTapped) forControlEvents:UIControlEventTouchUpInside]; 

,並創建一個方法來處理自來水,如:

- (void)yourButtonTapped 
{ 
    self.textFieldOne.text = [self.textFieldOne.text stringByAppendingString:@"whatever"]; 
} 
+0

那太棒了!我不敢相信我忽略了這一點。我非常關注IBAction處理所有我完全錯過的事件。太棒了。謝謝! – soulshined 2013-05-11 13:16:51

+0

我會爲你投票,但我沒有足夠的聲望 – soulshined 2013-05-11 13:17:31

+0

不客氣:-) – d4Rk 2013-05-11 16:47:41

相關問題