UIButton标题问题

时间:2014-07-23 09:55:54

标签: ios objective-c uitableview uibutton

我从UITableViewCell获取数据,并且存储到字符串中的是countryString。我想用国家字符串更改按钮标题,但它不起作用,我认为这是一个简单的问题,但我在过去2小时内挣扎。我仍然无法找到解决方案,为什么不工作

    countryButton=[[UIButton alloc]initWithFrame:CGRectMake(145, 145, 100, 30)];
    countryButton.backgroundColor=[UIColor whiteColor];

    [countryButton setTitle:CountryString forState:UIControlStateNormal];
    countryButton.titleLabel.textColor=[UIColor blackColor];
    [countryButton addTarget:self action:@selector(countryTable) forControlEvents:UIControlEventTouchUpInside];
    [notesView addSubview:countryButton]; 


-(void)tableView:(UITableView*)tableView didSelectRowAtIndexPath:(NSIndexPath*)indexPath 
{   
    jsonDict = [newarray objectAtIndex:indexPath.row];
    CountryString=[jsonDict objectForKey:@"countryName"];
    NSLog(@"country is %@", CountryString);
    CountryTableview.hidden=YES;
}

2 个答案:

答案 0 :(得分:1)

假设它是你的str

 CountryString =@"XXXX";

UIButton *countryButton = [UIButton buttonWithType:UIButtonTypeRoundedRect]; //or button type custom 
[countryButton addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
[countryButton setFrame:CGRectMake(145, 145, 100, 30)];
[countryButton setTitle: CountryString forState:UIControlStateNormal];
[countryButton setExclusiveTouch:YES];
countryButton.backgroundColor=[UIColor whiteColor];
countryButton.titleLabel.textColor=[UIColor blackColor];
[self.view addSubview: countryButton];   //whatever u need self.view or notesView

-(void) buttonClicked:(UIButton*)sender
{
NSLog(@"you clicked on button %@", sender.tag);
 }

答案 1 :(得分:1)

试试这个

-(void)tableView:(UITableView*)tableView didSelectRowAtIndexPath:(NSIndexPath*)indexPath 
{   
    jsonDict = [newarray objectAtIndex:indexPath.row];
    CountryString=[jsonDict objectForKey:@"countryName"];
    NSLog(@"country is %@", CountryString);
    [countryButton setTitle:CountryString forState:UIControlStateNormal];
     countryButton.titleLabel.textColor=[UIColor blackColor];
    CountryTableview.hidden=YES;
}