Cocoa/iPad 插入新视图...我做错了什么

发布于 2024-11-06 12:12:16 字数 1118 浏览 0 评论 0原文

抱歉想问一个简单的问题。

我已将初始视图加载到主视图控制器(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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

坚持沉默 2024-11-13 12:12:16

调用 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文