I have a CoreData project where:
-VC1 has a table of items. Clicking a cell invokes didSelectRowAtIndexPath func which leads to VC2 with presentViewController method.
-VC2 shows detail of the selected item. There is a button Edit which leads to VC3 where I can edit the details of the item and save, or another button to go back to VC1.
-From VC3 to VC2: When I edit the detail and save in VC3, VC3 will be dismissed, VC2 will reappear and the updated information is shown. No problem.
-From VC2 to VC1: When I press back button in VC2, VC2 will be dismissed, and VC1 is shown but the table Data are original ones and the updated detail/item is not shown. It will be shown only if I quit the app and rerun it.
I tested with print() to see if VC1's viewDidAppear(where I have the standard fetchAndResults and tableView.reloadData()) will kick in when returning back from VC2, and I noticed it doesn't print.
Does it mean VC1 is not disappeared when VC2 is presented with didSelectRowAtIndexPath func?
If so, how do I make VC1 to rerun its viewdidappear?
I tried to reload VC1's tableview from VC2 just before dismiss, but it gives me unexpected nil error.
Help me! Thanks.
Related
So I have a "Save" button on a view controller, we'll call VC2. When clicked, it unwinds back the original view controller, we'll call VC1.
However, I want to run some logic on the data from VC2 before I pass it back to VC1. I tried mapping an IBAction from the Save button that exits VC2 and runs the unwind code on VC1. But I noticed it doesn't run the IBAction mapped to the button. I'm guess this is because it's linked to exit.
So my question is: if I have a button that exits my view controller, can I also have it linked to an IBAction so that I can run some logic on the view controller being exited (VC2) before it exists?
If your button is linked to a segue on the storyboard, its IBActions does not seem to run. I don't know if this is documented anywhere, but that seems to be the behaviour.
Instead of linking the button to the "Exit" of VC1, link your VC2 to the "Exit", then give that unwind segue an identifier, let's say unwindToVC1.
Now you can add your IBAction to the button, and write:
#IBAction func buttonPress() {
// process your data or whatever
// perform the unwind segue
performSegue(withIdentifier: "unwindToVC1", sender: yourData)
}
I have a View Controller (1) that lists some entities in a table. If the user wants to add a new entity they click and "add" button where they segue to VC2 (a view containing a form to fill in the necessary fields to add a new entity). After they add an entity at VC2 I want them to be automatically forwarded to the details screen VC3 related to the entity that they just added. Normally this detail screen VC3 is a segue from VC1 when they click on an existing entity from the table.
currently in VC2 I have
//go back to VC1
navigationController?.popViewController(animated: true)
//Segue to VC3
VC1?.performSegue(withIdentifier: "goToDetail", sender: self)
The problem with this is the user sees the view change back to VC1 then changes to VC3 due to the "pop" and then segue.
If I omit the navigationController?.popViewController(animated: true) and go directly to VC3 it works as expected except when the user uses the back button from VC3 where it returns them to VC2 which is the form they filled out but I really want them to skip VC2 and go back to VC1.
Well you have two obvious choices, both very easy.
Either go ahead and push from VC2 to VC3 and then remove VC2 from the stack, by calling setViewControllers(_:animated:) with [VC1,VC3], or else...
Change what happens when the user pops from VC3 so that we pop all the way down to VC1, by calling popToRootViewController(animated:).
Now I have 3 Viewcontrollers like this:
HomeViewcontroller = VC1.
Viewcontroller2 = VC2 --> has a table view with cells on it.
Viewcontroller3 = VC3 --> allows me to edit each cell in VC2 or delete.
I am using NavigationController between Viewcontrollers, to get the nice "back" button :)
Lets say I am in VC1, I press a button and I go to VC2.
Now let's say I have 4 cells.
I press cell number 2 and I go to VC3, where I can edit what I have in the cell or even delete it.
This is all working OK (tested it).
Let's say I want to delete that cell. I have a button on VC3 and I use it and I delete the cell (this is tested and also working OK).
Now when I do this I push into VC2 again like this:
let viewController = storyboard?.instantiateViewController(withIdentifier: "VC2") as! VC2!
self.navigationController?.pushViewController(viewController!, animated: true)
The problem is that when I do this I go to VC2 but I doesn't reload my tableView has it should (the erased cell it's still there).
To fix this I tried adding some code in VC2, in the viewDidLoad:
self.tableView.reloadData()
Now I managed to do it work. I mean when I delete a cell from VC3 I am pushed to VC2 and the cell it's not there. However If I press the back button it takes me to the previous cell that I have deleted,,, If the cell doesn't exist anymore I don''t want it to take me there. I want it to take me to the VC1 (home).
How can I make this work?
Perhaps I should do this in another way and not pushing like that?
Practically you are doing wrong, when delete / edit operation done on VC3 and you want to come back on VC2, why you are doing pushViewController , you have to apply popViewController.
You have to apply self.navigationController?.popViewController(animated: true) coming back to VC2 from VC3.
Remove
let viewController = storyboard?.instantiateViewController(withIdentifier: "VC2") as! VC2!
self.navigationController?.pushViewController(viewController!, animated: true)
Add
self.navigationController?.popViewController(animated: true)
Also
Put reloadData() method for table view in viewWillAppear. like below code.
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
self.tableView.reloadData()
}
Some reference material to read about UINavigationController.
https://developer.apple.com/reference/uikit/uinavigationcontroller
Put your self.tableView.reloadData() in an override of viewWillAppear. viewDidLoad is only called once after the viewController is created, but viewWillAppear is called every time it is about to appear on screen.
Also, don't use a push to return to VC2 from VC3. You should pop the current viewController.
self.navigationController?.popViewController(animated: true)
Instead of pushing ViewController2 again from ViewController3 you need to create one delegate and implement that delegate within your ViewController2 now create the instance of that delegate in viewController3 and set that delegate when you are moving from ViewController2 to ViewController3. Now when you update or delete data of ViewController2's simply use that delegate to call method from ViewController2 and inside that method update your DataSource and reload the tableView.
N.,
Normally when different view controllers are embedded in a navigation controller, you push up the stack and going back you pop from the stack. As I understand your story it seems you are only pushing new view controllers on the stack.
Your first view controller is VC1. You press a button and you push the new view controller on the stack, VC2. The navigation controller now has 2 view controllers on the stack VC1 and VC2. Then you select a cell in the table view of VC2 and it takes you to VC3.
The navigation controller now holds 3 view controllers on the stack VC1, VC2 and VC3. Now you delete a cell and you push a NEW view controller on the stack, which is again a VC2, but a new one. The navigation controller now has four view controllers on the stack VC1, VC2, VC3 and another instance of VC2.
I would assume you have a model object containing the data in the table views of VC2 and VC3. When you delete the cell you should delete the cell in your model object and not push a new VC2 on the stack, but dismiss the VC3. You can do this with an #IBAction in VC2 to which your delete button is connected. It then pops the VC3 from the stack and you land back in VC2 in the IBAction method to which you have linked the delete button. The navigation controller then only has 2 view controllers on the stack again, VC1 and VC2.
In that IBAction method you can reload the table view and the table view will call the datasource method
tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
This method will look at your model object and not find the deleted data and your cell is gone.
When you then push the back button, the navigation controller pushes the VC2 from the stack and is left with only one view controller VC1. You will land back on your page.
Hope this helps.
For sake of simplicity I have 3 UIViewControllers named, vc0, vc1, vc2. My Flow of operations is a button in vc0 calls
[vc0 presentModalViewController:vc1]
Then in vc1 I have another button in vc1 that calls
[vc1 presentModalViewController:vc2]
In both vc1 and vc2 I have an X button that calls
[self dismissModalViewController];
Now the first run threw that flow is fine, vc1 is presented modally, followed by vc2 after the correct button presses. Dismissing the views also behaves correctly. However, when I attempt to start the flow all over again I'm unable to present vc2 modally from vc1. Has anyone else run into a similar problem before?
I think You might be missing the allot + init for the view controllers that you want to present
I have a navigationController and 3 View controllers. VC1 pushes VC2 and VC2 uses PresentModalViewController to display the 3rd VC
When VC2 uses presentModalViewController to show VC3, is the VC3 actually pushed on the navigationcontroller stack?
viewdidload of VC3 is called only 1st time. My goal is to show VC3 with a new imageView everytime. Where do I add the code to do that? viewdidappear and viewwillappear of VC3 is not fired either
It is my understanding that VC3 will be in the view hierarchy of VC2 and not the navigationController. In order to be added to the view hierarchy of the navigationController you would have to push VC3 onto it.
viewDidLoad should only be called once, unless the nib file itself was unloaded from memory, due to low memory. The documentation states that viewWillAppear and viewDidAppear should be called on VC3, so I don't know why they are not.
Update
I just tested and VC3 does have -(void)viewDidAppear:(BOOL)animated called. Make sure that the signature on the selector is correct