Swift, core data, add item from modally presented view - swift

I am updating an app into Swift and have a tableView to which items can be added from a modally presented view. This works fine when the Save button is pressed, but when cancel is pressed the table view is presented with a blank line at the top, showing that an empty item was added. What am I missing?
The AddViewController protocol is:
protocol NewGaugeDelegate {
func didFinish(viewController:AddGaugeViewController, didSave:Bool)
}
and the didSave function is:
extension AddGaugeViewController {
#IBAction func cancelButtonWasTapped(_ sender: AnyObject) {
delegate?.didFinish(viewController: self, didSave: false)
}
#IBAction func saveButtonWasTapped(_ sender: AnyObject) {
addGauge()
delegate?.didFinish(viewController: self, didSave: true)
}
}
in the tableView controller, the protocol is accessed like this:
extension GaugeTableViewController: NewGaugeDelegate {
func didFinish(viewController: AddGaugeViewController, didSave: Bool) {
guard didSave,
let context = viewController.context,
context.hasChanges else {
dismiss(animated: true)
return
}
context.perform {
do {
try context.save()
} catch let error as NSError {
fatalError("Error: \(error.localizedDescription)")
}
self.coreDataStack.saveContext()
}
dismiss(animated: true)
}
}
Suggestions as to why the cancel function is not working properly would be appreciated.

Related

Refresh Core Data in ViewController when Modal View (2nd VC) is Dismissed

I have 2 ViewControllers:
VC1 populates its tableView based on CoreData attribute isPicked, which is bool and show only items with true state. VC2 is a second Modal (not Full Screen) View Controller which allow user to change the state of isPicked attribute: check and uncheck item (make it true or false) and after he pressed save button changes will be saved in CoreData and VC2 will be dismissed. However VC1 doesn't update itself with recent changes comes from VC2.
What I tried:
load my array to show in VC1 in viewWillAppear(), but since my VC2 is a modal view and not in full screen this method calls only once
tried to implement protocol and delegate, but result is the same - can't reload data (but assume I could done it wrong)
My VC1:
override func viewDidLoad() {
super.viewDidLoad()
let predicate = NSPredicate(format: "isPicked == YES")
converterArray = load(for: tableView, and: predicate)
}
func load(for tableView: UITableView, with request: NSFetchRequest<Currency> = Currency.fetchRequest(), and predicate: NSPredicate? = nil, sortDescriptor: [NSSortDescriptor] = [NSSortDescriptor(key: "shortName", ascending: true)]) -> [Currency] {
var array = [Currency]()
request.predicate = predicate
request.sortDescriptors = sortDescriptor
do {
array = try context.fetch(request)
} catch {
print(error)
}
DispatchQueue.main.async {
tableView.reloadData()
}
return array
}
My VC2:
#IBAction func saveButtonPressed(_ sender: UIBarButtonItem) {
do {
try context.save()
} catch {
print(error)
}
dismiss(animated: true)
}
Also please take a look at a short GIF where I show what happens: CLICK
I don't know how after VC2 dismissal automatically update changes in VC1...
I am assuming the data is being saved properly in VC2 and the only issue you have is the right way to reload the data in VC1.
Making VC1 conform to the UIViewControllerTransitioningDelegate and implementing its delegate function animationController(forDismissed dismissed: UIViewController) might solve your problem
Read more here
Try this
// Some function in VC1 which loads VC2
func loadVC2()
{
let newVC = UIViewController()
// This is important
newVC.transitioningDelegate = self
present(newVC, animated: true, completion: nil)
}
Then in VC1, implement the function animationController(forDismissed dismissed: UIViewController)
extension VC1: UIViewControllerTransitioningDelegate
{
// UIViewControllerTransitioningDelegate delegate function auto invoked during modal transitions
func animationController(forDismissed dismissed: UIViewController) -> UIViewControllerAnimatedTransitioning?
{
print("returned back")
// reload your table in VC1 here
/// Returning nil as per guidelines
return nil
}
}
Give this a try and see if this solves your problem.

how can i dismiss a viewcontroller whilst at the same time segue onto a different one

I am currently unable to dismiss a viewcontroller and at the same time make the application move to a different controller without experiencing a few bugs. What my code does currently is when the timer 'secondsRemaining' hits zero it will dismiss the current viewcontroller it is on and will show the previous viewController that is displayed underneath the current viewcontroller being the 'homeScreenViewController'. Because of this it will not call the code- self.performSegue(withIdentifier: "gameScreenToHighscore", sender: self).
Current code:
#objc func updateTimer() {
if secondsRemaining > 0 {
secondsRemaining -= 1
timerLabel.text = "Timer:\(secondsRemaining)"
print(secondsRemaining)
} else {
self.dismiss(animated: true, completion: nil)
self.performSegue(withIdentifier: "gameScreenToHighscore", sender: self)
}
}
I think the best approach is that you handle the navigation in the parent ViewController, You can achieve this by making delegate.
In the ChildViewController:
#protocol ModalVCDelegate {
func navigateToTheSecondVC()
}
class ModalViewController {
weak delegate: ModalVCDelegate!
}
and you should call the delegate method when the timer has done.
delegate?.navigateToTheSecondVC()
And Also in the ParentViewController, you should set the delegate:
childVC.delegate = self
present(childVC, animated: true, completion: nil)
and then:
extension MainViewController: ModalVCDelegate {
func navigateToTheSecondVC {
dismiss(animated: true, completion: {
self.performSegue(withIdentifier: "gameScreenToHighscore", sender: self)
})
}
}

How can I get an updated value from UserDefaults?

I have an Onboarding controller, when a user has completed Onboarding I am writing a value in UserDefaults and then dismissing the OnboardingViewController.
My base viewcontroller however is pushing them back to the Onboarding flow as UserDefaults.standard.bool(forKey: "ONBOARDING|COMPLETE") is returning false.
If I restart my app however is will return true and they are pushed to the correct VC.
My Base ViewController
class ViewController: UIViewController {
var onboardingComplete: Bool {
get {
return UserDefaults.standard.bool(forKey: "ONBOARDING|COMPLETE")
}
}
override func viewDidLoad() {
super.viewDidLoad()
}
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
print(UserDefaults.standard.bool(forKey: "ONBOARDING|COMPLETE"))
print(onboardingComplete)
setBaseViewController()
}
fileprivate func setBaseViewController() {
if onboardingComplete {
print("has completed onboarding")
} else {
let layout = UICollectionViewFlowLayout()
navigationController?.present(OnboardingController(collectionViewLayout: layout), animated: true, completion: { })
}
}
}
My Onboarding controller has a method in which I am using to set the value as complete
#objc func handleCompleteOnboarding() {
dismiss(animated: true) {
DispatchQueue.main.async {
UserDefaults.standard.set(true, forKey: "ONBOARDING|COMPLETE")
}
}
}
You are setting the value in the completion handler.
Move this outside of dismiss.
#objc func handleCompleteOnboarding() {
UserDefaults.standard.set(true, forKey: "ONBOARDING|COMPLETE")
dismiss(animated: true)
}
You have essentially created a race condition between you setting the value and your viewDidAppear method being called.

Why delegate event is not received swift?

I would like to pass data from EditPostViewController to NewsfeedTableViewController using delegates, but func remove(mediaItem:_) is never called in the adopting class NewsfeedTableViewController. What am I doing wrong?
NewsfeedTableViewController: UITableViewController, EditPostViewControllerDelegate {
override func viewDidLoad() {
super.viewDidLoad()
//set ourselves as the delegate
let editPostVC = storyboard?.instantiateViewController(withIdentifier: "EditPostViewController") as! EditPostViewController
editPostVC.delegate = self
}
//remove the row so that we can load a new one with the updated data
func remove(mediaItem: Media) {
print("media is received heeeee")
// it does't print anything
}
}
extension NewsfeedTableViewController {
//when edit button is touched, send the corresponding Media to EditPostViewController
func editPost(cell: MediaTableViewCell) {
let editPostVC = storyboard?.instantiateViewController(withIdentifier: "EditPostViewController") as? EditPostViewController
guard let indexPath = tableView.indexPath(for: cell) else {
print("indexpath was not received")
return}
editPostVC?.currentUser = currentUser
editPostVC?.mediaReceived = cell.mediaObject
self.navigationController?.pushViewController(editPostVC!, animated: true)
}
protocol EditPostViewControllerDelegate: class {
func remove(mediaItem: Media)
}
class EditPostViewController: UITableViewController {
weak var delegate: EditPostViewControllerDelegate?
#IBAction func uploadDidTap(_ sender: Any) {
let mediaReceived = Media()
delegate?.remove(mediaItem: mediaReceived)
}
}
The objects instantiating in viewDidLoad(:) and on edit button click event are not the same objects. Make a variable
var editPostVC: EditPostViewController?
instantiate in in viewDidLoad(:) with delegate
editPostVC = storyboard?.instantiateViewController(withIdentifier: "EditPostViewController") as! EditPostViewController
editPostVC.delegate = self
and then present it on click event
navigationController?.pushViewController(editPostVC, animated: true)
or
present(editPostVC, animated: true, completion: nil)
you can pass data from presenter to presented VC before or after presenting the VC.
editPostVC.data = self.data
I suggest having a property in NewsfeedTableViewController
var editPostViewController: EditPostViewController?
and then assigning to that when you instantiate the EditPostViewController.
The idea is that it stops the class being autoreleased when NewsfeedTableViewController.viewDidLoad returns.

Return control to function when modal viewcontroller dismissed

I need to present a modal VC that sets a property in my presenting VC, and then I need to do something with that value back in the presenting VC. I have to be able to pass pointers to different properties to this function, so that it's reusable. I have the code below (KeyPickerTableViewController is the modal VC).
It should work, except not, because the line after present(picker... gets executed immediately after the picker is presented.
How do I get my presenting VC to "wait" until the modal VC is dismissed?
#objc func fromKeyTapped(_ button: UIBarButtonItem) {
print("from tapped")
setKey(for: &sourceKey, presentingFrom: button)
}
#objc func toKeyTapped(_ button: UIBarButtonItem) {
print("from tapped")
setKey(for: &destKey, presentingFrom: button)
}
fileprivate func setKey(for key: inout Key!, presentingFrom buttonItem: UIBarButtonItem) {
let picker = KeyPickerTableViewController()
picker.delegate = self
picker.modalPresentationStyle = .popover
picker.popoverPresentationController?.barButtonItem = buttonItem
present(picker, animated: true, completion: nil)
if let delKey = delegatedKey {
key = delKey
}
}
You could use delegate pattern or closure.
I would do the following
1. I would not use inout pattern, I would first call the popover and then separately update what is needed to be updated
2. In KeyPickerTableViewController define property var actionOnDismiss: (()->())? and setting this action to what we need after initialisation of KeyPickerTableViewController
I could show it in code, but the abstract you've shown is not clear enough to come up with specific amendments. Please refer the illustration below.
import UIKit
class FirstVC: UIViewController {
var key = 0
#IBAction func buttonPressed(_ sender: Any) {
let vc = SecondVC()
vc.action = {
print(self.key)
self.key += 1
print(self.key)
}
present(vc, animated: true, completion: nil)
}
}
class SecondVC: UIViewController {
var action: (()->())?
override func viewDidLoad() {
onDismiss()
}
func onDismiss() {
action?()
}
}
While presenting VC, add dismissing modal VC action in its completion handler, so that Viewcontroller will be presented after dismissal is completed
present(picker, animated: true, completion: { (action) in
//dismissal action
if let delKey = delegatedKey {
key = delKey
}
})