ios - 当你在第一个 VC 中时,如何在第二个 VC 中运行一个函数?

标签 ios swift function uitableview uiviewcontroller

虽然我很确定这个问题有一个简单的解决方案,但这里的问题是 secondVC 与 firstVC 没有任何区别或形式。 secondVC 是您点击 tableViewCell 时得到的。您点击的单元格(使用 indexPath)我将一些数据发送到 secondVC,然后您可以在 secondVC 中编辑该任务,甚至为其设置通知提醒。

在 secondVC 中,我正在运行一些代码,在用户为该特定任务设置的时间触发计时器。作为返回,计时器会调用选择器方法来更新标签并将其更改为“Time's Up”。

当这个标签是“时间到了”时,我将 firstVC 的 tableviewcell 中的 bellicon 设置为白色或从特定任务中删除 bellicon。

即使我不在第二个 VC 中,我也希望这段代码能够运行。 (我在 viewDidAppear 中运行了这段代码)。如果我把这段代码放在一个函数中,然后在我在 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)
}


}

更新 belliconcolor 的选择器代码:

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

当您在第二个 VC 中时,上面的代码有效。即使我在第一个 VC 中,我也希望它能正常工作。因为我使用的是 FetchedResultsController,所以我很确定如果数据发生更改,tableView 将被更新,并且由于 belliconcolor 是我保存在 coreData 中的一个元素,更改它会更新 tableview。

问题是:如何从 firstVC 为 tableview 的所有元素运行这段代码? bellicon 出现在您在第二个 VC 中为其设置提醒的那些任务旁边。您可以通过点击表格 View 中的相关单元格来访问 secondVC: enter image description here

您在第二个 VC 中设置了提醒。如果时间到了,则截止日期下方的标签将更改为“时间到了”,并且特定任务的 belliconcolor 将更改为 .white

enter image description here

时间到了! -> 这段代码已经被执行,这个特定任务的 belliconcolor 是.white。当我去 firstVC 时,这会很明显。即使我在 firstVC 中,我也希望此代码运行,以便自动更新 tableview。 enter image description here

最佳答案

通常有两种方法:

  1. 使用 NotificationCenter。
  2. 使用委托(delegate)模式。

两者都可以。如何选择将是一个正确的问题。接听this可能有帮助。

通常,如果我只想在 2 个对象之间建立 1 个连接,我通常会使用 Delegate。如果您想通知应用中的许多对象,请使用 NotificationCenter。

希望对您有所帮助。如果有什么不清楚的地方,请告诉我,我会尽力解释。

更新:

再读一遍,是的,如果您离开该屏幕,来自 SecondVC 的 Notifications/Delegate 将不起作用。离开屏幕将取消你在那里的一切。因此,您需要一些,例如,一直保存在内存中的单例类。所以。从 SecondVC 中,您将计时器设置为单例,稍后单例会将通知/委托(delegate)发送给 FirstVC(或您可能需要的任何其他地方)。

关于ios - 当你在第一个 VC 中时,如何在第二个 VC 中运行一个函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49015119/

相关文章:

ios - Firebase 存储上传在不利网络条件下失败 iOS

ios - 将 subview 添加到容器 View 共享的父 View 会调整容器 View 的大小

swift - 卡特琳娜测试版 5 : Quicktime Audio Recording Not Working on 2018 Macbook Pros sw

swift - 文件 'BluetoothController.swift' 是模块 'CoreBluetooth' 的一部分;忽略导入

c - C代码错误: expected identifier or '(' before '{' token

javascript - 为什么 JavaScript 函数可以在定义之前被访问?

python - 尝试在不使用 for(或类似)循环的情况下对 numpy 数组内的所有子数组执行操作

php - 我的代码沙盒 Paypal future 付款有什么问题

ios - UIPickerView NSInvalidArgumentException',原因: '-[__NSCFString superview]: unrecognized selector sent to instance

swift - 奇怪的编译器错误 : 'expectation' produces 'XCTestExpectation' , 不是预期的上下文结果类型 'XCTestExpectation'