我用UIButtons制作键盘。在这个键盘上有49个UIButtons,它们创建了以下内容:
- (void)viewDidLoad {
[self emoticonButtonOnTheKeyBoard_xCoordinate:0 yCoordinate:0 Emoticone_name:@"emoticon_angel.gif" backString:@"O:-)" keyboardView:self.view];
[self emoticonButtonOnTheKeyBoard_xCoordinate:40 yCoordinate:0 Emoticone_name:@"emoticon_angry.gif" backString:@":-@" keyboardView:self.view];
[self emoticonButtonOnTheKeyBoard_xCoordinate:80 yCoordinate:0 Emoticone_name:@"emoticon_big_smile.gif" backString:@":-D" keyboardView:self.view];
[self emoticonButtonOnTheKeyBoard_xCoordinate:120 yCoordinate:0 Emoticone_name:@"emoticon_confused.gif" backString:@":-s" keyboardView:self.view];
[self emoticonButtonOnTheKeyBoard_xCoordinate:160 yCoordinate:0 Emoticone_name:@"emoticon_cool.gif" backString:@"B)" keyboardView:self.view];
[self emoticonButtonOnTheKeyBoard_xCoordinate:200 yCoordinate:0 Emoticone_name:@"emoticon_cry.gif" backString:@":,-(" keyboardView:self.view];
[self emoticonButtonOnTheKeyBoard_xCoordinate:240 yCoordinate:0 Emoticone_name:@"emoticon_disappoint.gif" backString:@":-|" keyboardView:self.view];
[self emoticonButtonOnTheKeyBoard_xCoordinate:280 yCoordinate:0 Emoticone_name:@"emoticon_emo.gif" backString:@"(Emo)" keyboardView:self.view];
}
和创建方法:
- (IBAction) updatingTheTextFiledWithEmoticon
{
iPhoneClientDevAppDelegate *appDelegate = (iPhoneClientDevAppDelegate *)[[UIApplication sharedApplication] delegate];
[[appDelegate.personalchatviewcontrollerReference message_textfield] setText:[[[appDelegate.personalchatviewcontrollerReference message_textfield] text] stringByAppendingString:appDelegate.EmoticonCurrentButtonValue]];
}
- (void) emoticonButtonOnTheKeyBoard_xCoordinate:(int)x_coordinate yCoordinate:(int)y_coordinate Emoticone_name:(NSString *)emoticon_name
backString:(NSString *)stringback keyboardView:(UIView *)keyboard
{
iPhoneClientDevAppDelegate *appDelegate = (iPhoneClientDevAppDelegate *)[[UIApplication sharedApplication] delegate];
appDelegate.EmoticonCurrentButtonValue = stringback;
UIButton* emticon_button = [UIButton buttonWithType:UIButtonTypeCustom];
emticon_button.frame = CGRectMake(x_coordinate, y_coordinate, 40, 43);
[emticon_button setImage:[UIImage imageNamed:emoticon_name] forState:UIControlStateNormal];
[emticon_button setImage:[UIImage imageNamed:emoticon_name] forState:UIControlStateHighlighted];
[emticon_button setBackgroundImage:[UIImage imageNamed:@"background.png"] forState:UIControlStateNormal];
[emticon_button ]
[keyboard addSubview:emticon_button];
[emticon_button addTarget:nil action:@selector(updatingTheTextFiledWithEmoticon) forControlEvents:UIControlEventTouchUpInside];
}
所以,我成功地覆盖了键盘,但我遇到了一个大问题。 当我按下键盘上的按钮时,应用程序始终返回最后创建的按钮值。 我如何区分UIButtons?
谢谢!
答案 0 :(得分:1)
为他们提供一个唯一的.tag
属性(类型为int),并确保您的选择器将发件人作为参数:
-(IBAction)buttonPressed:(id)theButton
{
NSLog("Button %d pressed",[theButton tag]);
}