Я новичок в разработке iPhone. В моем приложении я использую tableview.In tableview Я использую два uibuttons один в белый цвет и еще один в graycolor. Если я нажму кнопку серого цвета, он должен изменен на белый цвет, а другая кнопка должна быть заменена на серый цвет в одно и то же время.Как изменить цвет фона двух uibutton на действие одного клика в iPhone?
Это мой код:
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
ExampleCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
cell=nil;
if(cell == nil)
{
cell = [[ExampleCell alloc] initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:CellIdentifier];
}
if(indexPath.row == 0)
{
postbtn = [UIButton buttonWithType:UIButtonTypeCustom];
[postbtn setFrame:CGRectMake(0, 205, 160, 34)];
[postbtn setBackgroundColor:[UIColor whiteColor]];
[postbtn setTitle:[NSString stringWithFormat:@"%@ POST",[AppDelegate.profileDic objectForKey:@"posts"]] forState:UIControlStateNormal];
[postbtn.titleLabel setFont:[UIFont fontWithName:@"Verdana-Bold" size:10]];
[postbtn setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
[postbtn setTag:1001];
[postbtn addTarget:self action:@selector(reloadAction:) forControlEvents:UIControlEventTouchUpInside];
[cell addSubview:postbtn];
likebtn = [UIButton buttonWithType:UIButtonTypeCustom];
[likebtn setFrame:CGRectMake(160, 205, 160, 34)];
[likebtn setBackgroundColor:[UIColor lightGrayColor]];
[likebtn setTitle:[NSString stringWithFormat:@"%@ LIKE",[AppDelegate.profileDic objectForKey:@"likes"]] forState:UIControlStateNormal];
[likebtn.titleLabel setFont:[UIFont fontWithName:@"Verdana-Bold" size:10]];
[likebtn setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
[likebtn setTag:1002];
[likebtn addTarget:self action:@selector(reloadAction:) forControlEvents:UIControlEventTouchUpInside];
[cell addSubview:likebtn];
}
)};
-(void)reloadAction:(id)sender
{
if ([sender tag] == 1001)
{
[postbtn setBackgroundColor:[UIColor whiteColor]];
[likebtn setBackgroundColor:[UIColor lightGrayColor]];
AppDelegate.selectedTab=1;
[profiletableview reloadData];
}
if ([sender tag] == 1002)
{
[postbtn setBackgroundColor:[UIColor lightGrayColor]];
[likebtn setBackgroundColor:[UIColor whiteColor]];
AppDelegate.selectedTab=2;
[self likewebservice];
[profiletableview reloadData];
}
}
Есть ли способ изменить цвет фона UIButton.
где проблема? ваш код работает или нет? – Ilario
Вам не нужно использовать тег 'if (sender == postbtn)' должен работать – KIDdAe