【问题标题】:Calling method from another ViewController从另一个 ViewController 调用方法
【发布时间】:2012-10-03 02:00:56
【问题描述】:

我有一个 ViewControllerA 和一个 ViewControllerB。我想从 ViewControllerB 调用 ViewControllerA 的方法。

在 ViewControllerA 中存在一个方法:

  -(NSMutableArray*) loadData;

在 ViewControllerB.h 中:

 #import "ViewControllerA.h"
  .......
 @property (nonatomic, strong) ViewControllerA * viewControllerA;
 @property (nonatomic, strong) NSMutableArray * mutableArray;

在 ViewControllerB.m 中:

self.mutableArray =[viewControllerA loadData];

但该方法没有调用。为什么?提前致谢

【问题讨论】:

  • 你是否将函数添加到视图控制器的标题中?
  • 你的意思是 viewControllerA 中的方法是类方法吗?

标签: iphone ios methods uiviewcontroller


【解决方案1】:

你不见了

self.

只要在 viewControllerB 的某处:

self.viewControllerA = [[viewControllerA alloc]init];  //or some other initialization occurs...

然后:

self.mutableArray =[self.viewControllerA loadData];

会起作用的。

【讨论】:

    【解决方案2】:

    确保在 viewControllerB 的头文件中指定了 loadData 方法。

    - (void)loadData;
    

    之后,您现在可以调用方法 loadData。

    [viewControllerA loadData];
    

    【讨论】:

      【解决方案3】:

      viewControllerA在调用[viewControllerA loadData]之前分配在ViewControllerB中?

      【讨论】:

        【解决方案4】:

        从控制器 A 推送控制器 B 时,只需指定

        viewControllerB.viewControllerA = self;
        [self.navigationController pushViewController:viewControllerB animated:YES];
        

        然后从 B 调用方法 A。您遇到的问题是由于未分配,只是声明了您在 B 中创建的“viewControllerA”。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2018-04-27
          • 2016-05-06
          • 1970-01-01
          • 1970-01-01
          • 2012-05-10
          • 1970-01-01
          • 2023-03-08
          • 1970-01-01
          相关资源
          最近更新 更多