Objective-c – iphone cocoa “error:request for member ____ in something not a structure or union”

cocoacocoa-touchiphoneobjective-cuiviewcontroller

this way works:

type1ViewController *viewController = [[type1ViewController alloc] initWithNibName:@"Type1View" bundle:nil];
viewController.parentViewController = self;
self.type1ViewController = viewController;
[self.view insertSubview:viewController.view atIndex:0];
[viewController release];

but this way gives me the error, "request for member parentViewController in something not a structure or union":

type1ViewController *viewController = [[type1ViewController alloc] initWithNibName:@"Type1View" bundle:nil];
self.type1ViewController = viewController;
self.type1ViewController.parentViewController = self;
[self.view insertSubview:viewController.view atIndex:0];
[viewController release];

I don't see why it should be different. What does the compiler see that it does not like?
Thanks for your help in advance

Best Solution

When you call self.type1ViewController.parentViewController instead of viewController.parentViewController, it's giving you an error because you have self.type1ViewController declared as some superclass, not a type1ViewController. When the compiler looks at this declaration it's not going to find the parentViewController property, so it's giving you the error.

In the first example your viewController is still declared as a type1ViewController, so it works fine. It would actually still work in the second example if you cast it to a type1ViewController, but of course it's better just to change the declaration.