2011-11-22 34 views
0
   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

试试这个:

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

     // do whatever you want with title 
    } 
} 
1
-(void) OnEmailIdSelection:(id)sender { 
    UIButton* button = (UIButton*)sender; 
    NSString* title = [button titleForState: UIControlStateNormal]; 
}