我正在构建一个任务计时器应用程序,我正在努力寻找能够发挥作用但不起作用的应用程序。我是初学者,我不知道如何解决这个问题。我想要发生的是用户能够点击直接切换到定时器视图控制器的表行,并且所选行中的字符串值显示在定时器视图控制器的标签中。我试图以几种不同的方式做到这一点,但不能围绕不同的技术。
这是我的计时器的视图控制器
import UIKit
class ViewController: UIViewController, UITextFieldDelegate {
@IBOutlet weak var timerLabel: UILabel!
@IBOutlet weak var pauseButton: UIButton!
@IBOutlet weak var startButton: UIButton!
@IBOutlet weak var timerTaskLabel: UILabel!
var seconds = 10
var timer = Timer()
var isTimerRunning = false
var resumeTapped = false
@IBAction func cancel(_ sender: Any) {
dismiss(animated: true, completion: nil)
}
@IBAction func startButtonTapped(_ sender: Any) {
if isTimerRunning == false {
runTimer()
self.startButton.isEnabled = false
}
}
func runTimer() {
timer = Timer.scheduledTimer(timeInterval: 1, target: self, selector: (#selector(ViewController.updateTimer)), userInfo: nil, repeats: true)
isTimerRunning = true
pauseButton.isEnabled = true
}
@IBAction func pauseButtonTapped(_ sender: UIButton) {
if self.resumeTapped == false {
timer.invalidate()
self.resumeTapped = true
self.pauseButton.setTitle("Resume",for: .normal)
} else {
runTimer()
self.resumeTapped = false
self.pauseButton.setTitle("Pause",for: .normal)
}
}
@IBAction func resetButtonTapped(_ sender: Any) {
timer.invalidate()
seconds = 60
self.timerLabel.text = timeString(time: TimeInterval(seconds))
if self.resumeTapped == true {
self.resumeTapped = false
self.pauseButton.setTitle("Pause",for: .normal)
}
isTimerRunning = false
pauseButton.isEnabled = false
startButton.isEnabled = true
}
func updateTimer() {
if seconds < 1 {
timer.invalidate()
//Send alert to indicate "time's up!"
} else {
seconds -= 1
timerLabel.text = timeString(time: TimeInterval(seconds))
}
}
func timeString(time:TimeInterval) -> String {
let hours = Int(time) / 3600
let minutes = Int(time) / 60 % 60
let seconds = Int(time) % 60
return String(format:"%02i:%02i:%02i", hours, minutes, seconds)
}
override func viewDidLoad() {
super.viewDidLoad()
pauseButton.isEnabled = false
self.timerLabel.text = timeString(time: TimeInterval(seconds))
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
这是我的表格视图的代码
import UIKit
class TaskListViewController: UITableViewController,
AddNewTaskViewControllerDelegate {
var tasks: [TaskData]
required init?(coder aDecoder: NSCoder) {
tasks = [TaskData]()
let row0item = TaskData()
row0item.task = "learn French"
row0item.time = "500"
tasks.append(row0item)
super.init(coder: aDecoder)
}
//DELEGATE PROTOCOLS
func addNewTaskViewControllerDidCancel(_ controller: AddNewTaskViewController) {
dismiss(animated: true, completion: nil)
}
func addNewTaskViewController(_ controller: AddNewTaskViewController, didFinishAdding item: TaskData) {
let newRowIndex = tasks.count
tasks.append(item)
print("\(item.time)")
let indexPath = IndexPath(row: newRowIndex, section: 0)
let indexPaths = [indexPath]
tableView.insertRows(at: indexPaths, with: .automatic)
dismiss(animated: true, completion: nil)
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "AddTask" {
let navigationController = segue.destination as! UINavigationController
let controller = navigationController.topViewController as! AddNewTaskViewController
controller.delegate = self
}
}
//DELEGATE PROTOCOLS
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
override func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {
tasks.remove(at: indexPath.row)
let indexPaths = [indexPath]
tableView.deleteRows(at: indexPaths, with: .automatic)
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return tasks.count
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "TaskListItem", for: indexPath)
let taskItem = tasks[indexPath.row]
let label = cell.viewWithTag(1) as! UILabel
label.text = taskItem.task
return cell
}
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
}
}
非常感谢任何帮助......我希望这个问题有意义
答案 0 :(得分:1)
根据我的理解,你想在一个视图中传递一个字符串值到另一个视图。您可以这样做的一种方法是在计时器视图控制器和其他视图中创建一个字符串变量,然后再将其设置为定时器视图,让它将该变量设置为该行的显示文本。然后在计时器视图控制器上你可以立即使用该变量,因为它是由调用segue的人设置的。 (在override func prepare(segue ...)方法中)
很抱歉没有编写任何代码,我目前无法在当前环境中使用。