Wednesday 13 August 2014

Get delegate and window reference in iOS

AppDelegate *appDelegate = (AppDelegate*) [[UIApplication sharedApplication] delegate];



UIWindow *mainWindow = [[UIApplication sharedApplication] windows].lastObject;
// or
 UIWindow *mainWindow = [(AppDelegate*) [[UIApplication sharedApplication] delegate] window];


// If UINavigationController is on the rootViewController

UINavigationController *navController = (UINavigationController*) [[appDelegate window] rootViewController];



//=====================     AppDelegate.m          ====================
#define IS_IPHONE_5 ( fabs( ( double )[ [ UIScreen mainScreen ] bounds ].size.height - ( double )568 ) < DBL_EPSILON )

@implementation AppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    // Override point for customization after application launch.
    UIStoryboard *mainStoryboard = nil;
    if (IS_IPHONE_5) {
        mainStoryboard = [UIStoryboard storyboardWithName:@"Storyboard_iPhone5" bundle:nil];
    }
    else {
        mainStoryboard = [UIStoryboard storyboardWithName:@"Storyboard_iPhone4" bundle:nil];
    }
    
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    self.window.rootViewController = [mainStoryboard instantiateInitialViewController];
    [self.window makeKeyAndVisible];
    return YES;
}

No comments:

Post a Comment