开发者

Dynamically load ViewController from TableView Row Selection

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!


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.


Add

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

after:

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

It might not be what you think.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜