Loading a Sub View Programatically

Discussion in 'iOS Programming' started by Lakario, Nov 6, 2008.

  1. Lakario macrumors member

    Oct 29, 2008
    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.

    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'
  2. jnic macrumors 6502a

    Oct 24, 2008
    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[B].view[/B]];
  3. Lakario thread starter macrumors member

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

Share This Page