Getting the reference to parent controller of the UIBarButtonItem when pressed - ios

I have a instance of UINavigationController namely N declared for my UIPopoverController in a method. I have two UIViewController namely A and B. Initially when I load the popover I assign viewcontroller A to my navigation controller N so A's view get displayed. At this point when N displays A, N has done button of type UIBarbuttonItem assigned as rightNavigation item and calls a method namely M().
So Here is the Question- When I press Done I need to load view controller B in the called Method M(). That is push B in N but for doing that I need the instance of navigation controller N from the UIBarButtonItem that I pressed. I assumed some thing like
-(void)M:(id)sender
{
UINavigationController *N = barButton.parentController;
[N pushViewController:B animated:NO];
}
But I didnt arrive to any solution. Can someone please help me with this. Thank you.

In method B, you use :
[self.navigationController pushViewController:ac animated:YES];
//ac == UIVIewController which you want to push

Related

Pass data from UITabBarController to its child view controllers objective C in Real Time

I have a UITabBarController with Tabs (say Tab1, Tab2, Tab3, Tab4) and UITabBarController is my RootViewController and I'm making an API Call in the same. Since it is a RootViewController I'm displaying Tab1 as my default View. When I get the results to my API Call in UITabBarController. I need to share the details in real time to my Tabs. I have tried few ideas(like NotificationCenter, Singleton Class) but it's not working out. Can somebody help me to fix this? Thanks in advance. If you have a working example I kindly request you to share it with me.
Img Representation:
UITabBarController has a property viewControllers that is an array of view controllers displayed (by index for tab position, but you probably don't need that). One simple solution is when your UITabBarController subclass gets data, it can loop over the viewControllers array and check each for delegate conformance / responds to selector and update accordingly.
That's probably the simplest. Another way would be to have the view controllers in the tabs register for updates. So they conform to a delegate protocol, and get a reference to their tab bar controller (the tab bar controller subclass), and call the tab bar saying "observe updates." The tab bar controller keeps storage of registered observing objects and calls each with new data.
This assumes you are setting the tab's view controllers in IB. If you are doing it programmatically, then with the second option you can just link the tabs to the tab bar controller when you add them. If set in IB you could also do it by overriding -prepareForSegue since embedding the tabs in the root is considered a segue, but you'd still have to cast the destinationViewController as whatever subclass can receive data.
The first option is simple enough and though I don't like casting, it's unavoidable to take advantage that the references you want are right there. Plus, there will in reality only ever be a single-digit number of tabs, so it can't get expensive. Hope this helps.
This is just same like passing the values to another viewController but here you need to use tabBarController's delegate method in below example:
Passing value using delegate method from controller A:
- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController
{
ControllerB *controllerB = (ControllerB *) [tabBarController.viewControllers objectAtIndex:1];
//In this example, there is only have 2 tab bar controllers (A and B)
//So, index 1 is where controller B resides and index 0 where controller A resides.
self.controllerB.data.text = #"some value!";
//This will change the text of the label in controller B
}
Getting value in controller B
// set property in controller B .h file
#property(strong, nonatomic) UILabel *data;
// in controller B .m file viewDidLoad method
- (void)viewDidLoad {
[super viewDidLoad];
NSLog(#"getting data: %#",data.text); // getting value
}
How about
-Declare a delegate protocol, say, Tab1ViewControllerDelegate, with a method - (void) tab1ViewController:(Tab1ViewController *) tab1ViewController, didReceiveData: (NSDictionary *) data
make a TabBarController subclass a delegate of your tab1ViewController
when tab1ViewController gets it's data, call [self.delegate tab1ViewController: self, didReceiveData: datDict]
then your TabBarController can distribute the data to its array of viewControllers

Present nth View Controller without showing its root VC

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!

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

Navigation bar back button change functionality

I have 3 view controllers: A, B and C. A is initial and it has 2 buttons: one is redirecting to B, and second one to C. When B is pushed, and button within B pressed user is redirected to C. Now on navigation bar within C, when I press back button I want to be redirected to A always, and never to B. Is that possible?
This is base functionality I want to achieve:
a)
A->B->C C back to A
b)
A->C C back to A
for (UIViewController *controller in self.navigationController.viewControllers)
{
if ([controller isKindOfClass:[A class]])
{
[self.navigationController popToViewController:controller
animated:YES];
break;
}
}
you can achieve this with presentview controller as modal view.first from a if first button is pressed present b view.if secnd button is pressed present c view and in c view controller put back button and add delegates and implement the delegate in a view controller.and you will have to dismiss one view after the other in a view controller.
The only way is replacing the back button by a custom button.
After reading what you said I think you meant implementing custom actions for each button.The way I would do it(In Swift) is this:
#IBAction func pressButton(sender : UIButton){
var SecondView :ViewController2
SecondView = self.storyboard?.instantiateViewControllerWithIdentifier("SecondView") as ViewController2
self.presentViewController(SecondView, animated: true, completion:nil)
self.viewWillDisappear(true)
}
This method I have used a lot and I know there is another way using segues but you still use #IBActions.You just create a new segue(identifier) and in the prepareforSegue method you say what happens when that segue is triggered

Resources