Passing values when popping a view controller with a UINavigationController - objective-c

I have a pretty basic question. Assuming I have a UINavigationController set up for my app and I want to pass a value to the previous view when the user hits the "back" button in the UINavigationBar. I know I call popViewController:, but is there a way to pass a value back with it? Assuming there is (which there must be), what's the best way to do this?
Thanks!

What you should do in this case is think about the problem in a different way. You can create a delegate protocol on the view controller you want to pass information back from. This protocol can have any number of methods, at least one of which will look something like viewController:(MyViewController *)theViewController didSelectThisInformation:(NSString *)someInformation. When some action takes place on this view controller, it makes a call to its delegate (which conforms to the protocol you defined) and calls the appropriate method. When pushing this view controller, you would set its delegate property to the object you want to receive that information.

Related

How can I use a variable in two modal separated view's with the same class (.h and .m files)?

I am trying to use a variable (birthDateLabel) in two view controllers that are separated via a modal but use the same view controller class (CreateAccountViewController). The label works in the view that it is set in but after [self dismissViewControllerAnimated:YES completion:nil]; is run the varriable is reset, how can I retain it?
Order of operations:
The first instance of CreateAccountViewController is run
A button is tapped to go to a new instance of CreateAccountViewController but this instance has a different view with a UIDatePicker to set the birthDateLabel variable.
The birthDateLabel is set
The user taps done and dismissViewControllerAnimated is run
The app updates a UILabel on the first instance of CreateAccountViewController
Step 5 is what does not work, if I put the label on the view as the picker it works but when the modal is dismissed the variable is reset. How can I keep the variable set after the modal is dismissed? Or is my only option to create separate view controller classes?
I tried to do my best explaining this, but if you need me to explain it more just comment.
The standard way to do this is to use a delegate. The fact that your two controllers are both instances of the same class doesn't make any difference, other than they will both have a variable called birthDataLabel. The value of that variable is specific to each instance, just as it would be if these were of two different classes.
So, you should do it the right way, which is to create a delegate protocol in your second instance, and have the first one set itself as the delegate when it first presents the second one.
#rdelmar provides the most technically correct answer, but if it's just one value you could simply use nsuserdefaults. If the number begins to expand creating a delegate might be a smart choice.

Send data back to rootviewcontroller before a popToRootViewController method call?

What is the best way to send data back to the root view controller before calling the popToRootViewControllerAnimated method? I am trying to signal to the rootviewcontroller that the current viewcontroller no longer exists and keep a record of what viewcontroller has been pushed thru segue.
You mustn't wait until the current view controller "no longer exists". While the current view controller does exist, the root view controller is the current view controller's navigationController.viewControllers[0]. So you have the reference you need to send a message from this one to that one. Just cast to the root view controller's class and now you can call any public method in the root view controller, e.g.
MyRootViewController* rvc = (MyRootViewController*)(self.navigationController.viewControllers[0]);
rvc.coolData = myCoolData;
There are two basic ways to do this: 1) with an NSNotification message or 2) with a delegate call. There are plenty of tutorials on how to code both. I wrote a small delegate tutorial as an answer to this question: Back button in iphone app and you can find a great NSNotification tutorial here: Send and receive messages through NSNotificationCenter in Objective-C?
You can subclass UINavigationController and override the necessary methods to track your array. I would recommend keeping a uniqued set of titles for each view controller popped off the stack. As for signaling to the current view controller that the previous view controller doesn't exist, that's what -viewWillAppear is for. If you need finer-grain control, create a protocol for your View Controller instances to conform to that the navigation controller calls out to when it pops one or more of them off the stack.

Setting an identifier for an Xcode Storyboard Segue in a UITabBarController, and getting a pointer to its view controller

This is my first app using storyboards/segues, and I'm pretty sure the answer to this is easy, but I'll be as thorough as possible in describing my issue.
I am making a simple app which has a Tab Bar Controller scene and two View Controllers.
My app launches by being sent a URL from another app. The application:openURL:sourceApplication:annotation: method in the app delegate performs some work to determine
which tab to display first, and
what information to display on it.
My goal is to use the performSegueWithIdentifier method (I'm open to alternatives though) from within the AppDelegate to select the tab, and also find a way to send a method to the instance of the view controller created by the storyboard.
Issue #1 is that I can't set an identifier. When I select the tab "relationship" there are no choices available in the Attributes Inspector (it says "Not Applicable"). How do I set a name for this segue? Can I? Or is there some rule under which UITabBarController segues can't be trigger programmatically?
Issue #2 is that I can't find a way to have a pointer to the view controller. Pre-Storyboard I would alloc and init a new view controller and then set it up, but here if I do that, it does not get displayed when my app launches (I think this is because Storyboard is displaying a different instance of the same view controller.)
I know this post is long, but I feel like I'm missing something simple. What is it?
Sounds like you need to have your AppDelegate set the entry point to your storybard.

Get an event from an other view controller

I have two ViewController. The first contains and displays an array with values.
The second is a picker view (modal view controller) that permits to choose a columns to add on the array (with a button "ADD").
But, I don't know how retrieve an event when user click on "ADD" button to refresh my array because IBaction function and the array are not in the same controller.
Thanks for your help.
Protocols can be helpful for such situations.
A protocol is simply a list of method declarations, unattached to a class definition.
Protocols can be helpful in a number of scenarios, a common usage is to define methods that are to be implemented by other classes. A familiar example is when using a tableview, your class implements the cellForRowAtIndexPath method which asks for cell content to insert into a table – the cellForRowAtIndexPath method is defined within the UITableViewDataSource protocol.
Simple objective-c protocol example
You can also pass one local Variable when You press your add button and after that you reload your array in table view.
I think you want same like add contact add field function in simulaor.
Welcome.
I consider that,you have an array in first view controller that you want to access in second view controller on button event (IBAction):
Create Method in secondviewcontroller with array as parameter like this.
-(void)methodname:(nsmutablearray *)array;
Call the above method in first view controller and pass your array in this method when you navigate to second view controller by initializing it.
So, In second view controller you will get an filled array that you can use further.

Call a method when switchig views with UITabBar

Is there any way to automatically call a custom method when switching views usig a UITabBarController? Like, I want to he able to, when I set a NSString in one view in a UILabel, haave it automatically update a different label on a different tab with the same string. Thank you!
Yes, take a look at the UITabBarControllerDelegate protocol. All you have to do is register your object as the tab bar delegate and implement the shouldSelect or didSelect callback (the didSelect would be better a better choice). The callback will get called each time the tab bar switches.
Take a look at tabBarController:didSelectViewController: in UITabBarControllerDelegate. Implement that method in your delegate and do whatever updating you need to do in there.