Ahmed
Ahmed

Reputation: 1359

accessing window property app delegate iOS

I am a newbie to IOS. I have one simple question. I want to access the window property in the AppDelegate.h file from another file. Also, I can't import the AppDelegate.h file to the other file because the files will keep importing each other endlessly. Is it possible to access the window property from outside the AppDelegate.h file?

thanks

Upvotes: 1

Views: 1828

Answers (2)

engili
engili

Reputation: 118

Option1 use KVC

id appDelegate = [UIApplication sharedApplication].delegate;
UIWindow *window = [appDelegate valueForKey:@"window"];

Option2 use keywindow property of UIApplication

UIWindow *keyWindow = [[UIApplication sharedApplication] keyWindow];

This property holds the UIWindow object in the windows array that is most recently sent the makeKeyAndVisible message.

Option3 use windoss property of UIApplication

NSArray *array = [[UIApplication sharedApplication] windows];

This property contains the UIWindow objects currently associated with the app. This list does not include windows created and managed by the system, such as the window used to display the status bar.

The windows in the array are ordered from back to front by window level; thus, the last window in the array is on top of all other app windows.

Upvotes: 2

Johnty
Johnty

Reputation: 195

Make global variable of AppDelegate like below, so you can use/access property of AppDelegate whenever you want.

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

And you can access window object by

appDelegate.window ...

Note: Don't forget to add declare object in .h file of AppDelegate

AppDelegate *appDelegate;

Upvotes: 1

Related Questions