0

While I am pretty sure of a simple solution to this issue, the issue here is that the secondVC is in no way or form separate from the firstVC. The secondVC is what you get when you tap on the tableViewCell. The cell you tapped (using the indexPath) I send some data to the secondVC and then you can edit that task in the secondVC and even set a notification reminder for it.

In the secondVC I am running some code that triggers a timer at the time set by the user for that particular task. The timer in return invokes a selector method that updates a label and changes it to "Time's Up".

When this label is "Time's Up", I set the bellicon which is in the tableviewcell in the firstVC to white or remove the bellicon from the particular task.

I want this peice of code to run even if I am not in the secondVC. (I have this code running in viewDidAppear). If I put this code in a function and then call it while I am in the firstVC, the code has no idea which time it should select.

override func viewWillAppear(_ animated: Bool) {
adjustTextViewHeight()
edittaskview.becomeFirstResponder()
cardremindery?.constant = -999

guard let selectedDate = editnotes?.sSelectedDate,
    var needsToRemind = editnotes?.sReminderState  else {
        print("No date selected")
        return
}
if (needsToRemind) {
    self.timesUpTimer = Timer(fireAt: selectedDate, interval: 0, target: self, selector: #selector(updateTimeLabel), userInfo: nil, repeats: false)
    RunLoop.main.add(timesUpTimer!, forMode: RunLoopMode.commonModes)
}


}

Selector code that updates the belliconcolor:

@objc func updateTimeLabel()
{
  editnotes?.sReminderDate = "Time's up"
  editnotes?.belliconcolor = .white
  reminderMsg.text = editnotes?.sReminderDate
  print("Time is up ok")
}

The above code works when you are in the secondVC. I want this to work even if I am in the firstVC. Since I am using FetchedResultsController, I am pretty sure that the tableView will be updated if data is changed and since the belliconcolor is an element I have saved in coreData changing it will update the tableview.

The question is: How can I run this piece of code from the firstVC for all the elements of the tableview? The bellicon appear next to those tasks that you set reminder for in the secondVC. You access secondVC by tapping the relevant cell in the tableview: enter image description here

You set the reminder in this secondVC. If the the time is up then the label underneath Due Date changes to 'Time's Up' and the belliconcolor for the specific task is changed to .white

enter image description here

Time's Up! -> The piece of code has been excecuted and the belliconcolor for this particular task is .white. This will be apparent when I go to the firstVC. I want this code to run even if I am in the firstVC so that the tableview is automatically updated. enter image description here

Osama Naeem
  • 1,830
  • 5
  • 16
  • 34
  • It's not clear to me what your overall design is, but you could move timer code out to a seperate object. It can notify views when updates are needed. – john elemans Feb 27 '18 at 18:59
  • Can you please elaborate a bit? – Osama Naeem Feb 27 '18 at 19:00
  • What about delegate pattern? You can make your firstVC secondVC's delegate and call delegate methods from secondVC. You can find many articles on Delegate topic on the web. – Yury Bogdanov Feb 27 '18 at 20:32

2 Answers2

1

Usually, there are 2 ways of doing it:

  1. Use NotificationCenter.
  2. Use Delegate pattern.

Both of them may work. How to choose will be a right question. To answer this may help.

Usually, if I want to have only 1 connection between 2 objects I usually go with Delegate. If you want to Notify many objects in your app then use NotificationCenter.

Hope it helps. Let me know if something isn't clear I will try to explain.

UPDATE:

Read it once more and yes Notifications/Delegate from the SecondVC won't work if you leave that screen. Leaving the screen will deinit everything you had there. Therefore you need some, for example, singleton class that will be hold in the memory all the time. So. from the SecondVC you set the timer into the singleton and singleton later will fire the Notifications/Delegates to FirstVC (or anywhere else you may need it).

Tung Fam
  • 7,899
  • 4
  • 56
  • 63
  • Thanks! 1. For example if I go with NotificationCenter. How will I implement it? The thing is that the function mentioned in the post works only when you have loaded the secondVC so the coreData isn't updated. In order for me to send a notification to firstVC when the secondVC label/coreData is updated, I need the function to run in secondVC even if the user is in firstVC. 2. How will I implement this when there are multiple elements involved like in tableView. Some sample code can help me here. Thanks! – Osama Naeem Feb 28 '18 at 05:16
  • Just checked the updated answer. The thing is that I am in a way saving this time in coreData which I can access from anywhere. How can I implement Singleton? I don't have much experience with it. Sorry! – Osama Naeem Feb 28 '18 at 20:13
  • sorry but it's a different question now. you may google it easily. we can't answer all the questions in 1 question :))) try googling it it's easy – Tung Fam Mar 01 '18 at 12:22
0

I found an excellent discussion here on stackoverflow that presents and explains many solutions to the type of questions you have, Passing Data between View Controllers

Joakim Danielson
  • 43,251
  • 5
  • 22
  • 52