Thursday 5 July 2018

Xcode iPhone/iPad find the top View Controller in the current View Hierarchy

There are three possiblities. 
Case 1: If the view controller is embedded in UINavigationController
Case 2: If the view controller is embedded in UITabControllerBar
Case 3: If the view controller is presented  via other UIViewController
So we have to traverse through the possible combination until we reach the topmost layer.


#pragma mark- View Hierarchy-
+ (UIViewController*)topViewController {
    return [self topViewControllerWithRootViewController:[UIApplication sharedApplication].keyWindow.rootViewController];
}

+ (UIViewController*)topViewControllerWithRootViewController:(UIViewController*)rootViewController {
//Type 1: Travese in tabbar controller-
    if ([rootViewController isKindOfClass:[UITabBarController class]]) {
        UITabBarController* tabBarController = (UITabBarController*)rootViewController;
        return [self topViewControllerWithRootViewController:tabBarController.selectedViewController];
//Type 2: Travese in navigation controller-
    } else if ([rootViewController isKindOfClass:[UINavigationController class]]) {
        UINavigationController* navigationController = (UINavigationController*)rootViewController;
        return [self topViewControllerWithRootViewController:navigationController.visibleViewController];
//Type 3: Travese in view controller-
    } else if (rootViewController.presentedViewController) {
        UIViewController* presentedViewController = rootViewController.presentedViewController;
        return [self topViewControllerWithRootViewController:presentedViewController];
    } else {
        return rootViewController;
    }

}

Sunday 11 March 2018

Shortcut to add paragraph break in the text in UITextView in IBOutlet

While editing the attributed text in the text view  IBOutlet in the Interface Builder.
Press alt(option) + enter (return).