我试图在聊天屏幕上隐藏UIAlertView,并在用户收到消息时在所有其他页面上显示。
知道如何从appDelegate找到当前的控制器吗?
我是一个目标C还在学习的菜鸟。
-(void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo {
if (isOnInstantMessagingScreen) {
//Show nothing
}
else {
UIApplicationState state = [application applicationState];
if (state == UIApplicationStateActive) {
NSString *cancelTitle = @"Close";
NSString *showTitle = @"View";
NSString *message = [[userInfo valueForKey:@"aps"] valueForKey:@"alert"];
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"New Message!"
message:message
delegate:self
cancelButtonTitle:cancelTitle
otherButtonTitles:showTitle, nil];
[alertView show];
}
}
我发现了这个:
- (UIViewController *)topViewController{
return [self topViewController:[UIApplication sharedApplication].keyWindow.rootViewController];
}
- (UIViewController *)topViewController:(UIViewController *)rootViewController
{
if ([rootViewController isKindOfClass:[UINavigationController class]]) {
UINavigationController *navigationController = (UINavigationController *)rootViewController;
return [self topViewController:[navigationController.viewControllers lastObject]];
}
if ([rootViewController isKindOfClass:[UITabBarController class]]) {
UITabBarController *tabController = (UITabBarController *)rootViewController;
return [self topViewController:tabController.selectedViewController];
}
if (rootViewController.presentedViewController) {
return [self topViewController:rootViewController];
}
return rootViewController;
}
我的消息controller = MessageViewController
我如何调用此代码?
答案 0 :(得分:0)
如果你没有使用导航控制器,那么你可以通过以下方式获得:
<?php
use Doctrine\Common\Annotations\AnnotationRegistry;
use Composer\Autoload\ClassLoader;
error_reporting(error_reporting() & ~E_USER_DEPRECATED);
$loader = require __DIR__.'/../vendor/autoload.php';
AnnotationRegistry::registerLoader(array($loader, 'loadClass'));
return $loader;
如果您查看控制器是在导航控制器下托管,则需要额外的步骤。
id myCurrentController = self.window.rootViewController; //you can cast it
答案 1 :(得分:0)
解决相同的问题,将控制器嵌套在UITabBarController
和UINavigationController
中,我做了这个(通用)扩展名:
import Foundation
import UIKit
extension AppDelegate {
func findController<T: UIViewController>(_ type: T.Type) -> T? {
let controller = window?.rootViewController
return findController(type: type, controller)
}
fileprivate func findController<T: UIViewController>(type: T.Type, _ controller: UIViewController?) -> T? {
guard controller != nil else {
return nil
}
return controller as? T ??
findController(type: type, nestedIn: controller as? UINavigationController) ??
findController(type: type, nestedIn: controller as? UITabBarController)
}
fileprivate func findController<T: UIViewController>(type: T.Type, nestedIn controller: UINavigationController?) -> T? {
return findController(type: type, controller?.topViewController)
}
fileprivate func findController<T: UIViewController>(type: T.Type, nestedIn controller: UITabBarController?) -> T? {
return findController(type: type, controller?.childViewControllers[controller!.selectedIndex])
}
}
像这样使用:
if let controller = findController(YouViewController.self) {
controller.whateverYouNeed()
}