pop to TableViewController from Detail Controller - ios

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];

Related

How to navigate to UIViewController which is not present in navigationStack

I have three UIViewControllers. I am navigating to VC3 from VC1. I want to navigate to VC2 if I click on cancel or done button from VC3.
I have added the VC2 controller to navigation stack programmatically.
DocListViewController *temp1 = [[DocListViewController alloc] initWithNibName:#"DocListViewController" bundle:nil];
[self.navigationController addChildViewController: temp1];
for (UIViewController *controllers in self.navigationController.viewControllers) {
if([controllers isKindOfClass: [DocListViewController class]]) {
DocListViewController *VC2ViewController = (DocListViewController*)controllers;
[self.navigationController popToViewController:VC2ViewController animated:YES];
}
}
What you are doing wrong here is adding it as child view controller,addChildViewController as it as a child of the current view controller does not add it in the navigation stack.
You need to insert the controller in you navigation stack before doing popToViewcontroller.
VC2 *temp1 = [[VC2 alloc] initWithNibName:#"DocListViewController" bundle:nil];
NSMutableArray *vcArray = [NSMutableArray arrayWithArray:self.navigationController.viewControllers] ;
[vcArray insertObject:temp1 atIndex:1]; // ** This index is `1` assuming you only have 2 controllers and we are pushing it in the middle,
// if you have many vc in navigation stack and just want to insert a new vc just before your current vc go with this:
/*
[vcArray insertObject:temp1 atIndex:vcArray.count - 2];
*/
self.navigationController.viewControllers = vcArray;
/* --- ** This part is also not needed:
for (UIViewController *controllers in self.navigationController.viewControllers) {
if([controllers isKindOfClass: [DocListViewController class]]) {
[self.navigationController popToViewController: controllers animated:YES];
}
}
*/
[self.navigationController popViewControllerAnimated:true];
Also: you don't need VC2 *VC2ViewController = (VC2*)controllers;
inside the if block as you only need a UIViewController type object for pop-ing.
I have just edited your code, get the project reference below
Edit:
Adding GitHub link for better reference:
PlayingWithNavigation
You can navigate to VC2 using pushviewContoller
NSString * storyboardName = #"Main";
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:storyboardName bundle: nil];
VC2 * VC2View = [storyboard instantiateViewControllerWithIdentifier:#"VC2ID"]; // "VC2ID" is the storyboard Id of your view controller
[self.navigationController pushViewController:VC2View animated:YES];
UIViewController *vc2 = [[UIViewController alloc] init];
UIViewController *vc3 = [[UIViewController alloc] init];
[self.navigationController pushViewController:vc2 animated:NO];
[self.navigationController pushViewController:vc3 animated:YES];
Try this.

Click new view controller on button click In IOS

I have a button action in which i'm trying to open view controller on condition bases, but the view controller are not opening. I have use story board identifier method to open the new view controller, i'm confused why it isn't opening. My condition is not working fine, i got null but when i got login and comes back to home screen again it again shows null.
My code is,
- (IBAction)History:(id)sender {
NSString *savedValue = [[NSUserDefaults standardUserDefaults]
stringForKey:#"pin"];
if ([savedValue length]==0) {
NSLog(#"INNNNN");
UIStoryboard *story=[UIStoryboard storyboardWithName:#"Main" bundle:nil];
RegisterViewController *vc = [story instantiateViewControllerWithIdentifier: #"Register"];
[self.navigationController pushViewController:vc animated:YES];
}else{
UIStoryboard *story=[UIStoryboard storyboardWithName:#"Main" bundle:nil];
DetailsViewController *vc = [story instantiateViewControllerWithIdentifier: #"detail"];
[self.navigationController pushViewController:vc animated:YES];
}
}
The story board looks like this,
if your All viewcontrollers does not embed in UINavigationcontroller (Root) then use presentViewController for navigation.
- (IBAction)History:(id)sender {
NSString *savedValue = [[NSUserDefaults standardUserDefaults]
stringForKey:#"pin"];
UIStoryboard *story=[UIStoryboard storyboardWithName:#"Main" bundle:nil];
UIViewController *topViewVC;
if ([savedValue length]==0) {
RegisterViewController *vc = [story instantiateViewControllerWithIdentifier: #"Register"];
topViewVC = vc;
}else{
DetailsViewController *vc = [story instantiateViewControllerWithIdentifier: #"detail"];
topViewVC = vc;
}
[self presentViewController:topViewVC animated:true completion:nil];
}
option 2
if you want the pushViewController may be future purpose then dont do any changes in your code , just add UINavigationcontroller as root. Select your initial VC in storyboard then xcode menu --> Editor - -> Embed In --> Navigation Controller
It seems like you missed to embed your base view controller to navigation controller. Select your initial VC in storyboard. Go to Editor -> Embed In -> Navigation Controller.

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];

How to PushViewController From modalViewController

I have FavouriteViewController in which i have one button on click of button i am presenting a view modally called LoginViewController (using storyboard)
On this page(LoginViewController), i again have button, on click of that i want to push my view controller.
Is it possible ?
You can try below code.. It may help you to get your desired solution.
1) Present LoginViewController by write below code.
LoginViewController *login = [[[LoginViewController alloc]initWithNibName:#"LoginViewController" bundle:nil]autorelease];
UINavigationController *nc = [[[UINavigationController alloc] initWithRootViewController:login]autorelease];
nc.navigationBar.hidden = YES;
[self.navigationController presentModalViewController:nc animated:YES];
2) Now from LoginViewController, You can push your MyviewController as below.
MyviewController *adss = [[[MyviewController alloc]initWithNibName:#"MyviewController" bundle:nil]autorelease];
[self.navigationController pushViewController:adss animated:YES];
using UINavigationController
UINavigationController *nav=[[UINavigationController alloc]initWithRootViewController:self.LoginViewController];
[self presentModalViewController:nav animated:YES];
after this you can push any view controller from presented view controller(LoginViewController)
Here it is
[self.parentViewController.navigationController pushViewController:newViewController animated:YES];

Cannot push viewcontroller from appdelegate

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

Resources