Hot to pass Values between views or classes? - iphone

i have a view with label when button pressed moves to second view controller with a picker view , i need to retrive the selected value and then to pass to first view's label,
i have used delegates and protocols to acheive this,
but is there any possible ways like referencing like etc to do this?

You can use a singleton class to store the selected data and then can retrieve it wherever you want. Just like you get UIApplication's instance from 'sharedApplication' method, its the same object in the memory through out the application lifecycle.

Related

How to set a view's outlets' values from within another class

In xcode, If we have a view controller that has some subviews' (eg: label) outlets, and we want to change its text from the AppDelegate.swift class whenever an app is becoming active after suspended, How can this be done? I have tried making an instance of this view controller class from the AppDelegate class, but that didn't work because it made another view instance instead of the view itself, and we can't set the outlet as static. I have also thought of making a static ViewController property observer, but since it is static I still can't set the outlet from inside it. So, how can we do such a thing?
The best way to achieve what you want - is notifications. In your view controller add observer which will change your label's text or whatever. From AppDelegate method post notification. If you need an example, I can help you.
If we have a view controller that has some subviews' (eg: label) outlets, and we want to change its text from the AppDelegate.swift class whenever an app is becoming active after suspended, How can this be done?
Don't. Never speak directly to another class's outlets. Give that class a method that you can call where that class will respond to the calling of that method by speaking to its own outlet.
I have tried making an instance of this view controller class from the AppDelegate class, but that didn't work because it made another view instance
Correct. You need to get a reference to the existing instance of the other class. To do that, you need to know your own view controller hierarchy / structure or else provide one class with a delegate / reference to the other class at the time of creation.

iOS Development: How can I return data from a modal view?

If I have a parent view controller that displays a modal view with a textfield to collect data from the user, what's the best way to return that data to the parent view controller? Currently, I assign the parent view controller as a delegate that's called from the modal view when the user enters the data. Is there a simpler/better way to return data from a modal view?
Thanks so much for your help!
Personally, I would have the modal view dispatch an NSNotification that passes the data. A delegate works too, of course. I think that both a singleton and a delegate mean tighter coupling, but I understand that some might disagree.
But I do use singletons, too, if I need access to data stored centrally from many different places in an app. I just wouldn't use it simply to pass data from a view to another.
When needing to store and pass data around I usually have a singleton class that I use throughout my app. This keeps things a little cleaner in separating my views from each other. Here's a simple implementation:
http://www.galloway.me.uk/tutorials/singleton-classes/

How to programmatically set cell.textLabel.text from a different view?

I've got a view controller, call it VC1, that's a table view. When I tap a cell in the table view, I am presented with a new view controller, call it VC2, which is a short list of choices. After making a choice, I want to dismiss VC2 and set the cell.textLabel.text property of the VC1 cell I originally tapped to the value I selected in VC2.
Conceptually speaking, what is the proper way to do this? I've tried a handful of different approaches, but all of them seem wonky at best, and only one of them actually worked - although it was the most cumbersome of all, passing references to both view controllers and table view cells and all kinds of things. It just feels like I'm making a mountain out of what is probably a mole hill.
This is such a common paradigm that I find it hard to believe there's not a simple method for doing it.
There are a number of ways to handle this but one of the most flexible is via the Delegate Pattern. Define a delegate protocol in VC2's interface and then make VC1 conform to that protocol. When you create VC2 assign VC1 as it's delegate (similar to the way you did with your UITableView). One requirement of your protocol should be a method like didFinishWithStringSelection: (or whatever you call it) where you would update the table cell and reloadTable.
I do kind of that with with a table view presenting several attributes of an data object. To change a single attribute the user has to select the specific table cell showing the attribute he wants to change and that will push a new view controller (with a picker in my case) where the user can change the value from a selection.
I assign the data object to a property of the new controller before pushing. With this the value can be changed directly in the data object, and when i return (via navigation controller) to the first view controller there is a reloadData in viewWillAppear.
Did you try it this way?

How can I get a method to call in a class while the user is in a view of another class?

I have an iPhone app based on a tabBar and tableViews. I want the user to be able to click on one tab and access options for filtering the data in the initial tableView.
The problem I'm having is that while the user is selecting filter criteria, I want the main table (not visible) to update. The reason this is important is that I want to show how many cells are still in the table as it is being filtered in the navigation bar.
Currently, the method for filtering the main table (-handleFilter) is called in the viewWillAppear method of my rootViewController class. How can I call this method from my "searchOptions" class?
Thanks for the help!
It sounds like you're conflating too much between your model and your controllers (assuming you're following the MVC design pattern). The other controllers besides the main table should be able to query the model themselves to display the count information without asking the main table controller.
I could be misunderstanding something though, a little more information on what data you're using and how it's being filtered in the controllers attached to the other tab bar items would help.
The most straightforward way would be to give the options controller a pointer to the list controller. Then you can call the method directly.
Other options include defining a method/property on some global object (like your app delegate) to access the list controller from elsewhere in the app, and using a more decentralized mechanism like NSNotificationCenter to pass that information around (rather an a method call), or relying on the model itself to notify all of the controllers accessing it when it changes (possibly using Key-Value Observing, or an explicit delegate protocol).

How to access other view controllers data member variables?

I want to access the value of a view controller data member variables in another view controller object.
Or is it possible to access its controls like UILabel text property?
A lot of times when I find I have to do things like that I find I can redesign the solution and the need for it goes away. Jay's law: "If it's too hard you're probably doing it wrong."
It is possible to access a UILabel of another view controller, but don't. It will lead you to very hard-to-understand bugs. Any IBOutlet can become nil at surprising times when memory is low. You shouldn't mess with another object's UI elements directly.
Your initial idea of accessing the data (model) objects, is the right one, though generally you will be better off to just initialize both view controllers with the same model object. For instance, say you have a status message that you want to show up in two different UILabels in two different view controllers. Rather than have one view controller ask the other view controller for the data, it's better to have a model class like "Status" that both views have a pointer to. Whenever it changes, they change their UILabel.
Even better is to post a notification (StatusDidChangeNotification) and just let everyone who cares observe it and update their UI appropriately.
You want to keep UI elements very loosely coupled in Cocoa. Otherwise you wind up with hard-to-fix bugs when you make what seems like a minor UI change.
You are going to have to define the property in the view controllers interface, then as long as you have a reference to the view controller in the second view controller you should be able to access it like the text of a UILabel..
viewWillAppear: is only called by the framework when you use the built-in view controller transitions like presentModalViewController:animated: or pushViewController:animated:. In other cases, you have to call viewWill/Did(Dis)Appear: yourself.