views:

26

answers:

2

Hello

UIViewController *viewVC = nil;
NSString *TcodeNib = [selectedObject valueForKey:@"ViewControllerName"];
// Create the next-level view controller 
if ([TcodeNib isEqualToString:@"FirstViewController"]) {
    viewVC = [[FirstViewController alloc] initWithNibName:@"FirstViewController" bundle:nil];
} else if ([TcodeNib isEqualToString:@"SecondViewController"]) {
    viewVC = [[SecondViewController alloc] initWithNibName:@"SecondViewController" bundle:nil];
}
viewVC.referringObject = [selectedObject managedObjectContext];
viewVC.referringTransaction = referringObject;

However, I am getting the error when compiling the code as :

request for member 'referringObject' in something not a structure or union

I am getting the conditions to work properly when tested with fixed viewcontroller value... Any advice please!

A: 

Add

NSLog(@"Value of TcodeNib: %@",TcodeNib);

after:

NSString *TcodeNib = [selectedObject valueForKey:@"ViewControllerName"];

It might not be what you think.

Jordan
That would only help diagnose a problem at run time; it won't tell the questioner anything about a compiler error.
Peter Hosey
Thanks for the reply Joran, but I am getting the value as expected 2010-09-13 04:09:03.660 ApplicationDemo[6613:207] Value of TcodeNib: UserLookup
Accilies
+4  A: 
UIViewController *viewVC = nil;
[blah blah blah]
viewVC.referringObject = [selectedObject managedObjectContext];
viewVC.referringTransaction = referringObject;

However, I am getting the error when compiling the code as :

request for member 'referringObject' in something not a structure or union

That's because a UIViewController doesn't have a referringObject property. (Try compiling with Clang instead of GCC; it'll probably give you a clearer error message.) It doesn't have a referringTransaction property either, so if you cut out the first assignment, you'll get a similar error for the second one.

You declared viewVC as holding a pointer to a UIViewController, so that's all the compiler knows: That viewVC will hold either nil or a pointer to a UIViewController. It assumes nothing about what subclasses of UIViewController you might be instantiating. You said that that object is a UIViewController, so you can't assign to those properties of that object because a UIViewController doesn't have them.

You need to declare viewVC as holding a pointer to an object that does have those properties. Since you're going to store a pointer to an instance of one of two different classes (I assume both classes declare that their instances have those properties), there are two solutions:

  1. Make a protocol that declares the two properties, and make both FirstViewController and SecondViewController declare conformance to that protocol, and declare the variable as holding a pointer to an object that conforms to that protocol.
  2. Make an abstract subclass of UIViewController that declares and implements the two properties, and make both FirstViewController and SecondViewController subclasses of the abstract subclass, and declare the variable as holding a pointer to an instance of the abstract subclass.

I would probably do the latter, and move anything else that you currently have copied-and-pasted between First and SecondViewController into the abstract class as well.

Peter Hosey
Thanks Peter, thats exactly what I was not thinking about. I too have implemented the code by subclassing UiViewcontroller as per your point 2 and it works fine. Thanks for the help.
Accilies