Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6146233
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T18:55:05+00:00 2026-05-23T18:55:05+00:00

In the Key-Value Observing Programming Guide , the section Registering for Key-Value Observing says

  • 0

In the Key-Value Observing Programming Guide, the section Registering for Key-Value Observing says “Typically properties in Apple-supplied frameworks are only KVO-compliant if they are documented as such.” But, I haven’t found any properties in the documentation that are documented as KVO-compliant. Would you please point me to some?

Specifically, I would like to know if the @property(nonatomic,retain) UIViewController *rootViewController of UIWindow is KVO-compliant. The reason is that I’m adding the rootViewController property to UIWindow for iOS < 4 and want to know if I should make it KVO-compliant.

@interface UIWindow (Additions)

#if __IPHONE_OS_VERSION_MIN_REQUIRED < __IPHONE_4_0
@property (nonatomic, retain) UIViewController *rootViewController;
#endif;

@end

@implementation UIWindow (Additions)

#if __IPHONE_OS_VERSION_MIN_REQUIRED < __IPHONE_4_0
@dynamic rootViewController;

- (void)setRootViewController:(UIViewController *)newRootViewController {
    if (newRootViewController != _rootViewController) {
        // Remove old views before adding the new one.
        for (UIView *subview in [self subviews]) {
            [subview removeFromSuperview];
        }
        [_rootViewController release];
        _rootViewController = newRootViewController;
        [_rootViewController retain];
        [self addSubview:_rootViewController.view];
    }
}
#endif

@end
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T18:55:06+00:00Added an answer on May 23, 2026 at 6:55 pm

    Short answer: No.

    Long answer: Nothing in UIKit is guaranteed to be KVO-compliant. If you happen to find that KVO-ing a property works, be grateful, it’s unintentional. Also: be wary. It could very well break in the future.

    If you find that this is something you need, please file an enhancement request.


    About your actual code, it’s inherently flawed. Do NOT attempt to add a “rootViewController” setter to UIWindow this way. It will break when you compile your code on iOS 4 but someone runs it on an iOS 5 device. Because you compiled using the 4.x SDK, the #if statements will evaluate to true, meaning your category method smasher will be included in the binary. However, when you run it on an iOS 5 device, you’re now going to get a method conflict because two methods on UIWindow will have the same method signature, and there’s no guarantee as to which one will be used.

    Don’t screw with the frameworks like this. If you have to have this, use a subclass. THIS IS WHY SUBCLASSING EXISTS.


    Your subclass would look something like this:

    @interface CustomWindow : UIWindow
    
    @property (nonatomic, retain) UIViewController *rootViewController;
    
    @end
    
    @implementation CustomWindow : UIWindow
    
    static BOOL UIWindowHasRootViewController = NO;
    
    @dynamic rootViewController;
    
    - (void)_findRootViewControllerMethod {
      static dispatch_once_t predicate;
      dispatch_once(&predicate, ^{
        IMP uiwindowMethod = [UIWindow instanceMethodForSelector:@selector(setRootViewController:)];
        IMP customWindowMethod = [CustomWindow instanceMethodForSelector:@selector(setRootViewController:)];
        UIWindowHasRootViewController = (uiwindowMethod != NULL && uiwindowMethod != customWindowMethod);
      });
    }
    
    - (UIViewController *)rootViewController {
      [self _findRootViewControllerMethod];
      if (UIWindowHasRootViewController) {
        // this will be a compile error unless you forward declare the property
        // i'll leave as an exercise to the reader ;)
        return [super rootViewController];
      }
      // return the one here on your subclass
    }
    
    - (void)setRootViewController:(UIViewController *)rootViewController {
      [self _findRootViewControllerMethod];
      if (UIWindowHasRootViewController) {
        // this will be a compile error unless you forward declare the property
        // i'll leave as an exercise to the reader ;)
        [super setRootViewController:rootViewController];
      } else {
        // set the one here on your subclass
      }
    }
    

    Caveat Implementor: I typed this in a browser window

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

After reading the Key-Value Coding Programming Guide , the Key-Value Observing Programming Guide and
How can I set KVO (key-value-observing) with an NSMutableArray ? I want to be
I'm using key value observing on a boolean property an NSObject method: -(void)observeValueForKeyPath:(NSString *)keyPath
I am trying to get key-value-observing to work for an NSMutableArray. Below is the
I've looked on SO for examples of using Key Value Observing with an NSArray
The following key:value pairs are 'page' and 'page contents'. { 'section-a.html':{'contents':'section-b.html section-c.html section-d.html'}, 'section-b.html':{'contents':'section-d.html
I am starting out using key value observing, and the mutable array I'm observing
I'm having the worst time getting key value observing working in with a UITextView's
Can someone explain in simple terms what is Key-Value-Coding and Key-Value-Observing ? Please don't
When working with a custom NSOperation subclass I noticed that the automatic key-value observing

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.