用户单击完成按钮后如何更改 UIButton 的图像是 iOS 中的另一个 UIView

How to change image for UIButton after user click on Done button is another UIView in iOS(用户单击完成按钮后如何更改 UIButton 的图像是 iOS 中的另一个 UIView)
本文介绍了用户单击完成按钮后如何更改 UIButton 的图像是 iOS 中的另一个 UIView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当用户单击 tableview 上的 UIButton 时(tableview 在每一行中都有多个按钮),会显示另一个视图.在用户单击另一个 UIView 的完成按钮后,我想更改此按钮的图像.我怎样才能做到这一点?我是新手.你能为我提供一些代码吗?提前致谢.

When the user clicks on a UIButton on tableview ( tableview has mutiple button in each row), another View is shown. I want to change image for this button after the user has clicked on the Done button of the other UIView. How can I do that? I'm a newbie. Could you please provide some code for me? Thanks in advance.

更新代码:

表格视图代码:

    - (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;
}

打开按钮的代码(用户单击以显示另一个视图的按钮)

Code for Open button ( button that user click on to show another View)

    - (void)marketPressedAction:(id)sender
{

    UIButton *button = (UIButton *)sender;
    buttontag = button.tag;
    NSLog(@"Market button click at row %d",buttontag);
}

完成按钮的代码:

 -(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];

按钮点击代码:

 savedTag = button.tag;

完成按钮点击代码:

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];

In cellForRowAtIndexPath in place of this : [market setTag:3000]; write [market setTag:indexPath.row];

试试这个:

替换:

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 的第一行设为:

One more thing : make first line of cellForRowAtIndexPath as :

NSString *CellIdentifier = @"tableCell";

将你的 cellForRowAtIndexPath 更改为:

Change your cellForRowAtIndexPath as :

- (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;
}

这篇关于用户单击完成按钮后如何更改 UIButton 的图像是 iOS 中的另一个 UIView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Why local notification is not firing for UNCalendarNotificationTrigger(为什么没有为UNCalendarNotificationTrigger触发本地通知)
iOS VoiceOver functionality changes with Bundle Identifier(IOS画外音功能随捆绑包标识符而变化)
tabbar middle tab out of tabbar corner(选项卡栏中间的选项卡角外)
Pushing UIViewController above UITabBar(将UIView控制器推送到UITabBar上方)
How can I sync two flatList scroll position in react native(如何在本机Reaction中同步两个平面列表滚动位置)
Get an event when UIBarButtonItem menu is displayed(显示UIBarButtonItem菜单时获取事件)