Cocoa/iPad 插入新视图...我做错了什么
抱歉想问一个简单的问题。
我已将初始视图加载到主视图控制器(TestViewController)中。计划将该视图替换为另一个控制器 (PageOneViewController) 中的视图。两个视图都位于同一个 Nib 中,但链接到各自控制器中的插座。
当运行程序时,我按下按钮,视图消失,但没有任何东西可以替代它。我一定是误会了什么。根据我的理解,它应该有效。我做错了什么?
@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) UIView *view;
@end
#import "PageOneViewController.h"
@implementation PageOneViewController
@synthesize view;
@end
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) UIView *view;
@end
...
#import "PageOneViewController.h"
@implementation PageOneViewController
@synthesize view;
@end
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
调用
removeFromSuperview
会导致第一个视图不再显示。然后,您将第二个视图添加到第一个视图的顶部,但由于视图 1 仍未显示,因此用户看不到任何一个视图。您应该保持第一个视图可见并用第二个视图覆盖它,或者最好有一个空白的超级视图,其中包含 view1 或 view2 作为子视图,具体取决于程序需要哪个视图。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.