Last active
December 14, 2015 02:18
-
-
Save lamprosg/5012212 to your computer and use it in GitHub Desktop.
(iOS) Initializing View Controllers. Delegate file - Single & Multi View examples
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| //Import your root controller to the .h | |
| //And set your property of your controller | |
| //@property (strong, nonatomic) BIDViewController *switchViewController; | |
| @implementation BIDAppDelegate | |
| - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions | |
| { | |
| //Create the main window screen | |
| self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; | |
| /*****************************************************************************************/ | |
| //intialize the root controller, with the the controller's nib name file. | |
| self.switchViewController = [[BIDSwitchViewController alloc] initWithNibName:@"SwitchView" bundle:nil]; | |
| //If you need to change the view’s geometry so it does not get hidden behind the status bar. | |
| UIView *switchView = self.switchViewController.view; | |
| CGRect switchViewFrame = switchView.frame; | |
| switchViewFrame.origin.y += [UIApplication sharedApplication].statusBarFrame.size.height; | |
| switchView.frame = switchViewFrame; | |
| //Geometry changed | |
| //Make switchViewController the root controller | |
| self.window.rootViewController = self.switchViewController; | |
| /*****************************************************************************************/ | |
| //If you want to change the background color | |
| self.window.backgroundColor = [UIColor whiteColor]; | |
| //Already there | |
| [self.window makeKeyAndVisible]; | |
| return YES; | |
| } | |
| //HERE WE HAVE TO PUT CODE TO THE ROOT CONTROLLER SO IT LOADS SUBVIEWS OF THE OTHER | |
| //CONTROLLERS WE WANT TO SHOW, IN THE viewDidLoad METHOD | |
| //. | |
| //SEE switchViewController.mm BELOW |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| //Import your controller to the .h | |
| //And set your property of your controller | |
| //@property (strong, nonatomic) BIDViewController *viewController; | |
| @implementation BIDAppDelegate | |
| - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions | |
| { | |
| //Create the main window screen | |
| self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; | |
| //Initialize the controller | |
| self.viewController = [[BIDViewController alloc] initWithNibName:@"BIDViewController" bundle:nil]; | |
| //Set the root Controller to your root controller if any | |
| self.window.rootViewController = self.viewController; | |
| //Already there | |
| [self.window makeKeyAndVisible]; | |
| return YES; | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /************************************************************** | |
| Import all your controllers with their properties | |
| ex. of 2 view controllers | |
| @property (strong, nonatomic) BIDYellowViewController *yellowViewController; | |
| @property (strong, nonatomic) BIDBlueViewController *blueViewController; | |
| **************************************************************/ | |
| - (void)viewDidLoad | |
| { | |
| [super viewDidLoad]; | |
| // Do any additional setup after loading the view. | |
| /*******************************************************************/ | |
| //Override a UIViewController method that is called when the nib is loaded | |
| self.blueViewController = [[BIDBlueViewController alloc] initWithNibName:@"BlueView" bundle:nil]; | |
| //Insert it as a subview of the root view so the toolbar will be shown | |
| [self.view insertSubview:self.blueViewController.view atIndex:0]; | |
| /*******************************************************************/ | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| //Action that switches between 2 views | |
| //Here free any memory you can by adding code to this method | |
| - (void)didReceiveMemoryWarning | |
| { | |
| [super didReceiveMemoryWarning]; | |
| // Dispose of any resources that can be recreated. | |
| /*******************************************************************/ | |
| //Check which view is shown and release the other one | |
| if (self.blueViewController.view.superview == nil) | |
| { | |
| //Release the controller by adding nil at its property | |
| self.blueViewController = nil; | |
| } | |
| else | |
| { | |
| self.yellowViewController = nil; | |
| } | |
| /*******************************************************************/ | |
| } | |
| //The actual action | |
| - (IBAction)switchViews:(id)sender | |
| { | |
| //Animate the transition | |
| [UIView beginAnimations:@"View Flip" context:nil]; | |
| [UIView setAnimationDuration:1.25]; | |
| [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut]; | |
| //Check if yellowView is showing. Either not loaded at all or | |
| //not in the view hierarchy, since it won't have a superview | |
| if (self.yellowViewController.view.superview == nil) //if not | |
| { | |
| //If yellowView hasn't been loaded yet, load it | |
| if (self.yellowViewController == nil) | |
| { | |
| self.yellowViewController = [[BIDYellowViewController alloc] initWithNibName:@"YellowView" bundle:nil]; | |
| } | |
| [UIView setAnimationTransition:UIViewAnimationTransitionFlipFromRight forView:self.view cache:YES]; | |
| //Remove bluw view | |
| [self.blueViewController.view removeFromSuperview]; | |
| //Show yellowView | |
| [self.view insertSubview:self.yellowViewController.view atIndex:0]; | |
| } | |
| else //else the opposite | |
| { | |
| if (self.blueViewController == nil) | |
| { | |
| self.blueViewController = | |
| [[BIDBlueViewController alloc] initWithNibName:@"BlueView" bundle:nil]; | |
| } | |
| [UIView setAnimationTransition:UIViewAnimationTransitionFlipFromLeft forView:self.view cache:YES]; | |
| [self.yellowViewController.view removeFromSuperview]; | |
| [self.view insertSubview:self.blueViewController.view atIndex:0]; | |
| } | |
| [UIView commitAnimations]; | |
| } |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
[self.view addSubview:self.ViewController.view];
There are also:
[self.view sendSubviewToBack:self.ViewController.view]
and
[self.view bringSubviewToFront.ViewController.view]
to manage which one the user sees, if you have to keep both views around without destroying them