views:

26

answers:

1

Hi!

I make a keyboard from UIButtons. On this keyboard there are 49 UIButtons, which create the following:


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

}

and the creating methods:


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

So, I successfully covered the keyboard, but I have a big problem. When I press a button on the keyboard, the application always return the last created button value. How can I distinguish the UIButtons?

Thanks!

+1  A: 

Give them a unique .tag property (type int), and make sure your selector takes the sender as an argument:

-(IBAction)buttonPressed:(id)theButton
{
    NSLog("Button %d pressed",[theButton tag]);
}
mvds