Cannot push viewcontroller from appdelegate - ios

In my window base application I need to navigate to editorview from my appdelegate when i click on the second item of my tabbarcontroller it will shows an actionsheet with three option.
actionsheet works fine, it will shows an actionsheet if you choose the second item from tabbar.
But i need to push to the other view when i choose the first option of my action sheet
i declare my actionsheet in appdelegate.
i already implement the actionsheetdelegate
(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
the method is trigerred and it can shows if i do nslog. but it cannot push to the viewcontroller that i want..
here's my code:
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
if (buttonIndex == 0)
{
DetailViewController *v2 = [[DetailViewController alloc] init];
NSLog(#"PUSH TRIGER");
[self.window.rootViewController.navigationController pushViewController:v2 animated:YES];
}
}
NOTE: I already embed my tabbarcontroller and navigation controller in my storyboard

It is full of bad answer related to this issue.
I hope mine fits your requirements.
1.- Inside of
- (void)application:(UIApplication*)application didReceiveRemoteNotification:(NSDictionary*)userInfo
// 1.- First: instantiate the navigationController. Normally the rootviewcontroller
UINavigationController *navigationController = (UINavigationController *)self.window.rootViewController;
// 2.- Second: Instantiate Storyboard
// It might be tagged MainStoryBoard. Be careful if you've changed it
UIStoryboard *mainStoryboard = [UIStoryboard storyboardWithName:#"MainStoryboard" bundle: nil];
// 3.- Third instantiate the VC you want to navigate
// In my particular case it is called IniciarSliderViewController . Don't forget to import it in the appdelegate. Also pay attention to tagged correctly. In my case MenuSlider
IniciarSliderViewController *controller = (IniciarSliderViewController*)[mainStoryboard instantiateViewControllerWithIdentifier: #"MenuSlider"];
//4.- And last one, you can now push as you usually do in the VC
[navigationController pushViewController:controller animated:YES];
Don't hesitate to ask if you still have problems

Do like this
What did you assign VC for UITabBarController ? I bet UIViewController? Try assign UINavigationController instead of UIViewController
synthesize UINavigationController *navController;
self.navController = [[UINavigationController alloc] init];
SomeViewController *viewController = [[SomeViewController alloc] initWithNibName:#"SomeViewController" bundle:nil];
self.navController.viewControllers = [NSArray arrayWithObject:viewController];
UITabBarController *tabBarController = [[UITabBarController alloc] init];
[tabBarController setViewControllers:[NSArray arrayWithObjects:
self.navController, nil]];
Then on your actionsheet
[self.navController pushViewController:v2 animated:YES];
EDIT for storyboard
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"MainStoryboard" bundle:nil];
self.navController = (UINavigationController*) [storyboard instantiateViewControllerWithIdentifier:#"MyNavController"];
Hope that helps

instead of this self.window.rootViewController.navigationController
use UINavigationController object

Related

UINavigationBar was disappeared in iOS8 and the earlier version

The first ViewController is managed by a NavigationController, and it has a UIWebView. The WebView modules has click events to push a ViewController in the NavigationController.
But now, the ViewController which is init by code is normal, and which is init by storyboard is abnormal, the navigation bar was disappeared. It only occurs in iOS8 and earlier Version.
PS: The Init Code is like this:
Code:
ViewController *vc = [[ViewController alloc]init];
[self.navigationController pushViewController:vc animated:YES];
Storyboard:
ViewController *vc = [ViewController defaultViewControllerWithStoryboard:#"StoryboardName"];
[self.navigationController pushViewController:vc animated:YES];
Thank You.
MainViewController* presentController = [self.storyboard instantiateViewControllerWithIdentifier:#"StoryBoardidentifier"];
[self.navigationController pushViewController:presentController animated:YES];
if its not working try this Normally yourStoryBoardName is Main:
UIStoryboard* storyBoard = [UIStoryboard storyboardWithName:#"YourStoryBoardName" bundle:nil];
MainViewController* presentController = [storyBoard instantiateViewControllerWithIdentifier:#"StoryBoardIdentifier"];
[self.navigationController pushViewController:presentController animated:YES];

Cant find navigation controller to navigate another view controller

I have a custom UIPageViewController which contain 5 UIViewControllers, now I would like to navigate from one of those 5 UIViewController, But I can't because in those UIViewControllers have no UINavigationController. Can anyone suggest me , how I can navigate from one those 5 UIViewControllers to other UIViewController?
The code in your - [AppDelegate application:didFinishLaunchingWithOptions:]
might look like this (after checking your skipping condition of course):
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"Main" bundle:nil];
UIViewController *vc1 = [storyboard instantiateViewControllerWithIdentifier:#"MyAuth"]; //if you assigned this ID is storyboard
UIViewController *vc2 = [storyboard instantiateViewControllerWithIdentifier:#"Login"]; //if you assigned this ID is storyboard
UIViewController *vc3 = [storyboard instantiateViewControllerWithIdentifier:#"Details"];
NSArray *controllers = #[vc1, vc2, vc3];
UINavigationController *navController = (UINavigationController *)self.window.rootViewController;
[navController setViewControllers:controllers];
If you paste just this to - [AppDelegate application:didFinishLaunchingWithOptions:], you'll see that it works immediately.

How to go to particular viewcontroller from leftviewcontroller in IIViewDeck in storyboard?

I am having difficulty using IIViewDeck in my app. I am using storyboard. I have managed to bring leftviewcontroller which is a tableviewcontroller with cells that are supposed to segue to particular viewcontroller in the storyboard. How do I do this? I have put this in appdelegate.
UIStoryboard* mainStoryboard = [UIStoryboard storyboardWithName:#"Main" bundle: nil];
UIViewController *menuController = [mainStoryboard instantiateViewControllerWithIdentifier:#"mainSideNavMenu"];
UINavigationController* navigationController = (UINavigationController *) self.window.rootViewController;
self->viewDeckController = [[IIViewDeckController alloc] initWithCenterViewController:navigationController leftViewController:menuController rightViewController:nil];
self.window.rootViewController = self->viewDeckController;
You can manage it with code, with the didSelectRowAtIndexPath method of your table view.
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
UIStoryboard* sb = [UIStoryboard storyboardWithName:#"Main" bundle:nil];
if (indexPath.row == 0) {
MyTripsViewController* newController = [sb instantiateViewControllerWithIdentifier:#"ViewController1"];
[self.viewDeckController closeLeftViewBouncing:^(IIViewDeckController *controller) {
self.viewDeckController.centerController = newController;
}];
}
if (indexPath.row == 1) {
MyTripsViewController* newController = [sb instantiateViewControllerWithIdentifier:#"ViewController2"];
[self.viewDeckController closeLeftViewBouncing:^(IIViewDeckController *controller) {
self.viewDeckController.centerController = newController;
}];
}
else {
MyTripsViewController* newController = [sb instantiateViewControllerWithIdentifier:#"ViewController3"];
[self.viewDeckController closeLeftViewBouncing:^(IIViewDeckController *controller) {
self.viewDeckController.centerController = newController;
}];
}
}
So you have to defined you row / viewController couple.
I know it's a bit late, but someone might find the answer useful at some point.
Max's answer is generally a good answer. However, the original poster's question is a bit vague. He/She didn't specify if the transition is required in the center view controller or the left one.
Assuming the transition is in the left view controller:
If you want push transition, you can use a code like:
- (void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
if (indexPath.row == 0) {
UINavigationController * nc = (UINavigationController *)self.viewDeckController.leftController;
UIViewController * vc = [[UIStoryboard storyboardWithName:#"Main" bundle:nil] instantiateViewControllerWithIdentifier:#"yourView"]; // use the name of the view (not the navigation view controller)
[nc pushViewController:vc animated:YES];
}
}
If you want Modal transition, just replace the code within the previous if statement clause with the following code:
UIViewController * vc = [[UIStoryboard storyboardWithName:#"Main" bundle:nil] instantiateViewControllerWithIdentifier:#"yourViewName"]; // don't use the name of a navigation controller
UINavigationController * nc = [[UINavigationController alloc] initWithRootViewController:vc];
[self presentViewController:nc animated:YES completion:nil];
The navigation controller in this code is used to get the navigation bar shown. If you don't need the navigation bar, just omit the navigation controller definition and pass the view controller as a parameter instead in the presentViewController: animated: completion: method.
Assuming the transition is in the center view controller:
You can use the code Max had provided. Keep in mind that the way he showed the new view controller is by replacing the existing center view controller. The new view controller is neither pushed nor modally showed. If you want to push new view controller to the existing center view controller, you should use the same way I introduced the transition in the previous two pieces of code above. So, it should look like this:
Push Transition: (the following code should replace the code within the if statement clause in the first piece of code)
[self.viewDeckController closeLeftView];
UINavigationController * nc = (UINavigationController *)self.viewDeckController.centerController;
[nc pushViewController:[[[UIStoryboard storyboardWithName:#"Main" bundle:nil] instantiateViewControllerWithIdentifier:#"yourViewName"] parentViewController] animated:YES];
Modal Presentation: (the following code should replace the code within the if statement clause in the first piece of code)
[self.viewDeckController closeLeftView];
UINavigationController * center = (UINavigationController *)self.viewDeckController.centerController;
UIViewController * vc = [[UIStoryboard storyboardWithName:#"Main" bundle:nil] instantiateViewControllerWithIdentifier:#"yourViewName"];
UINavigationController * nc = [[UINavigationController alloc] initWithRootViewController:vc];
[center presentViewController:nc animated:YES completion:nil];

pop to TableViewController from Detail Controller

I have a ViewController1 which can do a push segue into a ViewController2. This, in turn, can do a push segue into a ViewController3. I have a UIBarButtonItem in the ViewController1 that can do a push segue into the ViewController3. However, when I press the back button in the ViewController3, I need it to pop to ViewController2.
How would I do this? Here's the code I tried:
ViewController2 *VC2 = [[ViewController2 alloc] init];
ViewController3 *VC3 = [[ViewController3 alloc] init];
[self.navigationController pushViewController:VC2 animated:NO];
[self.navigationController pushViewController:VC3 animated:YES];
Tried your solution, and seems to be working for me,
but, anyway.. if for any reason it doesn't work for you... try with this:
NSArray *array = self.navigationController.viewControllers;
[self.navigationController setViewControllers:[array arrayByAddingObjectsFromArray:#[vc1,vc2]] animated:YES];
Here's what I finally found, based on the answer from #Camo. I present ViewController3 from ViewController1 via. Segue. Then, in the ViewController3 viewDidLoad, I put this code:
NSArray *array = self.navigationController.viewControllers;
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"MainStoryboard" bundle:nil];
id TVC = [storyboard instantiateViewControllerWithIdentifier:#"VC2"];
[self.navigationController setViewControllers:[array arrayByAddingObjectsFromArray:#[TVC, self]] animated:NO];

ViewDeck removes navigation bar when setting center controller

I am currently using ViewDeck with Storyboards, and have the following setup in the application didFinishLaunchingWithOptions:
//View Deck Setup
UIStoryboard* mainStoryboard = [UIStoryboard storyboardWithName:#"MainStoryboard" bundle: nil];
UIViewController* menuController = [mainStoryboard instantiateViewControllerWithIdentifier:#"LeftSideMenu"];
UINavigationController* navigationController = (UINavigationController *) self.window.rootViewController;
self.viewDeckController = [[IIViewDeckController alloc] initWithCenterViewController:navigationController leftViewController:menuController rightViewController:nil];
self.window.rootViewController = self.viewDeckController;
However, when I am setting a new CenterController from my MenuViewController, the navigation bar is removed, even if loading the same center view controller as I was previously looking at.
- (IBAction)viewUsers:(UIButton *)sender
{
UIStoryboard* mainStoryboard = [UIStoryboard storyboardWithName:#"MainStoryboard" bundle: nil];
UIViewController* viewController = [mainStoryboard instantiateViewControllerWithIdentifier:#"middleViewController"];
[self.viewDeckController setCenterController:viewController];
[self.viewDeckController closeLeftView];
}
What am I doing incorrectly?
The solution is remove any "deck constructor" class between AppDelegate and you TabBar. When you need to set or change Deck structure I create a several methods for each one structure. For example, if you want Deck has "leftView" view controller in left side, "rightView" in the right and "tabBarHome" in center, create a method like this in App Delegate:
-(void) lanzarHome{
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"Main" bundle:nil];
IIViewDeckController* deckController =[[IIViewDeckController alloc] initWithCenterViewController:[storyboard instantiateViewControllerWithIdentifier:#"tabBarHome"]
leftViewController:[storyboard instantiateViewControllerWithIdentifier:#"leftView"]
rightViewController:[storyboard instantiateViewControllerWithIdentifier:#"rightView"]];
deckController.leftSize = 100;
self.window.rootViewController = deckController;
[self.window makeKeyAndVisible];
}
Now, you must call this method from your viewController with sentence like this:
[((VKAppDelegate*) [[UIApplication sharedApplication] delegate]) lanzarHome];
The most important think is that create the method that changes or inits Deck structure from AppDelegate and never you have another class or viewController between delegate and center TabBar.
I hope this answer solve your issue. For me was a very hard work to find out this issue.

Resources