需要从另一个viewController调用其他viewControllers中的方法 [英] Need to call methods in other viewControllers from another viewController

查看:222
本文介绍了需要从另一个viewController调用其他viewControllers中的方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个具有多个viewControllers的应用程序,其中一些viewControllers包含运行各种任务的方法.我需要做的是在初始viewController加载时,在其他viewController中调用这些方法,以便它们在后台运行,但是,这样做有一定难度.

I have an app that has multiple viewControllers, where some of these viewControllers contain methods that run various tasks. What I need to do is when the initial viewController loads, is to call these methods in the other viewControllers such that they run in the background, however, I am having some difficulty doing this.

假设我有4个viewController,分别是A,B,C和& D,其中A是初始viewController,在每个viewController中,我分别具有aMethod,bMethod,cMethod和dMethod.这是相关代码:

Let's say I have 4 viewControllers, A, B, C, & D, where A is the initial viewController, and in each viewController, I have aMethod, bMethod, cMethod, and dMethod respectively. Here is the relevant code:

在我打开的viewController(AviewController)里面:

Inside my opening viewController (AviewController):

在.h文件中:

#import "BViewController"
#import "CViewController"
#import "DViewController"

@interface AViewController:UIViewController {

BViewController *bViewCon;
CViewController *cViewCon;
DViewController *dViewCon;


}

@property (nonatomic, retain) BViewController *bViewCon;
@property (nonatomic, retain) CViewController *cViewCon;
@property (nonatomic, retain) DViewController *dViewCon;

@end

在我的.m文件中,我具有以下内容:

In my .m file I have the following:

#import "BViewController"
#import "CViewController"
#import "DViewController"

@implementation AviewController

@synthesize bViewCon, cViewCon, dViewCon;

- (void) viewDidLoad {

[super viewDidLoad];

bViewCon = [[BViewController alloc] init];
[bViewCon bMethod];

...

}

但是,我收到错误消息,对于'BViewController',没有可见的@interface声明了选择器'bMethod'".我需要以与该类(即AViewController)相同的方式从其他viewControllers中调用其他方法.

However, I am getting the error message, "No visible @interface for 'BViewController' declares the selector 'bMethod'". I need to call the other methods from the other viewControllers the same way from this class (i.e. AViewController).

首先感谢所有回复的人.

Thanks in advance to all who reply.

推荐答案

要解决您收到的错误,请确保在每个控制器的头(.h)文件中声明了所有方法(否则,编译器将无法看到它们.)

To solve the error you're receiving, make sure that all of the methods are declared in the header (.h) files of each controller (otherwise, the compiler won't be able to see them).

由于所有这些控制器都是AViewController的子级(它们是由AViewController创建的,并保留为ivars),因此在这里我不会使用NSNotificationCenter(除非还有其他需要通知的对象)如果某些事件也发生了,这些事件不属于AViewController.

As all of these controllers are children to AViewController (they are created by AViewController and kept as ivars on it), I wouldn't use NSNotificationCenter here (unless there are other objects that need to be notified in case of certain events occurring too, which aren't owned by AViewController).

相反,我只是在尝试进行操作时直接调用这些方法.

Instead, I would just call the methods directly as you're attempting to do.

另一方面,如果这些方法正在启动正在进行的任务(在后台运行任务),则最好将方法调用移至AViewControllerinit:方法. (在iOS 5上,视图可以被卸载,因此viewDidLoad:可能被多次调用……例如在内存警告和视图被屏蔽的情况下).我可能会去做这样的事情:

On another note, if these methods are starting ongoing tasks (running tasks in background), it may be best for you to move the method calls to the init: method of AViewController. (As on iOS 5, views CAN be unloaded and hence viewDidLoad: can be called potentially more than once... such as in the case of memory warnings and the view being off screened). I might go about doing something like this:

- (id)initWithNibName:(NSString *)nibName bundle:(NSBundle *)bundle
{
    self = [super initWithNibName:nibName bundle:bundle];  // your correct stuff here
    if (self)
    {
         bViewCon = [[BViewController alloc] init];
         [bViewCon bMethod];
         // ... and so on for the other controllers
    }
    return self;
}

修改

尽管,如评论中所述,UIViewController在内存方面并不便宜……老实说,最好将代码重构为具有单个控制器(而不是NSObject的子类. UIViewController(价格便宜))来充当将在后台运行的任务的管理器.我想这可能会在以后帮助您,因为它将有助于分隔每个控制器的任务和目的(在这种情况下,UIViewController应该主要负责管理视图(在某些情况下/视图层次结构)和相关任务...如果正在进行的任务不在与该视图相关联的事物范围之内,则可能是UIViewController不应该处理它们的迹象...

Although, as mentioned in a comment, UIViewControllers aren't exactly cheap in terms of memory... it honestly would likely be best to refactor this code to have a single controller (a subclass of NSObject instead of UIViewController, which is cheaper) to act as a manager for the tasks that are going to be running in the background. I imagine this will likely help you later down the road too, as it would help compartmentalize the tasks and purpose of each of your controllers (in such, UIViewControllers should primarily be responsible for managing a view (/view hierarchy in some cases) and associated tasks... if there are ongoing tasks that are occurring outside the scope of things associated with said view, it's probably a sign that the UIViewController shouldn't be handling them...

这篇关于需要从另一个viewController调用其他viewControllers中的方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆