0

I'm trying to hide a UIAlertView from the chat screen and shows on all other pages when the user receives a message.

Any idea how i can find the current controller from appDelegate ?

I'm a noob with objective C still learning.

-(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];

}



}

I have found this:

 - (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;
   }

And my messages controller = MessageViewController

How can i call this code in?

V. Sec
  • 1
  • 4

2 Answers2

0

If you're not using a navigation controller than you can get it by:

id myCurrentController = self.window.rootViewController; //you can cast it

If you're view controller is hosted under a navigation controller than you need an extra step.

id navigationController = self.window.rootViewController;
UINavigationController *nav = (UINavigationController*)navigationController;
id myController = nav.topViewController
Akram
  • 453
  • 4
  • 21
  • Does i need to add that code inside: - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions – V. Sec Dec 03 '15 at 23:33
  • No, you could use it anywhere in the AppDelegate. – Akram Dec 04 '15 at 00:32
  • How can i use id myCurrentController = self.window.rootViewController; after place it. if (myCurrentController = 'MessagesViewController' ) { } Something like this ? – V. Sec Dec 04 '15 at 00:36
  • You could check the type of myCurrentController like the following if ([myCurrentController isKindOfClass:[MessagesViewController class]]){ MessagesViewController * mvc = (MessagesViewController*)myCurrentController; } – Akram Dec 04 '15 at 12:38
0

Solving the same issue, having my controller nested in both UITabBarController and UINavigationController I made this (generic) extension:

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])
    }
}

Use like this:

if let controller = findController(YouViewController.self) {
    controller.whateverYouNeed()
}
user2878850
  • 2,446
  • 1
  • 18
  • 22