I have a MainViewController which is a table view controller, with custom cells where the user can select some data. There is a button on this MainViewController that goes to PreferencesViewController, which has two buttons on the navigation bar, one for back (it is wokring now good), and the other one for save (here is my problem)
This is a screenshot of the navitation bar in the PreferencesViewController:
I drag a segue from the save bar button back to the MainViewController.
my problem
when I hit save, the MainViewController appears, but without saving the values the the user has already selected. sounds like a new instance of this MainViewContoller is being created.
What am i missing here? what other approach i should have already used please?
You need to connect your save button to an IBAction, not a segue.
In that IBAction, do whatever you need to do to save your data, and then do a dismissViewController:animated: call yourself in code.
If you wanted to use a segue you wouldn't be able to (easily) save your data, and yes, it creates a new instance.
An alternative would be to connect your save button to an unwind segue, rolls back, or unwinds, one or more segues to get back to a previous view controller. To do that you'd have to put your save logic in prepareForSegue (with code to test which segue is being invoked and only save in response to the save segue.)
i found the solution myself using:
1- custom delegate:
2- self.navigationController?.popViewControllerAnimated(true)
Related
I have a MainController with a button
Click button will pushViewcontroller to UsersViewController
In UsersViewController, I will load an table view of users with an selected cell to be highlighted
Ask: How could I save the state of tableview or state of the ViewController after hitting the back button from navigationcontroller, And the next time when a initiate the UsersViewController again, the table view will be at last state.
while traversing from the page, save the details in a Manager object, and next time, whenever you appear on the page, check whether the details are there in Manager object, if true, show the details from the object itself, otherwise, show them like you do as now.
You can try to avoid using a pushViewController / Navigation Controller by using a TabBarController for example but that might change the behavior of your app.
Or avoid your UsersViewController being deallocated and just initialize it once.
here is my scenario case.
Initially for going to this VC without loading is hidden.when I click to first view controllers button it goes to second view controller.When I click button from secondVC it come back to first one and for going to this VC without loading button is now visible.Now when I click for going to this VC without loading I want to show my second view controller without reload because my previous loaded data for second view controller is needed.So how can I do that?
the actual scenario of my app look like this.My first VC
and the second one.
It's a picture of sound cloud but the case is same.
First possible solution,
Add SecondViewController as child view controller of FirstViewController using container view in Storyboard.
Every time you want to remove SecondViewController just hide/remove it with custom animation block.
Keep the reference of SecondViewController in FirstViewController
Second possible solution,
Create shared data object.
Then you can use that shared data object in any view controller, regardless of saving the state of any view controller.
I would create an object where i put the data und pass this from ViewController to ViewController by properties. Maybe this is to simple but it should work.
FIRST SCENARIO:
I have two view controllers
VC1 has a button, and a label
VC2 has a button, and a text field
theres a modal segue between VC1 -> VC2
when I run this segue, we set VC1, as the delegate for VC2.
We go to VC2, fill out the text field, hit the button, and VC2 is dismissed.
some delegated method is run on VC1, and VC1.label is filled in.
question: is there any way to do this without dismissing VC2.. for example, if VC2.button just modal segues us back, or slides us back to VC1 im assuming it re initializes the viewcontroller and the label wont be changed. do you always have to dismiss the view controller
SCENARIO 2:
again, two view controllers.
this time its reversed.. so i have
VC1 with a textfield and a button
VC2 with a label and a button
soo now we fill out VC1, and we expect it to show up on VC2. But without a segue, they have no relationship. is there any way to pass data between VCs using delegation without one initial segue? Is this segue requirement to use delegation something specific to view controllers? Im assuming it is because in other cases we just instantiate objects, and use their delegate methods. but with view controllers we want to reference one that is already created, and not instantiate a second one.
note: im using story boards
1) You could do it without dismissing VC2, but it's not a good idea. You don't want to segue "back" to one, because, as you surmised, you're actually creating a new instance of VC1, and then if you segue again to VC2, you're creating a new instance of that too. You will keep piling up more and more instance of the two controllers and none will ever be deallocated.
2) Again, your instincts are correct -- you need to somehow get a reference to the instance of VC2 that your putting on screen in order to set yourself as delegate. You don't have to have a segue to do that, you could create the second controller in code and do a manual push or presentViewController, but that's functionally, the same as doing a segue.
Sorry, dont fully understand what you want .. but here is my take.
FIRST SCENARIO:
Why would you need to update the view that isnt on screen ?
Just update in viewWillAppear.
Otherwise you can have the delegate update it when you finish editing that textfield.
SCENARIO 2:
You need a link between the view controllers, use segues makes easy, set as delegate and pass along the info. Why make it harder than it needs to be
Many things have delegates, textfields etc, you are just saying this class / obj will do something for something else.
There are many youTubes about delegates, ie
http://www.youtube.com/watch?v=eNmZEXNQheE
For more info see this stack post - it covers everything you need to know
Passing Data between View Controllers
Well, the question is in the tittle:
How to catch the moment, when the UIViewController is pushed to the front because back button was pressed?
I need it to reload data on the list when it's UIViewController is on the front again. The problem is I don't want to do that every time the view appears, for example when the view is first shown.
The easy way is to set a flag on your first controller, when you push into another view set it to YES and on your viewWillAppear: check if you need to do some reloading.
The other option, assuming your second view will configure some things for the view A, is to create a delegate method that let's the view A know it needs reloading.
The third option is to use a custom back button (rounded, not with the back button shape), capture the event and do the popViewController manually after you do whatever you want to do.
then you make a new method and call it before the push (pop) on front the view controller and this method help you to reload the list on itself class also :-) i hope you understand what i want to say then when you want to reload the data you simply call that function and list is reload .
I have a simple iPhone app based on a navigation controller. There are two view controllers, VC1 and VC2, both with table views. VC2 also has a custom table cell. When a user selects a row in VC1, VC2 is pushed on to the stack. When the user selects the back button it's removed. Typical navigation stuff.
The problem I have is that the data in the cells in VC2 persists when the back button is pressed, so that when the user selects a different row in VC1, VC2 is pushed back on to the stack with the 'old' data in the cells, before the methods in VC2 reload the data.
I want to make sure that the data in the table in VC is removed every time the back button is pressed. I've tried releasing the tableview using viewWillDisappear, but it's not working. What's the recommended way of dealing with this situation? I've looked at the docs but it's not obvious (to me at least).
Try out this code snippet in viewWillDissapear or dealloc method.
if(yourTableViewCellObject) [yourTableViewCellObject release];
if(yourTableViewCellObject) yourTableViewCellObject=nil;
This might work.
I've used this technique several times since I asked the original question. As I mentioned in my comment to #Aditya, I've found that the easiest way to deal with this is to use viewWillDisappear to hide the tableview with the 'old' data, and when the user navigates back to the page, wait until the 'new' data is loaded into the table before making the tableview visible again.