UINavigationController could't push two vc at same time - ios

UIViewController *vc1 = [[UIViewController alloc] init];
vc1.view.backgroundColor = [UIColor redColor];
UIViewController *vc2 = [[UIViewController alloc] init];
vc2.view.backgroundColor = [UIColor blueColor];
[self.navigationController pushViewController:vc1 animated:YES];
[self.navigationController pushViewController:vc2 animated:YES];
This is my code, when i run this , i get a red ViewController rather than a blue one. So i want to know what's problem is?

You're pushing two controllers with animation, in which case, during the animation of the first one, you can't push the second one.
Either push the first one without the animation, or just use setViewControllers: method.

Related

Panel sliding from the bottom

I want to implement panel, that by default appears from the bottom only by 1/4.
And when i tap on this part, or swipe it, it should roll out on full screen.
exactly what i want was implemented in this app
So what i tried already:
Making UIView and trying to apply tap and swipe gestures. UIView didn't catch it. But it caught touchesMoved, touchesEnded etc.
UITableView. Same.
Making UIViewController. Ended without any idea how to handle it to right way.
So any ideas or links are appreciated.
Thanks in advance.
Check PPRevealSideViewController https://github.com/ipup/PPRevealSideViewController, I think you can find what you need.
When you tap on the new viewController you can change the Offset.
AppDelegate.m
MainViewController *main = [[MainViewController alloc] init];
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"Main" bundle:nil];
main = [storyboard instantiateViewControllerWithIdentifier:#"MainViewControllerr"];
UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:main];
_revealSideViewController = [[PPRevealSideViewController alloc] initWithRootViewController:nav];
_revealSideViewController.delegate = self;
self.window.rootViewController = _revealSideViewController;
MainViewController.m
-(void)popViewController{
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"Main" bundle:nil];
PoppedViewController *pvc = [storyboard instantiateViewControllerWithIdentifier:#"PoppedViewController"];
[self.revealSideViewController pushViewController:pvc onDirection:PPRevealSideDirectionBottom withOffset:_offset animated:_animated completion:nil];
}
PoppedViewController.m
-viewDidLoad{
//...
UITapGestureRecognizer *gesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(getFullView)];
gesture.delegate = self;
[self.view addGestureRecognizer:gesture];
}
-getFullView{
[self.revealSideViewController changeOffset:0.0f forDirection:PPRevealSideDirectionLeft];
}

UINavigationController issue - if i return to first view, i see the color of second view

I am creating UINavigationController and it's UIViewControllers like this. First with yellow color, second with blue color.
Then adding it to stack. Yellow is displayed for a while, then the second blue.
What's the problem - if i press the button in top bar to get back to previous Controller, i don't see yellow background but blue, thought the title of window is "One" which is correct.
Why is this happening? Thx
UIViewController *one = [[UIViewController alloc] init];
[one.view setBackgroundColor:[UIColor yellowColor]];
[one setTitle:#"One"];
UIViewController *two = [[UIViewController alloc] init];
[two.view setBackgroundColor:[UIColor blueColor]];
[two setTitle:#"Two"];
UINavigationController * navController = [[UINavigationController alloc] init];
[self.view addSubview:navController.view];
[navController pushViewController:one animated:YES];
[navController pushViewController:two animated:YES];
I figured out what's the reason.
Because navController was relased from the memory.

navigation controller without using story boards

SecondViewController *testAppViewController2Obj =[[SecondViewController alloc]initWithNibName:#"TestAppViewController2" bundle:nil];
[self.navigationController pushViewController:testAppViewController2Obj animated:YES];
ContainerViewController *container = [[ContainerViewController alloc] init];
UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:container];
UINavigationBar *navBar = [[UINavigationBar alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, 65)];
container.navigationItem.title = #"Frames";
self.window.rootViewController = nav;
i hope it works
In order to push a new view controller you should first create a view controller, VC1, link it with a navigation controller and then link the navigation controller to the rootViewController in the AppDelegate
#AppDelegate
FirstViewController *VC1 = [FirstViewController new];
UINavigationController *nVC = [UINavigationController alloc] initWithRootViewController:VC1];
self.window.rootViewController = nVC;
Then when VC1 gets pushed, you can create another view controller and push it like this:
#Now in VC1
SecondViewController *VC2 = [SecondViewController new];
[self.navigationController pushViewController:VC2 animated:YES];

Best approach to switch between TabBarController and ViewController

I have been trying an app with initial login screen which then takes to a TabBarController.
I want to know whats the best approach to do this any example code would be appreciated. I have tried it but I am unable to switch from ViewController to TabController.
I'm not sure that this is the best way to do it, but it's quick and dirty and works. Present a modal view controller inside your applicationDidFinishLaunchineWithOptions: method. You should replace the #selector with something more appropriate to what you want to do. Background color is for effect only.
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
UIViewController *viewController1 = [[FirstViewController alloc] initWithNibName:#"FirstViewController" bundle:nil];
UIViewController *viewController2 = [[SecondViewController alloc] initWithNibName:#"SecondViewController" bundle:nil];
self.tabBarController = [[UITabBarController alloc] init];
self.tabBarController.viewControllers = #[viewController1, viewController2];
self.window.rootViewController = self.tabBarController;
[self.window makeKeyAndVisible];
// ***** The relevant code *****
UIViewController *viewController = [[UIViewController alloc] init];
UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:viewController];
[[viewController view] setBackgroundColor:[UIColor redColor]];
UIButton *dismissButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[dismissButton setFrame:CGRectMake(10, 10, 300, 44)];
[dismissButton setTitle:#"Dismiss" forState:UIControlStateNormal];
[dismissButton addTarget:[self tabBarController] action:#selector(dismissModalViewControllerAnimated:) forControlEvents:UIControlEventTouchUpInside];
[[viewController view] addSubview:dismissButton];
[[self tabBarController] presentViewController:navigationController animated:NO completion:nil];
return YES;
}
I normally wouldn't like to put this sort of code in the app delegate, but if it's a one-time thing like login details, maybe it's ok.
Assume your root view controller is also your login view.
Now from your root view controller, you can present the tab bar controller a number of ways. One way is to just call the presentViewController method from the root view controller.
Setup
From within the root view controller, sometime before presenting the tab bar, set it up:
myTabBarViewController = [[MyTabBarViewController alloc] init];
[myTabBarViewController setModalPresentationStyle:UIModalPresentationFullScreen];
[myTabBarViewController setModalTransitionStyle:UIModalTransitionStyleFlipHorizontal];
[myTabBarViewController setRootTabBarDelegate:self];
Presentation
When you are ready to present, just call this:
[self presentViewController:myTabBarViewController animated:YES completion:nil];
Notes
The view controller hierarchy looks like this:
AppDelegate
L RootViewController
L MyTabBarController

UIPopoverController with UIPageViewController shows no content

I try to show views of different view controllers within a popover controller's view. I tried for three hours now but without success. Basically this is what I've done:
- (IBAction)buttonTapped:(id)sender {
NSDictionary *options = [NSDictionary dictionaryWithObject: [NSNumber numberWithInteger:UIPageViewControllerSpineLocationMin] forKey:UIPageViewControllerOptionSpineLocationKey];
UIPageViewController *pageVC = [[UIPageViewController alloc] initWithTransitionStyle:UIPageViewControllerTransitionStylePageCurl navigationOrientation:UIPageViewControllerNavigationOrientationHorizontal options:options];
UIStoryboard *sb = [UIStoryboard storyboardWithName:#"MainStoryboard_iPad"
bundle:[NSBundle mainBundle]];
UIViewController *firstVC = [sb instantiateViewControllerWithIdentifier:#"FirstTableViewController"];
UIViewController *secondVC = [sb instantiateViewControllerWithIdentifier:#"SecondViewController"];
NSArray *viewControllers = [[NSArray alloc] initWithObjects:firstVC, secondVC, nil];
[pageVC setViewControllers:viewControllers direction:UIPageViewControllerTransitionStylePageCurl animated:NO completion:nil];
UIButton *button = sender;
UIPopoverController *popover = [[UIPopoverController alloc] initWithContentViewController:pvc];
[popover presentPopoverFromRect:button.frame inView:self.view permittedArrowDirections:UIPopoverArrowDirectionUp animated:YES];
}
And this is what I get to see:
What am I missing here?
Ok, if someone encounters a similar problem, I solved it:
It seems that right after initialization you have to set exactly the number of view controllers that are needed at a time.
So, if you create a UIPageViewController with one page displayed at a time, put an array containing exactly(!) one view controller in setViewControllers:direction:animated:completion:. More than one caused my controller to display nothing. Other view controllers can later be added lazily by implementing the UIPageViewControllerDelegate and UIPageViewControllerDataSource protocols.

Resources