Present nth View Controller without showing its root VC - ios

Let's say I have 3 controllers (A, B, C). A and C is ViewControllers, B is NavigationController. Normal application flow is A as root view, A present (modal) B, B push C.
What I want is to present C as top view controllers without going through all the animation from A-B-C but still have the hierarchy (means C can go back to A), is it possible?
We can set window rooViewController directly to C but it wont have the hierarchy
EDIT:
Maybe my question isnt clear enough, the main point here is, when I open my app, I want to show C directly but still have A->B->C view hierarchy so I can go back to A via normal pop and dismiss
EDIT2:
I manage to show C with B-C hierarchy, so I can pop back to B from C. Now my problem is how can I present B (NavigationController) from A (ViewController) so when I close B it will **dismiss* to A
EDIT3:
I saw some answer that use NavigationController, it works BUT not what I want because normally from A to B I use modal presentViewController and from B to A I use dismissViewController
EDIT4:
So far what I got is
self.window.rootViewController = vcA;
[self.window makeKeyAndVisible];
[vcA presentViewController:vcB animated:NO completion:nil];
[vcB pushViewController:vcC animated:NO];
this will give correct hierarchy that I want but it give fast animation (a blink) showing A and than C and also give warning Unbalanced calls to begin/end appearance transitions for <vcA: 0x7fcfa0cf9c50>.
EDIT5:
I endup ignoring the warning and stick with my prev answer (but still welcome for another solution). And for the blinking problem I use workaround below
uiview *overlay = [new uiview]; // using vcA.frame
overlay.backgroundColor = white; // I use dominant color of vcC
vcA addSubview:overlay;
self.window.rootViewController = vcA;
[self.window makeKeyAndVisible];
[vcA presentViewController:vcB animated:NO completion:^{
[overlay removeFromSuperview];
}];
[vcB pushViewController:vcC animated:NO];
This will disguise the blinking behavior so no one will notice (I hope :-p)

Use a UINavigationViewController and then call
setViewControllers(_:animated:)
Use this method to update or replace the current view controller stack without pushing or popping each controller explicitly. In
addition, this method lets you update the set of controllers without
animating the changes, which might be appropriate at launch time when
you want to return the navigation controller to a previous state.
If animations are enabled, this method decides which type of
transition to perform based on whether the last item in the items
array is already in the navigation stack. If the view controller is
currently in the stack, but is not the topmost item, this method uses
a pop transition; if it is the topmost item, no transition is
performed. If the view controller is not on the stack, this method
uses a push transition. Only one transition is performed, but when
that transition finishes, the entire contents of the stack are
replaced with the new view controllers. For example, if controllers A,
B, and C are on the stack and you set controllers D, A, and B, this
method uses a pop transition and the resulting stack contains the
controllers D, A, and B.
Let me know if it help you :)

push A and B and C like you normally would but do it by using presentViewController:? animated:NO and pushViewController:? animated:NO -- not animating is the clue
e.g. (mock code)
applicationDidFinishLaunching {
id a = [MyA new]; //root, presents b
id b = [MyA new]; //pushes c you said.. so it is or has a navigationController
id c = [MyA new];
[a presentViewController:b animated:NO];
b.navigationController pushViewController:c animated:NO];
}

To segue to any ViewController you want to: Have you drawn a custom segue in the storyboard by holding down the control key on your keyboard and clicking the ViewController you want the segue to be in? While still holding control, you can drag it to the viewcontroller you want to segue to. After that just let go and XCode will let you choose the type of segue you want: push, modal, or custom.
After that, click the visual segue reference that Xcode creates (looks like a big grey arrow in the storyboard that points to your viewcontrolelrs) and click the attributes inspector. Then look where it says identifier. From there you can name the segue anything you want and reference it programmatically. Just do the above and call the below message and you should be able to go to any viewcontroller when ever you want to.
[self performSegueWithIdentifier:#"ShowViewControllerA" sender:self];
Also, I agree with everyone else saying to set Viewcontroller C as root ViewController in the storyboard. PresentViewController is also a good idea. etc

Follow #Daij-Djan's answer:
applicationDidFinishLaunching {
id a = [MyA new]; //root, presents b
id b = [MyA new]; //pushes c you said.. so it is or has a navigationController
nav d = [[Nav alloc] initWithRoot:b];
id c = [MyA new];
[a presentViewController:d animated:NO];
b.navigationController pushViewController:c animated:NO];
}

Why don't you just present C on top of A with presentViewController?
EDIT:
A -> C:
In vcB I would add a boolean property indicating whether we are in the mentioned flow and present vbB in vcA this way:
// We are in vcA where you want to present vcB and vcC
vcB.transient = YES;
[vcA presentViewController:vcB animated:NO completion:^{
[vcB pushViewController:vcC animated:NO];
}];
C -> A
When you want to go back to vcA, popping vcC will call viewDidAppear in vcB.
// We are in vcB
- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
if(self.transient == YES) {
[self dismissViewControllerAnimated:NO completion:nil];
return;
}
}
With this solution when you go back from vcC to vcA you will temporary see vcB, as we have to wait for viewDidAppear to be called.
EDIT 2:
Alternatively if you don't need to directly go back from vcC to vbA, just use the first piece of code (no transient property required).

Keep A as rootVC(in applicationDidFinishLaunching). So when you open your app it will load A first.
Once A is loaded(in viewDidLoad) call a method to present B(keep animation = NO while presenting).
When B is loaded, call a method to push to C(keep animation = NO while pushing).
Hope this helps!

Related

Close own uiviewcontroller after segue to child uiviewcontroller ios?

I have three UIViewControllers (let's call them A, B and C) in a navigation controller. A can segue into either B or C. B can segue into C. When C closes, I want it to always return to A, i.e. it automatically closes B upon closing C, if opened from B.
Now, I tried using segue unwinding, so that when C closes, B's return method gets called to dismiss the destination controller:
- (IBAction)returnFromC:(UIStoryboardSegue*)segue
{
[segue.destinationViewController dismissViewControllerAnimated:YES completion:nil];
}
I put a breakpoint in this method - it is called. I have verified that the destinationController is indeed B. However, I noticed when the break point hits, C is still visible. After playing from the break point, C does exit as expected, but B is still visible.
Any ideas? Many thanks in advance.
When C closes, I want it to always return to A, i.e. it automatically closes B upon closing C, if opened from B
The simplest solution is: in C's viewDidAppear:, secretly remove B as a child of the navigation controller. In other words, you've got this:
A > B > C
Now you rearrange things so that you've got this:
A > C
Thus, the only thing to go back to from C is A.
It's easy to manipulate the navigation controller's set of children in this way. Just call setViewControllers:animated: (with a second argument of NO).
[But if I've understood your setup properly, another easy way would be to implement the unwind method in A and not B. Then do an unwind segue. We always unwind to the first view controller that contains the unwind method, so that would always be A.]
dismissViewControllerAnimated:completion: is used to dismiss a Modal segue and has no effect for Push segues
As #matt suggested you could just remove B (the middle) view controller from self.navigationController.viewControllers and here's a sample code that you can put in B view controller:
- (void)viewDidDisappear:(BOOL)animated {
[super viewDidDisappear:animated];
// No need to process if the viewController is already being removed
if (!self.isMovingFromParentViewController) {
// Getting a mutable copy of the viewControllers (can't directly modify)
NSMutableArray *temp = [self.navigationController.viewControllers mutableCopy];
// Removing 'self' -> so A > B > C will become A > C
[temp removeObject:self];
// Setting the new array of viewControllers
self.navigationController.viewControllers = [temp copy]; // getting an immutable copy
}
}
P.S. You could use popViewControllerAnimated: instead of Unwind segue if you're not planning to send the data back.

How to jump to any other controller in iOS?

My storyboard structure is like:
A is a navigation controller
B is a tab bar controller
C,D,E are view controllers
F,G,H,I,J are a view controllers
if now i am on I ,and there's a button the i pressed then I go back to C.How to do that?
I tried make segue between I and C, but C has a back button, you pressed it,you back to I.
I don't want that.when i came from I to C, i want C is as I first come to C from B.
if i want to go to H from I,I want H have a back button that you pressed and you back to F not to I.
From I to C: do popViewController twice, or loop through the navigationcontroller's viewcontrollers and find C, pop to C.
From H to I: Programmatically push to I. In storyboard set an storyboard ID for I, and you can create a I instance through [storyboard instantiateViewControllerWithIdentifier:ID];
I would set the viewControllers property of UINavigationController.
Create an array of View Controllers like you would desire the stack to be. Example: #{C, F, I} or in the case of your question you would use #{ C } then update the navigationController to contain these views.
[self.navigationController setViewControllers:(NSArray *)];
If you want to do something other than what your Segues are set up for, be sure to give your VC a stoyboard ID
Then using that storyboard ID, you can call this code to create/display;
-(IBAction)myButtonAction:(id)sender{
RDLaunchOptionsTableViewController *vc = [self.storyboard instantiateViewControllerWithIdentifier:#"RDLaunchOptionsTableViewController"];
vc.delegate = self;
[self.navigationController pushViewController:vc animated:YES];
}
You don't have to use a nav controller. Just use [self presentViewController....];

Modal View Controller, dismiss and pop back to view controller

Im trying to dismiss a ModaViewController called C, back to A. The ModalViewController is presented in B. Therefore the Navigation flow is A->B - (present ModalView) -> C. I am able to dismiss the ModalViewController back to B, but I am unable to pop back to A in the completion bracket. This is the code I have tried:
[self dismissViewControllerAnimated:YES completion:^{
[self.navigationController popToViewController:[[self.navigationController viewControllers] objectAtIndex:0] animated:YES];
}];
The ModalViewController is dismissed but does not pop back to A. I call this block of code in an IBAction.
Any advice?
On a second note, when I dismiss the ModalViewController all my UIPickers in Controller B are empty/ deallocated. I am using ARC as well.
The problem with your code is that self.navigationController will be nil. If you have a controller (A) embedded in a navigation controller, and that controller pushes to another controller (B) which then presents your last controller (C), then you need to do something like this,
-(IBAction)dismissToBThenPop:(id)sender {
UINavigationController *nav = (UINavigationController *)self.presentingViewController;
[self dismissViewControllerAnimated:YES completion:^{
[nav popViewControllerAnimated:YES];
}];
}
Even though you present C from B, the actual presentingViewController will be the navigation controller. This code will dismiss C then pop B, but you will see B for an instant before ut pops back to A. If you don't want to see this, then you should use an unwind segue to go directly back to A from C.
Your second problem about the pickers being empty and deallocated should not be happening under the scenario that you say you have. You will have to provide more information about what you're doing in B to solve that problem.
Create a protocol in ModalViewController, let's say ModalViewControllerDelegate with a method -(void)dismissTheModal, and make B implement this protocol. Before showing the ModalViewController, do modalViewController.delegate = self. When you're IBAction is called, do [self.delegate dismissTheModal], and in controller B you should do :
-(void)dismissTheModal {
[self dismissViewControllerAnimated:YES completion:^{
[self popViewController];
}];

chaining uiviewcontroller delegates

i have a uivewcontroller (let's call it A) that loads uiviewcontroller (B) setting it up as a delegate which i use to close B and continue code on A. There is also a scenerio where B leads to another uiviewcontroller C (again with a delegate). When C is closed i use it's delegate to return to B but in this scenario i also want B to be immediately dismissed and the code to return to A. Now, B->A works, and C->B works but doing C->B->A fails at B with an error:
"attempt to dismiss modal view controller whose view does not currently appear" it appears to be trying to dismiss view C again.
Both viewcontrollers are being dismissed with this code (though the code sits in different uiviewcontrollers)
[self dismissViewControllerAnimated:YES completion:nil];
Am I using delegates correctly for what i want, or should i be using a different process?
Code for option 1 (A->B, B<-A):
A -> B
scorer_turn *st = (scorer_turn *) segue.destinationViewController;
st.st_delegate=self;
st.league = _match.league;
st.match = _match;
st.leg = _leg;
st.set = _set;
B -> A
-(void)closeView{
[_st_delegate scorer_turn:self didFinish:YES];
}
-(void)scorer_turn:(scorer_turn *)controller didFinish:(BOOL)finish{
[self dismissViewControllerAnimated:YES completion:nil];
}
Code for option 2 (A->B, B->C, C->B->A):
as above plus:
B -> C
matchSummaryViewController *ms = (matchSummaryViewController *) segue.destinationViewController;
ms.match = _match;
ms.oneScreen = NO;
ms.delegate = self;
[[segue destinationViewController] setManagedObjectContext:self.managedObjectContext];
C -> B, B -> A
in C:
[_delegate matchSummaryViewController:self didFinish:YES];
in B:
[self dismissViewControllerAnimated:YES completion:nil];
[_st_delegate scorer_turn:self didFinish:YES];
in A (this is where the error occurs):
-(void)scorer_turn:(scorer_turn *)controller didFinish:(BOOL)finish{
[self dismissViewControllerAnimated:YES completion:nil];
}
If ViewController B is a modalViewController, and ViewController C is pushed onto the same modalViewController, then calling
[self dismissViewControllerAnimated:YES completion:nil];
should dismiss the entire modal view - as in, it'll dismiss C and skip B all-together (B wont even appear), which would cause problems if B tries to also call dismissViewControllerAnimated:completion: since there is no more modal view to dismiss.
Now, if you have some other set-up, such as B being a modal which, when pushing C, it actually dismisses itself and launches a new modal view (this would happen if you always use the presentViewController:animated:completion function), then you have other design problems - you shouldn't be swapping modal views like that, instead use
[self.navigationController pushViewController:C animated:YES]
to show the view and
[self.navigationController popViewControllerAnimated:YES]
to dismiss View C and return to View B.
This is assuming of course that you're using a navigationController to manage your views (which you typically should be doing).
If on the other hand you aren't using modal views at all, then dismissViewControllerAnimated:completion: isn't what you want to use at all.

Push to root View controller in UIStoryboard

I have hierarchy of ViewControllers in my storyboard structure.
It is A-B-C-D. A is embed with NavigationController and the flow goes on till D viewController. All fours view attached through segues. Now I am on D viewController, I defined some action to the button of D that It should take me directly to A viewController that is rootViewController or B viewController. Then how can I achieve this. I tried everything but didn't succeed.
I want something like it should not disturb A-B-C-D flow and it should take me to A viewController from D.
Right click on your D viewcontroller and drag i to your A viewcontroller.
Then click on the object which appears on the line you just created.
Write something like DtoA in the storyboard segue identifier in the attributes inspector.
Now in D view controller, just do:
[self performSegueWithIdentifier:#"DtoA" sender:self];
And if you instead wish to pop to a previous viewcontroller the old fashioned way, like from D to B:
UINavigationController* navController = self.navigationController;
UIViewController* Bviewcontroller = [navController.viewControllers objectAtIndex:1];
[navController popToViewController:controller animated:YES];
I hope this helps!

Resources