Closed farazhaider88 closed 10 years ago
swapViewControllers was called from my main class and i m passing tag of buttons from there.
Do you have a fork of this repository or something I could look at? If not, that's okay. I can use your code and add a third view controller. Are you triggering the swap with three different buttons or a single segmented button? Please forgive me if I'm not responsive over the weekend but I do want to take a look at this as soon as I can after the weekend.
i m using three different buttons. giving them different tags. using a class variable to hold the value so if you are on the same control as the tag than this function wont call but if not than this function called, i put value of animation to 0.0 but still it does not solve the problems. thanks for your cooperation.
this is how iam calling it:
(IBAction)tabbarButtonPressed:(id)sender { NSLog(@"%d",[sender tag]); int buttonTag = [sender tag];
if(segueIndex != buttonTag){ [self.topMenuImageView setImage:[UIImage imageNamed:[NSString stringWithFormat:@"topMenu%dSelected.png",buttonTag]]]; [self.containerViewController swapViewControllers : buttonTag]; segueIndex = buttonTag; }
segueIndex is local variable.
Okay, I started with the original example and added a third view controller and three buttons instead of just the one. The solution here is the result of a quick afternoon hack session and isn't all that elegant but it should work without timing issues. I created a threebuttons
branch with my solution. Hopefully, this helps. Let me know how it works out for you.
Hello ... forgive me if I open again this post but I have a problem ... I also need to work with three viewcontroller instead of two, but the difference for me is that I do not need three buttons to operate the following but using a single button ... Could you help me understand how I can add another ViewController not change the function of your original button?
i have customize your code to make it work for more than 2 viewcontrollers, it starts crashing when ever i start quickly moving on each controllers, can you please check what i m doing wrong here.
import "ContainerViewController.h"
import "VenueDetailViewController.h"
import "MenuViewController.h"
import "LocationViewController.h"
define SegueIdentifierFirst @"embedMain"
define SegueIdentifierSecond @"embedMenu"
define SegueIdentifierThird @"embedMap"
@interface ContainerViewController ()
@property (strong, nonatomic) NSString currentSegueIdentifier; @property (strong, nonatomic) VenueDetailViewController venueDetailController; @property (strong, nonatomic) MenuViewController venueMenuController; @property (strong, nonatomic) LocationViewController venueMapController; @property (assign, nonatomic) BOOL transitionInProgress;
@end
@implementation ContainerViewController
(void)viewDidLoad { [super viewDidLoad];
self.transitionInProgress = NO; self.currentSegueIdentifier = SegueIdentifierFirst; [self performSegueWithIdentifier:self.currentSegueIdentifier sender:nil]; }
(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender { // Instead of creating new VCs on each seque we want to hang on to existing // instances if we have it. Remove the second condition of the following // two if statements to get new VC instances instead. if (([segue.identifier isEqualToString:SegueIdentifierFirst]) && !self.venueDetailController) { self.venueDetailController = segue.destinationViewController; }
if (([segue.identifier isEqualToString:SegueIdentifierSecond]) && !self.venueMenuController) { self.venueMenuController = segue.destinationViewController; }
if (([segue.identifier isEqualToString:SegueIdentifierThird]) && !self.venueMapController) { self.venueMapController = segue.destinationViewController; }
// If we're going to the first view controller. if ([segue.identifier isEqualToString:SegueIdentifierFirst]) { // If this is not the first time we're loading this. if (self.childViewControllers.count > 0) { [self swapFromViewController:[self.childViewControllers objectAtIndex:0] toViewController:self.venueDetailController]; } else { // If this is the very first time we're loading this we need to do // an initial load and not a swap. [self addChildViewController:segue.destinationViewController]; ((UIViewController )segue.destinationViewController).view.frame = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height); [self.view addSubview:((UIViewController )segue.destinationViewController).view]; [segue.destinationViewController didMoveToParentViewController:self]; } } // By definition the second view controller will always be swapped with the // first one. else if ([segue.identifier isEqualToString:SegueIdentifierSecond]) { [self swapFromViewController:[self.childViewControllers objectAtIndex:0] toViewController:self.venueMenuController]; }
else if ([segue.identifier isEqualToString:SegueIdentifierThird]) { [self swapFromViewController:[self.childViewControllers objectAtIndex:0] toViewController:self.venueMapController]; }
}
(void)swapFromViewController:(UIViewController )fromViewController toViewController:(UIViewController )toViewController { toViewController.view.frame = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height);
[fromViewController willMoveToParentViewController:nil]; [self addChildViewController:toViewController];
[self transitionFromViewController:fromViewController toViewController:toViewController duration:0.0 options:UIViewAnimationOptionTransitionCrossDissolve animations:nil completion:^(BOOL finished) { [fromViewController removeFromParentViewController]; [toViewController didMoveToParentViewController:self]; self.transitionInProgress = NO; }]; }
(void)swapViewControllers :(int)segueTag { if (self.transitionInProgress) { return; }
self.transitionInProgress = YES;
switch (segueTag) { case 1: { self.currentSegueIdentifier = SegueIdentifierFirst; break; }
} [self performSegueWithIdentifier:self.currentSegueIdentifier sender:nil]; }