views:

3971

answers:

1

I'm trying to keep my code clean and keep the number of files down. I am using a UITableViewController and I would like to load another view on top of it. I thought this would be pretty simple:

(1) create an IBOutlet in my .h file

@interface MyViewController : UITableViewController {
    ...
    UIView *downloadView;
    ...
}
...
@property (nonatomic, retain) IBOutlet UIView *downloadView;
...

(2) link it to my view in IB

(3) do something like:

self.view = downloadView;

or

[self.view addSubview:self.downloadView];

But that doesn't work for me. If I do

[self.tableView removeFromSuperview];

Then the table view goes away but I don't know how to add my view from my nib. I'm doing everything for tableView programatically but I didn't think that would matter. And with UITableViewController subclassing UIViewController I thought it would be no problem using addSubview or something like that. What am I missing? This shouldn't be that hard right?

Update: So if I do the following:

UIView *newView = [[UIView alloc] initWithFrame:self.view.frame];
newView.backgroundColor = [UIColor greenColor];
[self.view addSubview:newView];

It does (mostly) what I want. So does this mean I'm messing something up with the way I'm connecting my view from IB? In IB I set the fileOwner's class to MyViewController and I make a connection between downloadView and the view that I created (in IB). That's all I should have to do right?

A: 

I think it's an issue of where in the hierarchy you're adding the view. You can try this:

[UIWindow addSubview:self.downloadView];

and see if it appears. Or perhaps,

[self.tableView addSubview:self.downloadView];

Otherwise I think you have the right idea.

Ken Pespisa
UIWindow doesn't like addSubview:*** +[UIWindow addSubview:]: unrecognized selector sent to class 0x31a5c4e0*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** +[UIWindow addSubview:]: unrecognized selector sent to class 0x31a5c4e0'And I also tried the second way and that doesn't do anything.
RyanJM
Sorry, the UIWindow code isn't literal. You'd need to replace the UIWindow part with an actual UIWindow object. Where in the Interface Builder hierarchy is the UIView? Is the UIView under MyViewController?
Ken Pespisa