在其点击事件中获取按钮标题

时间:2011-11-22 11:52:28

标签: iphone ios

                for (int i = 0; i < ABMultiValueGetCount(emails)  ; i++)
                        {

                            CFStringRef email = ABMultiValueCopyValueAtIndex(emails, i);
                            //CFStringRef emailType = ABMultiValueCopyLabelAtIndex(emails, i);
                            emailString  = (NSString *)email;
                            //emailTypeString = (NSString *)emailType;

                            UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
                            button.frame = CGRectMake(15, 320 - x, 290, 40);
                            [button setTitle:emailString forState:UIControlStateNormal];
                            button.titleLabel.font  = [UIFont fontWithName:@"Helvetica Bold" size:18];
                            [button addTarget:self action:@selector(OnEmailIdSelection:) forControlEvents:UIControlEventTouchUpInside];
                            [self.selectEmailId addSubview:button];
                            x = x+70;
                        }


                }

我有上面的代码,其中有一个按钮单击事件OnEmailIdSelection。在这个事件中,我想得到发送者的,即点击按钮的标题到字符串变量。

2 个答案:

答案 0 :(得分:2)

试试这个:

-(void) OnEmailIdSelection:(id)sender
{
    if ([sender isKindOfClass:[UIButton class]])
    {
        UIButton *button = (UIButton*)sender;
        NSString *title = button.currentTitle;

        // do whatever you want with title
    }
}

答案 1 :(得分:1)

-(void) OnEmailIdSelection:(id)sender {
    UIButton* button = (UIButton*)sender;
    NSString* title = [button titleForState: UIControlStateNormal];
}