【问题标题】:Access viewController data from popOverView从 popOverView 访问 viewController 数据
【发布时间】:2013-05-25 17:19:40
【问题描述】:

假设我有一个viewController 视图和一个弹出窗口UITableView 表。

我希望能够通过从表格中选择一个选项来更改调用弹出表格的viewController 的背景视图。

所以我想做的是:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{


    int selectedEntry = indexPath.row;
    switch(selectedEntry){
        case 1:
          //CODE TO CHANGE ORIGINAL VIEW TO IMAGE 1
        case 2:
          //CODE TO CHANGE ORIGINAL VIEW TO IMAGE 2
        //etc
   }
}

我已经知道,如果我直接在基视图类中编写方法,我可以直接编写

self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"IMAGE"]];

但由于它是从弹出菜单中调用的,我不知道如何访问它。

【问题讨论】:

  • 你在哪里写这个方法..?在同一个视图控制器或任何其他视图控制器中..?

标签: ios objective-c xcode ipad uiviewcontroller


【解决方案1】:

一种解决方案可能是在您的 UITableView 子类中向您的 UIViewController 添加一个属性,这样您就可以保留对它的引用,然后再访问它:

@property (nonatomic, weak) MyViewController *parentVC;

MyViewController 是你的 UIViewController 子类)

UIViewController 初始化UITableView 时,将其parentVC 属性设置为self。然后在didSelectRowAtIndexPath:,使用:

parentVC.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"IMAGE"]];

【讨论】:

    【解决方案2】:

    当您单击弹出框控制器中的按钮时,您可以使用委托来监听按钮操作并在视图控制器中执行必要的操作。或者您可以使用 NSNotification 在按钮单击期间发布通知并在视图控制器中收听通知。

    在按钮动作中写下这个

    [[NSNotificationCenter defaultCenter] postNotificationName:UIButtonOneClickedNotification object:nil];
    

    把这个写在视图控制器的didLoad方法中

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(buttonOneClicked:) name:UIButtonOneClickedNotification object:nil];
    

    并在方法buttonOneClicked中做你需要做的事情:

    【讨论】:

      【解决方案3】:

      你可以使用NSNotificationCenter

      添加此行。

      - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
      {
          int selectedEntry = indexPath.row;
          switch(selectedEntry){
              case 1:
                [[NSNotificationCenter defaultCenter] postNotificationName:@"ChangeBackground" object:self userInfo:yourData1];
              case 2:
                [[NSNotificationCenter defaultCenter] postNotificationName:@"ChangeBackground" object:self userInfo:yourData2];
              //etc
         }
      }
      

      将此添加到您要更改背景的BaseViewController

      - (void)viewDidLoad {
      
      [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(changeBackgroundView:) name:@"ChangeBackground" object:nil];
      
      }
      
      - (void)changeBackgroundView:(NSNotification *)yourData {
      
          self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"IMAGE"]];
      
      }
      

      【讨论】:

      • 我对应该为“yourData”输入什么感到困惑。它只是带有图像文件名的字符串吗?
      • 如果我尝试做 [[NSNotificationCenter defaultCenter] postNotificationName:@"ChangeBackground" object:self userInfo:@"IMAGE1.png"];然后,当我调用 changeBackgroundView 时,我无法使用“yourData”作为文件名的字符串。
      • 像这样访问它。 NSString *imageName = [yourData userInfo];
      • 而不是 userinfo 在“object”中传递你的字符串,像这样 [[NSNotificationCenter defaultCenter] postNotificationName:@"ChangeBackground" object:@"IMAGE1.png" userInfo:nil];并按如下方式访问它。 NSString *imageName = [你的数据对象];因为 userInfo 只接受 NSDictionary 作为参数。您可以在“object”中传递任何对象,在“userInfo”中传递 NSDictionary。在选择器方法中,只需使用 [yourData object] 访问它;和 [yourData userInfo];
      【解决方案4】:

      通过使用协议,您可以这样做 像 AVC(aViewController) show BVC(bviewcontroller) as popover 那么你需要做的如下 在 BVC.h 中

      @protocol TablePopoverDelegate <NSObject>
      -(void)doSomeWork;
      @end
      @property (nonatomic, weak) id<TablePopoverDelegate> delegate;
      

      在 BVC.m 中你需要做的是

      - (IBAction)buttoonpressd:(id)sender {
          [self.delegate doSomeWork];
      }
      

      在 AVC.h 中你需要添加这个协议:"TablePopoverDelegate"

      然后在 AVC.m 中你需要实现所需的功能 并设置委托 如下所示

      从你展示弹出框的地方做这些事情

      这里的“按钮”是一个发送者,按下它就会显示弹出框

      __weak UIViewController *selfView = self;
      
      UIViewController *nextViewController;
      UIViewController *infoViewController = [[UIViewController alloc] init];
                  infoViewController.delegate = self;
                  nextViewController = infoViewController;
      
      
      _popOverController = [[UIPopoverController alloc] initWithContentViewController:nextViewController];
              [self.popOverController setDelegate:self];
              __weak UIPopoverController *blockPopoverController = self.popOverController;
      
              self.popoverControllerPresentationBlock = ^(void){
                  [blockPopoverController presentPopoverFromRect:[button convertRect:button.bounds toView:selfView.view]
                                                          inView:selfView.view
                                        permittedArrowDirections:UIPopoverArrowDirectionUp
                                                        animated:YES];
              };
              // invoke the presentation block to show the popover now
              self.popoverControllerPresentationBlock();
      

      这是您通过委托调用的函数

      -(void)doSomeWork
      {
          //do your stuff here
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2016-06-27
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-11-04
        相关资源
        最近更新 更多