从另一个视图控制器中取消选择按钮

时间:2013-07-17 12:42:39

标签: ios uitableview uiviewcontroller uibutton

已更新

viewcontroller A转到B,然后回到A.我在viewcontrollerA的UItableview的每个单元格中都有一个UIButton。单击按钮时,按钮突出显示并转到viewcontrollerB。当你回到viewcontrollerA时,我希望取消选择按钮。 按钮的突出显示效果很好,即取消选择是问题。

在cellforrowatindexpath中,我有这个:

        if(indexPath.row==selectedbuttonindex)
        {
            addpeople.selected=YES;
        }
        else
        {
            addpeople.selected=NO;
        }

            UIImage *buttonImageNormal =[UIImage imageNamed:@"addtochatNormal.png"];
                 UIImage *buttonImageSelected =[UIImage imageNamed:@"addtochatSelected.png"];
           [addpeople addTarget:self action:@selector(addpeopletoChat:)
            forControlEvents:UIControlEventTouchDown];


                [addpeople setBackgroundImage:buttonImageSelected     forState:UIControlStateHighlighted];


                 [addpeople setBackgroundImage:buttonImageNormal forState:UIControlStateNormal];
                  [addpeople setBackgroundImage:buttonImageSelected forState:UIControlStateSelected];

                  addpeople.frame = CGRectMake(0,0, 51, 44);

                  [cell.contentView addSubview:addpeople];

我考虑了下面的答案,这里是我的更新代码,但仍然没有工作:

使用以下方法选择按钮和更改视图控制器:

  -(void) addpeopletoChat : (UIButton*)button{



       UITableViewCell *cell_ = (UITableViewCell *)button.superview.superview;
       NSIndexPath *index_Path = [topictableView indexPathForCell:cell_];
       NSLog(@"%@", index_Path);
   selectedbuttonindex=index_Path.row;
  [topictableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:index_Path]    withRowAnimation:UITableViewRowAnimationFade];
   countaddpeople=countaddpeople+1;

  addpeopletochatViewController* viewcontrollerB = [[addpeopletochatViewController alloc]    init];
   viewcontrollerB.viewcontrollerA=self;

   [self.slidingViewController anchorTopViewTo:ECRight];//changes view to viewcontroller B


  }

这应该取消选择

  -(void) somefuction{

  NSLog(@"%d", selectedbuttonindex);
   NSUInteger indexArr[] = {0,selectedbuttonindex};
   NSIndexPath *index_Path = [NSIndexPath indexPathWithIndexes:indexArr length:2];
   selectedbuttonindex=-1;
   NSLog(@"%@" @"%@", @"hi", index_Path);

   [topictableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:index_Path] withRowAnimation:UITableViewRowAnimationFade];

   }

在viewcontroller B的h文件中我有:

  #import <UIKit/UIKit.h>
  #import "officerTopicsViewController.h"

  @class officerTopicsViewController;
  @interface addpeopletochatViewController : UIViewController{


  }
  @property (nonatomic,retain) officerTopicsViewController *viewcontrollerA;

  @end

并在其m文件中我有:

 -(void)viewDidDisappear:(BOOL)animated
  {

 [viewcontrollerA somefunction];


  }

但它仍然没有用,但这次有些功能甚至没有被调用,因为NSlogs没有像第一次那样出现。

此致

1 个答案:

答案 0 :(得分:2)

你没有显示A-> B或B-> A转换代码,所以我假设从A你正在推B而从B你只是“回到”A。在Viewcontroller B中,你正在创建一个viewcontroller A的新实例并调用somefunction,它与你现有的viewcontroller无关,它是推送你的viewcontroller B的实例。你需要做的是传递你的实例viewcontroller A作为viewcontroller B的属性,并在保存的viewcontroller A上调用somefunction。或者,您可以在viewcontrollerA的viewWillAppear方法中将selectedbuttonindex重置为-1。

例如,在创建和推送ViewControllerB时:

...
viewControllerB = [[ViewControllerB alloc] init];
[viewControllerB setViewControllerA: self];
[self.navigationController pushViewController:viewControllerB animated:YES];
...

然后在ViewControllerB中你有:

-(void)viewDidDisappear:(BOOL)animated
{
    [myViewControllerA somefuction];
}