无法释放视图控制器/导致EXC_BAD_ACCESS [英] Not able to release view Controller/ cause EXC_BAD_ACCESS

查看:61
本文介绍了无法释放视图控制器/导致EXC_BAD_ACCESS的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我认为这很奇怪,但是相同的代码在其他应用程序中也可以正常工作,因此,我在继承继承了Super类(即Main委托)的iphone应用程序委托中重载了我的功能

I think this is weird but same code was working fine for me in other application So, I am overriding my function in iphone app delegate which inherits Super class that is Main delegate

       * .appdelegate_iPhone.h *
        @interface AppDelegate_iPhone : AppDelegate 
        {

        }

        @end

    * .appdelegate_iPhone.m *
........

        - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
        {
          LoginViewController_iPhone * login_view = [[LoginViewController_iPhone alloc] initWithNibName:@"LoginViewController_iPhone" bundle:nil];
                [super.window addSubview:login_view.view];
                [super.window makeKeyAndVisible];
               //[login_view release];


            // Override point for customization after application launch.
            [self.window makeKeyAndVisible];
            return YES;

        }
......
@end

如您所见,我注释掉了释放login_view的原因,因为如果我不释放该对象,那以后我将无法控制任何IBoutlet UIField.

As you can see that i commented out releasing login_view because if i don't and release the object, I am not able to Control any IBoutlet UIField after.

我的意思是,如果我释放login_view并尝试使用UITextField.text,则会出现exc_bad_access错误

I mean if i release login_view and try to use UITextField.text, i get exc_bad_access error

推荐答案

首先,使用内置模板及其nib文件通常会使所有这些操作变得更加简单,我强烈建议您不要尝试以此方式构建它.也就是说,让我们谈谈正在发生的事情.

First, using the built-in templates and its nib file generally makes all of this much simpler, and I highly recommend that rather than trying to build it this way. That said, let's talk about what's happening.

您要创建一个视图控制器,然后将其视图取出并放在窗口中.然后,您将丢弃正确分配的视图控制器.我不知道您将在哪里调用 UITextField.text ,但是在此方法完成时,视图控制器应该已经消失了.如果要保留视图控制器,则需要将其放在ivar中.

You're creating a view controller, then taking its view out and putting it in the window. You're then throwing away the view controller, which correctly deallocates. I don't know where you would call UITextField.text, but by the time this method completes, the view controller should be gone. If you want to hold onto the view controller, you need to put it in an ivar.

如@Cyupa所述,您不应在此处使用 super.window .您应该使用 self.window ,尽管理想情况下,如果可以的话,您应该使用其中一个模板.

As @Cyupa notes, you should not be using super.window here. You should be using self.window, though ideally you should use one of the templates instead if you can.

这篇关于无法释放视图控制器/导致EXC_BAD_ACCESS的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆