0

I have a editviewcontroller(secondVC)(which you access by tapping on the uitableviewcell task) where you get the option of setting a reminder. When you set the reminder; an icon appears in front of the task in the UITableViewCell in the firstVC.Now I want that once the reminder is triggered and a notification is sent the icon from the task is removed in realtime. Currently, the way I have set it; if you visit the editVC after the task has been reminded, i compare the current time to time set by the user and then updated a label which says "Time's up".

I want a similar thing to occur with the appropriate cell in the firstVC.

FIRSTVC: FIRSTVC

When the time is up, it tells you that time is up and when you return to the firstVC the bell icon is removed. But I want it to happen in realtime even if you are in the firstVC and you don't have to go to the secondVC and then return to firstVC to get the changes.

In short, i want the bell icon to be removed when a task has been reminded to the user which is set in the secondVC. Thanks!

EditVC: enter image description here

Code: The following code is executed in the editVC in viewDidLoad. If the current time is more than the time selected, it changes the label to "Time's Up" and changes the bellicon tintcolor to white for that specific reminder.

      guard let selectedDate = editnotes?.sSelectedDate,
        var needsToRemind = editnotes?.sReminderState else {

            print("nil")
            return
    }

    if selectedDate <= Date() && needsToRemind {
        editnotes?.sReminderDate = "Time's up"
        editnotes?.belliconcolor = .white
        reminderMsg.text = editnotes?.sReminderDate

    }
Osama Naeem
  • 1,830
  • 5
  • 16
  • 34

4 Answers4

1

You can use to fire post notification.post notification is used to perform an action without going to particular VC.

1

Do you know about post notification or Notification center? If yes, then it is easy to implement in your code otherwise you need some R&D on its. First of all, register post notification on first vc then after on secondvc fire this notification which is register on first vc. It's simple. If you can't get it then i will send some code for easily got it.

  • I got the idea behind this. The only issue is how willI be able to know which row of the tableview has to be edited? And is there any way I can fire this notification at a specific time(sSelectedDate?) – Osama Naeem Feb 25 '18 at 06:26
1

You can fire when your timer stop. And one more important things that when you fire notification , you must need to pass current stop time. Because this time is used to first vc method which is register. In this method, you can compare your reminder time and your current time which is passed by notification if both are same then you can hide the bell otherwise not. One more thing, please Manage array to accurate the code after then reload table.

0

One problem with your code that I can see right away is that you will only say "Time's up" in your edit VC's viewDidLoad. What happens if time is up a few seconds (or even a second) after viewDidLoad?

If this were my code, I would have a Timer property on BOTH the editVC and on the parent (or first) view controller. It would look like this:

var timesUpTimer : Timer?

Which you can set up in viewWillAppear:

if let selectedDate = editnotes?.sSelectedDate
{
    self.timesUpTimer = Timer(fireAt: date, interval: 0, target: self, selector: #selector(doSomething), userInfo: nil, repeats: false)
    RunLoop.main.add(timesUpTimer, forMode: RunLoopMode.commonModes)
}

// in the editVC you can have this:
func doSomething()
{
    editnotes?.sReminderDate = "Time's up"
    editnotes?.belliconcolor = .white
    reminderMsg.text = editnotes?.sReminderDate
}

and you would do the appropriate thing in the doSomething you have in the firstVC.

Also, in viewWillDisappear for each view controller, you need to invalidate and set your Timer property to nil.

Michael Dautermann
  • 88,797
  • 17
  • 166
  • 215
  • Thanks I will try this code. For the firstVC how do I pinpoint which tableviewcell task reminder has been completed and triggered? Thats where I am having issues? – Osama Naeem Feb 23 '18 at 20:27
  • I don't see any code from your firstVC so I'll have to suggest generally: It sounds like each tableviewcell in your firstVC has a date to go with it. You can create as many Timers as you want (you can even put them in an array, like "`var timerArray : [Timer]`"). Then, you'd simply need to figure out which row need to get updated when a timer fires. – Michael Dautermann Feb 23 '18 at 20:34
  • Yes, those with a reminder set has reminderDate. So I can set a timer that gets fired at the reminderDate and then removes the bellicon from specific row? Correct? – Osama Naeem Feb 24 '18 at 10:51
  • Hey, just need some help in figuring this logic out. The first view controller has a tableview and I just can't figure out how to implement timers with it. How do I keep track of which date/time to use to fire timer that updates the corresponding cell? I have simple TableVC with NSFetchedresultscontroller. – Osama Naeem Feb 24 '18 at 22:08