简体   繁体   中英

How to run a function in a secondVC when you are in firstVC?

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: 在此处输入图片说明

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

在此处输入图片说明

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. 在此处输入图片说明

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).

我在这里找到了关于stackoverflow的精彩讨论,该讨论提出并解释了您所遇到的问题类型的许多解决方案,即在View Controller之间传递数据。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM