简体   繁体   English

在firstVC中时,如何在secondVC中运行功能?

[英]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. 虽然我非常确定可以解决此问题的简单方法,但这里的问题是,secondVC绝对不会与形式或形式与firstVC分开。 The secondVC is what you get when you tap on the tableViewCell. 第二个VC是您点击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. 您点击的单元格(使用indexPath)将一些数据发送到secondVC,然后您可以在secondVC中编辑该任务,甚至为其设置通知提醒。

In the secondVC I am running some code that triggers a timer at the time set by the user for that particular task. 在secondVC中,我正在运行一些代码,该代码在用户为特定任务设置的时间触发计时器。 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. 当此标签为“ Time's Up”时,我将firstVC的tableviewcell中的钟形图标设置为白色,或从特定任务中删除该钟形图标。

I want this peice of code to run even if I am not in the secondVC. 即使我不在secondVC中,我也希望运行这段代码。 (I have this code running in viewDidAppear). (我在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. 如果我将此代码放入函数中,然后在firstVC中调用它,则该代码不知道应该选择哪个时间。

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: 更新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. 当您在secondVC中时,上面的代码有效。 I want this to work even if I am in the firstVC. 我希望即使我在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. 由于我使用的是FetchedResultsController,因此我非常确定如果更改了数据,则会更新tableView,并且由于belliconcolor是我保存在coreData中的元素,因此它会更新tableview。

The question is: How can I run this piece of code from the firstVC for all the elements of the tableview? 问题是:如何从firstVC中为tableview的所有元素运行这段代码? The bellicon appear next to those tasks that you set reminder for in the secondVC. 该钟形图标出现在您在secondVC中设置提醒的那些任务的旁边。 You access secondVC by tapping the relevant cell in the tableview: 您可以通过点击表视图中的相关单元格来访问secondVC: 在此处输入图片说明

You set the reminder in this secondVC. 您可以在第二个VC中设置提醒。 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 如果时间已到,则“到期日期”下方的标签将更改为“时间已到”,特定任务的.white改为.white

在此处输入图片说明

Time's Up! 时间到! -> The piece of code has been excecuted and the belliconcolor for this particular task is .white. ->该代码段已执行,此特定任务的belliconcolor为.white。 This will be apparent when I go to the firstVC. 当我转到firstVC时,这将很明显。 I want this code to run even if I am in the firstVC so that the tableview is automatically updated. 我希望即使我在firstVC中也要运行此代码,以便自动更新tableview。 在此处输入图片说明

Usually, there are 2 ways of doing it: 通常,有两种方法可以做到这一点:

  1. Use NotificationCenter. 使用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. 通常,如果我只希望2个对象之间只有1个连接,则通常使用Delegate。 If you want to Notify many objects in your app then use NotificationCenter. 如果要在应用程序中通知许多对象,请使用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. 再次阅读它,是的,如果您离开该屏幕,则SecondVC中的Notifications / Delegate将不起作用。 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). 从SecondVC中,您将计时器设置为单例,稍后将单例触发到FirstVC的Notifications / Delegates(或您可能需要的其他任何地方)。

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

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

相关问题 如何自动并实时在secondVC中发生更改时如何在firstVC中更新UITableViewCell - How to update a UITableViewCell in firstVC when a change occurs in secondVC automatically and in realtime 在FirstVC中选择单元格后如何为SecondVC中的每个单元调用按钮操作? - How to call button action for each cell in SecondVC after selecting cell in FirstVC? secondVC中的结构数组未从firstVC追加 - Struct Array in secondVC not being appended from the firstVC 如何在应用程序首次加载时运行函数 - How to run a function when an app first loads Swift 5:你如何传递一个函数以在函数内运行 () Noobie in swift - Swift 5 : How do you pass a function to run inside a function () Noobie in swift 如何在点击MKMapView时运行函数但在点击AnnotationView时返回? - How to run function when tap MKMapView but return when tap AnnotationView? 线程1:运行AppDelegate时发出信号SIGABRT - Thread 1: Signal SIGABRT In AppDelegate when you run it 通知触发且应用未运行时如何运行功能 - How to run a function when notification fires and app isn't running 如何运行 function 发送时不显示推送消息? - How to run the function without showing the push message when it is sent? 如何在异步调用完成并且代码在swift3中的asyn函数之外运行代码 - How do you run code after an async call finishes and the code is outside of the asyn function in swift3
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM