简体   繁体   English

如何自动并实时在secondVC中发生更改时如何在firstVC中更新UITableViewCell

[英]How to update a UITableViewCell in firstVC when a change occurs in secondVC automatically and in realtime

I have a editviewcontroller(secondVC)(which you access by tapping on the uitableviewcell task) where you get the option of setting a reminder. 我有一个editviewcontroller(secondVC)(您可以通过点击uitableviewcell任务来访问它),在这里您可以选择设置提醒。 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. 在firstVC中的UITableViewCell中,任务前会出现一个图标。现在,我希望一旦触发提醒并发送通知,就可以实时删除任务中的图标。 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". 如果在提醒任务后访问了editVC,我会将当前时间与用户设置的时间进行比较,然后更新一个标签,上面写着“时间到了”。

I want a similar thing to occur with the appropriate cell in the firstVC. 我希望firstVC中的适当单元格发生类似的情况。

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. 时间到了,它会告诉您时间到了,当您返回第一个VC时,响铃图标将被删除。 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. 但是我希望它实时发生,即使您在firstVC中,也不必转到secondVC然后返回firstVC来获取更改。

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. 简而言之,我希望在第二个VC中设置的任务提醒用户时删除响铃图标。 Thanks! 谢谢!

EditVC: EditVC: 在此处输入图片说明

Code: The following code is executed in the editVC in viewDidLoad. 代码:在viewDidLoad中的editVC中执行以下代码。 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

    }

您可以用来触发发布通知。发布通知用于执行操作,而无需转到特定的VC。

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. 首先,在第一个vc上注册发布通知,然后在secondvc上触发在第一个vc上注册的通知。 It's simple. 这很简单。 If you can't get it then i will send some code for easily got it. 如果您无法获得它,那么我将发送一些代码以轻松获得它。

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. 因为这个时间是用来第一个vc方法注册的。 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. 还有一件事,请在重新加载表后管理数组以准确编码。

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 . 我可以立即看到的代码问题是,您只能在编辑VC的viewDidLoad说“时间到了”。 What happens if time is up a few seconds (or even a second) after viewDidLoad ? 如果在viewDidLoad之后几秒钟(甚至一秒钟)时间到了,该怎么办?

If this were my code, I would have a Timer property on BOTH the editVC and on the parent (or first) view controller. 如果这是我的代码,我想有一个Timer editVC和家长(或第一)视图控制器属性。 It would look like this: 它看起来像这样:

var timesUpTimer : Timer?

Which you can set up in viewWillAppear : 您可以在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. 并且您将在firstVC中的doSomething执行适当的操作。

Also, in viewWillDisappear for each view controller, you need to invalidate and set your Timer property to nil. 同样,在每个视图控制器的viewWillDisappear中,您需要使Timer属性无效并将其Timer属性设置为nil。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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