PDA

View Full Version : Loading a Sub View Programatically




Lakario
Nov 7, 2008, 12:40 AM
I created a nib in the Interface Builder with a sub view that I will use for loading dynamic content. The subview is wired to an IBOutlet and when a certain event occurs I went to load a specific control into that sub view. I tried instantiating the view I was going to use and then adding it as a subview of the the contentView IBOutlet, but all I get is an exception.


- (IBAction)sellItem {
if(sellView == nil) {
SellViewController *viewController = [[SellViewController alloc] initWithNibName:@"SellViewController" bundle:[NSBundle mainBundle]];
self.sellView = viewController;
[viewController release];
}

[self.sellView setItem:item];
[self.contentView addSubview:self.sellView];
}


My application crashes on that last line. (Which also happens to be producing a "warning: passing argument 1 of 'addSubview:' from distinct Objective-C type.". I tried casting it to a (UIView *) but that still gives me exceptions.

Exception:
2008-11-06 23:28:34.580 iPhoneHelloWorld[4069:20b] *** -[SellViewController superview]: unrecognized selector sent to instance 0x479480
2008-11-06 23:28:34.582 iPhoneHelloWorld[4069:20b] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[SellViewController superview]: unrecognized selector sent to instance 0x479480'



jnic
Nov 7, 2008, 04:00 AM
Heed the compiler warning, it's trying to tell you that you're attempting to cast a view controller to a view (sellView is a SellViewController). I think what you meant to do was:

[self.contentView addSubview:self.sellView.view];

Lakario
Nov 7, 2008, 08:52 AM
Heed the compiler warning, it's trying to tell you that you're attempting to cast a view controller to a view (sellView is a SellViewController). I think what you meant to do was:

[self.contentView addSubview:self.sellView.view];

Well that is good to know. I will give that a shot, thank you. :)