开发者

Cocoa/iPad Inserting new view...what am I doing wrong

Sorry for wanting to ask a simple question.

I have my initial view loaded in the main view controller (TestViewController). The plan is to replace that view with the view from another controller (PageOneViewController). Both views live in the same Nib but are linked to outlets in their respective controllers.

When it comes to running the program, I press the button, the view disappears but nothing replaces it. I must have misunderstood something. By my understanding, it should work. What am I doing wrong?

@class PageOneViewController;

@interface TestViewController : UIViewController {

}

- (void) addPageOne;
- (IBAction) pressButton:(id)sender;

@end

...

#import "TestViewController.h"
#import "PageOneViewController.h"


@implementation TestViewController

- (IBAction)pressButton:(id)sender  {

 [self addPageOne];

}

- (void) addPageOne {

 PageOneViewController *pageOne = [PageOneViewController alloc];
 [self.view removeFromSuperview];
 [self.view insertSubview:pageOne.view atIndex:0]; 
 [pageOne release];   

}

- (void)dealloc
{...

...

@interface PageOneViewController : UIViewController {

IBOutlet UIView *view;

}

@property (nonatomic, retain) 开发者_Go百科UIView *view;

@end

...

#import "PageOneViewController.h"

@implementation PageOneViewController

@synthesize view;

@end


Calling removeFromSuperview causes the first view to no longer be displayed. Then you add the second view on top of the first view, but since view 1 still isn't being displayed the user can't see either one. You should either keep the first view visible and just cover it up with the second view, or, preferably, have a blank superview that contains either view1 or view2 as a subview depending on which one the program needs.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜