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

  • SEARCH
  • Home
  • 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 4093478
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T19:37:24+00:00 2026-05-20T19:37:24+00:00

I would like to switch between UIViews in a correct way. This is how

  • 0

I would like to switch between UIViews in a correct way.

This is how I currently do it :

  • I trigger a method when clicking on a button

[myButton addTarget:self
action:@selector(switchToMySecondView:)
forControlEvents:UIControlEventTouchUpInside];

  • In switchToMySecondView, I allocate my new UIViewController (mySecondViewController is a attribute of the current class) :

MySecondViewController* mySecondView = [[MySecondViewController alloc] initWithNibName:@"SecondViewXib" bundle:nil];
self.mySecondViewController = mySecondView;
[mySecondView release];

  • Add some stuff in mySecondViewController...

UILabel *myLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 10, 50)];
myLabel.text = [aGlobalArray objectAtIndex:[sender tag]];
[mySecondViewController.view addSubView:myLabel];

  • Next I display it, using a UIAnimation :

[UIView setAnimationTransition: UIViewAnimationTransitionFlipFromLeft forView:self.view cache:YES];
[mySecondViewController viewWillAppear:YES]; [self.view addSubview:mySecondViewController.view]; [mySecondViewController viewDidAppear:YES];
[UIView commitAnimations];

  • Finally, in my second view controller, I use the reverse method to switch back to my first view :

[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.40];
[UIView setAnimationDelegate:self];
[UIView setAnimationTransition: UIViewAnimationTransitionFlipFromLeft forView:self.view.superview cache:YES];
[self.view removeFromSuperview];
[UIView commitAnimations];

Ok. That works fine. I have no doubt there are memory leaks, but it's currently not my priority.

Here is my problem :

Each time I click on my button in the first view, I add a different UILabel to my second view (using the [sender tag] index in my aGlobalArray).
Each time this UILabel is added to my secondView, it overlays on the old UILabel, so I still can see the both UILabel.

UILabel is just an example. In my app I am also adding UIImages which overlay too.

I tried to write it in my secondView when switching back to my first view :

[UIView setAnimationTransition: UIViewAnimationTransitionFlipFromLeft forView:self.view.superview cache:YES];
[self.view removeFromSuperview]; [self release];
[UIView commitAnimations];

And my app is suddenly stopped after 2 or 3 switch/switch back, sometimes without ny message in the console, sometimes with a message saying I am releasing something not allocated.

What am I doing wrong ?

Any help would be greatly appreciated !

  • 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-20T19:37:24+00:00Added an answer on May 20, 2026 at 7:37 pm

    You shouldn’t be recreating the UILabel in the second view controller. You could either create the label in your MySecondViewController * class and expose it as a property. Then you’d simply do the following whenever you want to switch to the second view:

    mySecondViewController.theLabel.text = [aGlobalArray objectAtIndex:[sender tag]];
    

    Conversely, you could set a tag on the UILabel when you first create it and later retrieve the label using viewWithTag.

    EDIT: When I say expose as a property, you’d create the label as normal in your interface:

    @interface MySecondViewController : UIViewController {
        UILabel *label;
    }
    
    @property (nonatomic, retain) UILabel *label;
    

    The in the implementation file:

    @implementation MySecondViewController
    @synthesize label;
    

    Then you’d create the label only once – i.e. in MySecondViewController ViewDidLoad or instead of your:

    UILabel *myLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 10, 50)];
    myLabel.text = [aGlobalArray objectAtIndex:[sender tag]];
    [mySecondViewController.view addSubView:myLabel];
    

    you could do:

    if (mySecondViewController.label == nil) {
        mySecondViewController.label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 10, 50)];
        [mySecondViewController.view addSubView:self.mySecondViewController.label];
    }
    
    mySecondViewController.label.text = [aGlobalArray objectAtIndex:[sender tag]];
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I would like to switch between NUnit and VS Tests like this: #if !NUNIT
I have a few asp views that I would like to switch between with
I would like to implement a switch button, android.widget.Switch (available from API v.14). <Switch
I would like to create multiple grids and switch between them using a dropdown.
I am using a UINavigationController to switch between views. What I would like is
I load images in webview and would like to switch between them by fling
I currently use a left and right arrow to switch between images, but would
I have a ImageView, and I would like the picture to switch between two
I would like to switch from the debug version of the player to the
I have an existing model for my project. I would like to switch to

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.