views:

5610

answers:

2

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

+4  A: 

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.

Marc Charbonneau
While it's not obvious, I was hoping people would figure it out, but I should make it clear that this code is in the "ParentViewController" class that contains a type1ViewController property of class Type1ViewController. Type1ViewController is declared with a property parentViewController of type ParentViewController.so in that case, I'm not sure I totally understand you answer. Is it just the double '.' syntax that bothers the compiler? In the header file of ParentViewController, self.type1ViewController is declared as a Type1ViewController. what would be casted to make it work that way?
A: 

If the type1ViewController property of ParentViewController is declared with a class of Type1ViewController, then the first line should read:

Type1ViewController *viewController = [[type1ViewController alloc] initWithNibName:@"Type1View" bundle:nil];

not:

type1ViewController *viewController = [[type1ViewController alloc] initWithNibName:@"Type1View" bundle:nil];

Note the capitalization. I'm actually a bit surprised this compiled without errors or warnings.

cduhn