导航到 iPhone 中的另一个视图

发布于 2024-10-10 04:13:43 字数 880 浏览 0 评论 0原文

这是我的代码,我试图从一个视图到另一个视图,而不会出现任何内存泄漏。

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {    


    firstviewcontroller *first = [[firstviewcontroller alloc] init];
    [window addSubview:first.view];
    [self.window makeKeyAndVisible];

    return YES;
}

-(IBAction)gotosecondview:(id)sender
{ 
    secondviewcontroller *second = [[secondviewcontroller alloc] init];
    [self.view addSubview:second.view];
    [second release];
}

-(IBAction)gotofirstview:(id)sender
{
    [self.view  removeFromSuperview];
}

为了使上面的代码工作而不崩溃,我所要做的就是删除[第二个版本]。 如果我删除它,我会收到内存错误(构建和分析)。我该如何解决这个问题。我不想使用 [self.navigationController PushViewController:第二个动画:YES]; 我所做的就是从一个视图导航到另一个视图,反之亦然,而不使用导航控制器。我的firstviewcontroller和secondviewcontroller是UIViewController类型。

提前致谢。

here is my code , i am trying to get from one view to another without any memory leaks.

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {    


    firstviewcontroller *first = [[firstviewcontroller alloc] init];
    [window addSubview:first.view];
    [self.window makeKeyAndVisible];

    return YES;
}

-(IBAction)gotosecondview:(id)sender
{ 
    secondviewcontroller *second = [[secondviewcontroller alloc] init];
    [self.view addSubview:second.view];
    [second release];
}

-(IBAction)gotofirstview:(id)sender
{
    [self.view  removeFromSuperview];
}

to make the above code work without crashing , all i have to do is remove [second release].
if I remove it I get memory errors (build and analyze) . how can i solve this problem. and i dont want to use [self.navigationController pushViewController:second animated:YES];
all i am trying to do i navigating from one view to another and vice versa WITHOUT using navigation controller. my firstviewcontroller and secondviewcontroller are of type UIViewController.

Thanks in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

寄风 2024-10-17 04:13:43

您需要在显示视图时保持当前视图控制器处于活动状态(以便它可以处理用户输入等)。

在代码中,您可以通过多种方式实现这一点:

  • firstviewcontrollersecondviewcontroller 的实例保留为实例变量,并在 dealloc 上释放它们方法。
  • 为当前使用的 UIViewController 保留一个实例变量,并在切换到另一个视图时释放它。

第二个选项的代码如下所示:

@interface
    UIViewController *currentViewController;
@end

@implementation

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:    (NSDictionary *)launchOptions {

    firstviewcontroller *first = [[[firstviewcontroller alloc] init] autorelease];
    [self switchToViewController:first];

    [self.window makeKeyAndVisible];

    return YES;
}

- (void)switchToViewController:(UIViewController *)aViewController {
    [currentViewController.view removeFromSuperview];
    [currentViewController release];

    currentViewController = [aViewController retain];
    [self.window addSubview:currentViewController.view];
}

-(IBAction)gotosecondview:(id)sender { 
    [self switchToViewController:[[[secondviewcontroller alloc] init] autorelease]];
}

@end

这里,维持单个 UIViewController 活动的所有逻辑都位于 switchToViewController 方法中,该方法还处理切换逻辑从一种观点到另一种观点。作为额外的好处,您可以通过在 switchToViewController 中添加几行来快速添加对动画的支持。

You need to keep the current view controller alive while its view is showing (so it can process the user input, etc.).

In your code, you can achieve that in several ways:

  • Keep an instance of firstviewcontroller and secondviewcontroller as instance variables, and release them on the dealloc method.
  • Keep an instance variable with the currently in use UIViewController and release it when you switch to another view.

The code for the second option would look something like this:

@interface
    UIViewController *currentViewController;
@end

@implementation

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:    (NSDictionary *)launchOptions {

    firstviewcontroller *first = [[[firstviewcontroller alloc] init] autorelease];
    [self switchToViewController:first];

    [self.window makeKeyAndVisible];

    return YES;
}

- (void)switchToViewController:(UIViewController *)aViewController {
    [currentViewController.view removeFromSuperview];
    [currentViewController release];

    currentViewController = [aViewController retain];
    [self.window addSubview:currentViewController.view];
}

-(IBAction)gotosecondview:(id)sender { 
    [self switchToViewController:[[[secondviewcontroller alloc] init] autorelease]];
}

@end

Here, all the logic for maintaining a single UIViewController alive lies in the switchToViewController method, which also handles the logic for switching from one view to another. As an added bonus, you can quickly add support for animations by adding a couple of lines in switchToViewController.

黑寡妇 2024-10-17 04:13:43

您无法在通话中释放视图。
在这种情况下你只能做一件事。使用自动释放,

You can not release view in the call.
There is only one thing you can do in such conditions. use Autorelease,

别靠近我心 2024-10-17 04:13:43

[第二个版本] 导致代码崩溃的原因很可能是因为您正在释放视图控制器,而视图控制器又释放了第二个视图。 iPhone 食谱有一些关于切换/交换视图的示例代码(如果这就是您想要完成的全部任务)。这是链接。希望这有帮助!

链接文本

The reason [second release] is crashing your code is likely because you're releasing your view controller which in turn releases the second view. The iPhone cookbook has some sample code on switching/swapping views if that's all that you're trying to accomplish. Here's the link. Hope this helps!

link text

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