Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3623960
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T23:26:53+00:00 2026-05-18T23:26:53+00:00

here is my code , i am trying to get from one view to

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-18T23:26:54+00:00Added an answer on May 18, 2026 at 11:26 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to play a sound file from an iPhone program. Here's the code:
I'm trying to get the education info from Facebook's graph API using stdclass. here's
I'm trying to make things simpler. Here is my code: If Threading.Monitor.TryEnter(syncRoot) Then Try
I am trying to subclass NSOutlineView. Here is my code: OutlineViewSublcass.h: #import <Cocoa/Cocoa.h> @interface
I'm kind of like stuck trying to implement YUI autocomplete textbox. here's the code:
I'm trying to do an Outer Join in Symfony. Here's the bit of code
See here: http://code.google.com/p/ie7-js/ Does anyone have any experience or remarks about this javascript? Is
I'm trying to implement the code under Asynchronous Exchanges from this link in the
I'm trying to port some C++ code to C#, and one of the things
Edit: The code here still has some bugs in it, and it could do

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.