如何在用户点击完成button后更改UIButton的图像是iOS中的另一个UIView
当用户点击tableview上的UIButton
(tableview在每一行中有多个button)时,显示另一个视图。 我想在用户点击另一个UIView的完成button后改变这个button的图像。 我怎样才能做到这一点? 我是新手。 你能为我提供一些代码吗? 提前致谢。
更新代码:
代码为tableview:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { NSString *CellIdentifier = [NSString stringWithFormat:@"%d,%d",indexPath.section,indexPath.row]; UITableViewCell *cell = [_tableView dequeueReusableCellWithIdentifier:CellIdentifier]; if (cell == nil) { cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease]; UIButton *market = [UIButton buttonWithType:UIButtonTypeCustom]; [market addTarget:self action:@selector(marketPressedAction:) forControlEvents:UIControlEventTouchDown]; [market setTag:3000]; [market setFrame:CGRectMake(200, 6, 30, 30)]; [cell.contentView addSubview:market]; } for (UIButton *button in [cell subviews]) { // change name of table here if ([button isKindOfClass:[UIButton class]]) { button.tag = indexPath.row; [button setImage:[UIImage imageNamed:@"Marketplace.png"] forState:UIControlStateNormal]; } } cell.accessoryType = UITableViewCellAccessoryDetailDisclosureButton; _tableView.contentInset = UIEdgeInsetsMake(0, 0, 100, 0); return cell; }
打开代码button(用户点击以显示另一个视图的button)
- (void)marketPressedAction:(id)sender { UIButton *button = (UIButton *)sender; buttontag = button.tag; NSLog(@"Market button click at row %d",buttontag); }
和代码完成button:
-(void)submitMarket { for (UIButton *button in [_tableView subviews]) { // change name of table here if ([button isKindOfClass:[UIButton class]]) { if (button.tag == buttontag) { [button setBackgroundImage:[UIImage imageNamed:@"MarketplaceSelect.png"] forState:UIControlStateNormal]; } } } }
尝试这个 :
[yourButton setBackgroundImage:someImge forState:UIControlStateNormal];
点击button上的代码:
savedTag = button.tag;
完成button代码点击:
for (UIButton *button in [table subviews]) { // change name of table here if ([button isKindOfClass:[UIButton class]]) { if (button.tag == savedtag) { [button setBackgroundImage:someImge forState:UIControlStateNormal]; } } }
在cellForRowAtIndexPath代替这个: [market setTag:3000];
写[market setTag:indexPath.row];
尝试这个 :
replace:
marketButton = (UIButton *)[cell.contentView viewWithTag:3000]; [marketButton setTag:indexPath.row];
有了这个 :
for (UIButton *button in [cell subviews]) { // change name of table here if ([button isKindOfClass:[UIButton class]]) { button.tag == indexPath.row; } }
还有一件事:使cellForRowAtIndexPath的第一行为:
NSString *CellIdentifier = @"tableCell";
改变你的cellForRowAtIndexPath为:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { NSString *CellIdentifier = @"cell"; UITableViewCell *cell = [_tableView dequeueReusableCellWithIdentifier:CellIdentifier]; if (cell == nil) { cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease]; UIButton *market = [UIButton buttonWithType:UIButtonTypeCustom]; [market addTarget:self action:@selector(marketPressedAction:) forControlEvents:UIControlEventTouchDown]; [market setImage:[UIImage imageNamed:@"Marketplace.png"] forState:UIControlStateNormal]; [market setTag:indexPath.row]; [market setFrame:CGRectMake(200, 6, 30, 30)]; [cell.contentView addSubview:market]; } else { for (UIButton *button in [cell subviews]) { // change name of table here if ([button isKindOfClass:[UIButton class]]) { button.tag = indexPath.row; } } } cell.accessoryType = UITableViewCellAccessoryDetailDisclosureButton; _tableView.contentInset = UIEdgeInsetsMake(0, 0, 100, 0); return cell; }
你可以使用一个委托或者一个NSNotificationCenter,在你的视图里是Donebutton,你需要创build一个属性
@property (weak, nonatomic) <protocol_name> delegate;
并在你的DoneButton的动作方法中,你需要发送一个消息给该委托
[self.delegate method_name];
另一个视图将被设置为这个的代表
`[viewWithDoneButton setDelegate:self];
你需要实现委托方法`
你可以为你的button的选定状态设置另一个图像,并点击完成button后,只需设置选定的状态您的button。
[yourButton setBackgroundImage:someImge forState:UIControlStateSelected];
完成button后:
[yourButton setSelected:YES];
或者,如果您在UIControlStateSelected
之前使用,您也可以使用任何其他状态。 甚至用户状态 – UIControlStateApplication