Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/iphone/42.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Iphone 多个NavigationController,在“当前”中调用方法;topViewController";_Iphone_Methods_Callback_Appdelegate - Fatal编程技术网

Iphone 多个NavigationController,在“当前”中调用方法;topViewController";

Iphone 多个NavigationController,在“当前”中调用方法;topViewController";,iphone,methods,callback,appdelegate,Iphone,Methods,Callback,Appdelegate,问题在标题中。我写了“topViewController”来表示用户当前正在查看的全屏视图。我正在寻找一种从应用程序委托执行此操作的通用方法,而不管应用程序是使用0、1还是多个NavigationController 我可以为当前的应用程序解决这个问题,但需要特殊的案例代码。有时,您希望在应用程序委托中放置一个公共方法,并让它触发回调。我可以使用respondsToSelector,但它(对我来说)会对单独命名的回调方法进行难看的查找 对我来说,这只是一个有趣的问题(运行中的应用程序肯定知道!)

问题在标题中。我写了“topViewController”来表示用户当前正在查看的全屏视图。我正在寻找一种从应用程序委托执行此操作的通用方法,而不管应用程序是使用0、1还是多个NavigationController

我可以为当前的应用程序解决这个问题,但需要特殊的案例代码。有时,您希望在应用程序委托中放置一个公共方法,并让它触发回调。我可以使用respondsToSelector,但它(对我来说)会对单独命名的回调方法进行难看的查找


对我来说,这只是一个有趣的问题(运行中的应用程序肯定知道!),在你的军械库中有一个通用的解决方案会很好。

你是否在包含要调用方法的俯视图控制器的应用程序委托中引用导航控制器?如果是,UINavigationController有一个名为
topViewController
的成员。可以将其强制转换为自定义视图控制器类:

CustomViewController *customController = (CustomViewController *)[aNavController topViewController];
[customController myCustomMethod];
正如我所说的,如果你在你的App委托中有一个对导航控制器的引用,那么这个方法会起作用

如果没有,另一种方法可能是使用NSNotificationCenter。在“topViewController”中注册自定义通知名称,并从应用程序代理发布该通知。如果在每个视图控制器可见时注册通知,并在其消失时取消注册,则只有可见视图控制器才会收到通知:

在您的情况下,可能在
viewdide:

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(myCustomMethod:) name:@"MyCustomNotification" object:nil];
[[NSNotificationCenter defaultCenter] removeObserver:self name:@"MyCustomNotification" object:nil];
发布来自应用程序代理的通知:

[[NSNotificationCenter defaultCenter] postNotificationName:@"MyCustomNotification" object:myAccount];
不要忘了在
viewdidegame:

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(myCustomMethod:) name:@"MyCustomNotification" object:nil];
[[NSNotificationCenter defaultCenter] removeObserver:self name:@"MyCustomNotification" object:nil];

多个导航控制器。或者它可能只有一个Tabcontroller,而不需要navcontroller。