简体   繁体   中英

Not able to release view Controller/ cause EXC_BAD_ACCESS

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

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.

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

You did not initialize the view controller. The iOS allocated space for the view controller but you did not initialized it.

LoginViewController_iPhone * login_view  = [[LoginViewController_iPhone alloc] init];

And why are you calling [super.window addSubview:login_view.view]; and not [window addSubview:login_view.view]; ?

The super of your appDelegate is a NSObject , it does not have an window property.

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.

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM