ios - 如何从一行开始并在新 View Controller 中显示该行的名称?

标签 ios swift uitableview delegates

我正在构建一个任务计时器应用程序,我正在使用它来查看哪些有效,哪些无效。我是初学者,我不确定如何解决这个问题。我想要发生的事情是让用户能够单击直接连接到计时器 View Controller 的表行,并使所选行中的字符串值显示在计时器 View Controller 的标签中。我已尝试通过几种不同的方式来做到这一点,但无法完全理解这些不同的技术。

这是我的计时器的 View Controller

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

这是我的表格 View 的代码

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

}
}

任何帮助将不胜感激......我希望这个问题有意义

最佳答案

据我了解,您想在 segues 时将字符串值从一个 View 传递到另一个 View 。一种方法是在计时器 View Controller 和其他 View 中创建一个字符串变量,然后再转到计时器 View ,让它将该变量设置为行中的显示文本。然后在计时器 View Controller 上,您现在可以使用该变量,因为它是由调用该 segue 的人设置的。 (在override func prepare(segue...)方法中)

很抱歉没有编写任何代码,我目前不在我可以的环境中。

关于ios - 如何从一行开始并在新 View Controller 中显示该行的名称?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45201368/

相关文章:

ios - 发布流时出现内部错误

swift - 快速捕获列表自身

ios - 同时运行 Collection View 和 TableView 时应用程序崩溃。崩溃 :fatal error: unexpectedly found nil while unwrapping an Optional value

ios - NSMutableArray/UITableView

iphone - UITableView 滚动缓慢,内存泄漏问题

ios - 快速 - UITableview 在滚动时变得不稳定

ios - 重新捕获 block 内弱引用对象的强引用

iOS 自动布局问题 : Unable to simultaneously satisfy constraints

ios - 如何在我的类(class)中使用协议(protocol)和委托(delegate)?

iphone - __block 变量未保留