从其他视图访问表视图的按钮 [英] Accessing a button of a table view from other view
问题描述
我如何从任何其他视图访问视图中存在的按钮.让我详细说明一下,我有两个视图控制器A和B.在视图A中,我在表视图中有一个按钮,例如button1.我要访问我试图从另一个视图中查看表视图中的那个按钮.请帮忙.我尝试创建A类的对象,然后编写[objA.tableview button1然后我设置图像],但是它没有用.请帮助.任何帮助将不胜感激.
How can i access a button which is present in a view ,from any other view.Let me elaborate i have two view controller A and B.in view A i have a button inside table view say button1 .I want to access that button inside the table view from my other view.Please help.I tried creating an object of class A and then write [objA.tableview button1 then i set image ] but it hasnt worked .Please help. Any help will be appreciated.
谢谢, 克里斯蒂
推荐答案
您可以尝试为此按钮添加标签(确保它是唯一的).
You can try this give a tag to your button (make sure it is unique).
假设您的按钮带有标签45;并且您已经在表格视图中做到了.
Let say your button has tag 45; and you have made it in your table view.
现在,在另一种方法中,您想使用该按钮,如下所示:-
Now in another method you want to use your that button like this:-
-(void)deletePostMethod
{
UIButton *button=(UIButton *)[self.view viewWithTag:49];
[button setBackgroundColor:[UIColor redColor]];
NSLog(@"button=%@",button);
}
编辑答案:-
- (UITableViewCell *)tableView:(UITableView *)tablefirst cellForRowAtIndexPath:(NSIndexPath *)indexPath {
// I have added a button in my table view in first row
if (indexPath.row==0) {
UIButton *btn=[UIButton buttonWithType:UIButtonTypeCustom];
[btn setFrame:CGRectMake(0, 0, 40, 30)];
[btn addTarget:self action:@selector(deletePostMethod) forControlEvents:UIControlEventTouchUpInside];
btn.tag=579;
[cell.contentView addSubview:btn];
}
return cell;
}
在该按钮上单击方法
-(void)deletePostMethod
{
UIButton *button=(UIButton *)[self.view viewWithTag:579];
[button setBackgroundColor:[UIColor redColor]];
NSLog(@"button=%@",button);
}
点击此按钮按钮后,背景色设置为红色.
After clicking this button button background color set to red.
这篇关于从其他视图访问表视图的按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!