且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

寻找用于管理游戏关卡视图,关卡选择视图,首选项视图,存储关卡,环境变量的概念

更新时间:2023-02-02 21:10:36

我认为我应该使用 NSNotification 类。只需在包含子视图的对象(viewController)中设置一个侦听器,然后子视图就可以向控制器发送通知。然后,通知处理程序可以调用任何方法。

I think I should use the NSNotification class. It is simply set up a "listener" in the object (viewController) that contains the subviews, then subviews can send notifications to the controller. Then the notification handler can invoke any methods.

viewController 部分:

-(void) viewDidLoad
{   
//Set up a listener.
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(notificationHandler:) name:@"finishedCurrentLevel" object:nil];   
...
}

-(void) notificationHandler: (NSNotification*) notification
{
//Notification handling.
if ([notification name] == @"finishedCurrentLevel") [self finishedCurrentLevel];
}

-(void) finishedCurrentLevel
{
//View managing code here...
}

设置的通知,监听和通知的响应是这样的。实际的通知是这样的(可以从任何子视图执行):

The notification, the listening and the "responds" for the notifications set up this was. The actual notifying goes like this (can be executed from any subviews):

[[NSNotificationCenter defaultCenter] postNotificationName:@"finishedCurrentLevel" object:nil];

我认为这解决了我的沟通问题。

It solves my "communication" problem, I think.

关于全局变量,我只是创建了一个单独的globals.m文件,其coresponding globals.h没有定义任何类。它们只是附加一些 extern 变量,因此我可以从导入了globals.h的任何文件中访问它们。

About globals I simply created a separate globals.m file with the coressponding globals.h without defining any class. They just "attach" some extern variables, so I can reach them from any file having globals.h imported.