unom
unom

Reputation: 11476

What is different in setting a "Custom class" for a storyboard View Controller OR subclassing UIViewController and creating instances of that?

I just created a simple Master-Detail app (SplitViewController).

If I create a New File - Objective-C class with the name MYCustomSplitViewController subclassing/inheriting from UISplitViewController I can do two things:

  1. Go into Main.Storyboard, select the Split View Controller and set its "Custom Class" in the Identity Inspector to "MYCustomUISplitViewController"
    • Now, when the Split View Controller is loaded i get a "hit" inside MYCustomSplitViewController -viewDidLoad method.

In AppDelegate.m I have this, and one can clearly see that *splitViewController is an instance of UISplitViewController and not MYCustomUISplitViewController.

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    // Override point for customization after application launch.
    UISplitViewController *splitViewController = (UISplitViewController *)self.window.rootViewController;
    UINavigationController *navigationController = [splitViewController.viewControllers lastObject];
    splitViewController.delegate = (id)navigationController.topViewController;
    return YES;
}

OR 2. Go into Main.Storyboard, select the Split View Controller and set its "Custom Class" in the Identity Inspector to "none" - it defaults to "UISplitViewController"

In AppDelegate.m I now modify *splitViewController to be a MYCustomUISplitViewController directly.

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    // Override point for customization after application launch.
    MYCustomUISplitViewController *splitViewController = (MYCustomUISplitViewController *)self.window.rootViewController;
    UINavigationController *navigationController = [splitViewController.viewControllers lastObject];
    splitViewController.delegate = (id)navigationController.topViewController;
    return YES;
}

In the second case I don't get a "hit" in the -viewDidLoad of MYUISplitViewController.

I don't get it... is MYUISplitViewController in this case only used for creating the instance *splitViewController and that's it? After that it doesn't access the methods any more? Does leaving the "Custom class" field empty actually tie it to some UISplitViewController that I can't see (Apple default)?

If so how does setting the "Custom class" in the first case tie the Instance on screen with the code in my file? How does UISplitViewController become the "Custom Class" for the Instance on screen when I clearly created an Instance of MYCustomUISplitViewController?

Upvotes: 0

Views: 315

Answers (1)

Paul.s
Paul.s

Reputation: 38728

The type you use in code is just a hint to the compiler about what you expect a variable to be. In your first example you declare

UISplitViewController *splitViewController = (UISplitViewController *)self.window.rootViewController;

Which you are confusing for meaning that this is an instance of UISplitViewController. To get the actual class log out NSLog(@"%@", splitViewController.class). If you changed the custom class in the xib then the logged out class should be the custom class you set in the xib

Upvotes: 1

Related Questions