Loading a Sub View Programatically

Discussion in 'iPhone/iPad Programming' started by Lakario, Nov 6, 2008.

  1. macrumors member

    Joined:
    Oct 29, 2008
    #1
    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.

    Code:
    - (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'
     
  2. macrumors 6502a

    Joined:
    Oct 24, 2008
    Location:
    Cambridge
    #2
    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:

    Code:
    [self.contentView addSubview:self.sellView[B].view[/B]];
     
  3. thread starter macrumors member

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

Share This Page