gpt4 book ai didi

ios - 从firstFile.swift/viewController 调用secondFile.swift/viewController 中的函数?

转载 作者:行者123 更新时间:2023-11-30 10:53:36 26 4
gpt4 key购买 nike

我试图从第二个 ViewController 中创建的另一个 function() 调用第一个 ViewController 中创建的 function() 。

这是一个更新firstViewController中按钮标题的函数。

我已经搜索过,但找不到方法。

第一个 ViewController//ViewController.swift

import UIKit


class ViewController: UIViewController, UITextFieldDelegate {

override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
weightLabel.delegate = self

}




@IBAction func excerciseChooserButton(_ sender: UIButton) {
}

var weight = 0 {
didSet {
weightLabel.text = "\(weight)"
}
}

// User input WEIGHT

@IBOutlet weak var weightLabel: UITextField!

func textField(_ weightLabel: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
let isNumber = CharacterSet.decimalDigits.isSuperset(of: CharacterSet(charactersIn: string))
let withDecimal = (
string == NumberFormatter().decimalSeparator &&
weightLabel.text?.contains(string) == false
)
return isNumber || withDecimal
}



@IBAction func plusWeight(_ sender: UIButton) {
weight += 5
}

@IBAction func minusWeight(_ sender: UIButton) {
weight -= 5
}

// User input REPS


@IBOutlet weak var repLabel: UILabel!

@IBAction func repSlider(_ sender: UISlider) {
let currentRepValue = Int(sender.value)
repLabel.text = "\(currentRepValue)"
let cm = Calculator(weight: weightLabel.text!, reps: repLabel.text!)
let result = cm.calcRM()
repMax.text = "1RM: \(result)kg"

}


@IBOutlet weak var repMax: UILabel!

@IBOutlet weak var excerciseLabel: UIButton!

func changeText() {
excerciseLabel.setTitle(Excercises.excChosen, for: .normal)
print(excerciseLabel)
}


@IBAction func unwindToViewController(segue:UIStoryboardSegue) {
}
}

////////

第二个 ViewController//ExcerciseChooserViewController.swift

import UIKit


struct Excercises {
static var excChosen:String? = ""
}

class ExcerciseChooserViewController: UIViewController, UITableViewDelegate, UITableViewDataSource



// Data model: These strings will be the data for the table view cells
let excercises: [String] = ["Bench Press", "Squat", "Push Press", "Deadlift"]

// cell reuse id (cells that scroll out of view can be reused)
let cellReuseIdentifier = "cell"

// don't forget to hook this up from the storyboard
@IBOutlet var tableView: UITableView!




override func viewDidLoad() {
super.viewDidLoad()

// Register the table view cell class and its reuse id
self.tableView.register(UITableViewCell.self, forCellReuseIdentifier: cellReuseIdentifier)

// (optional) include this line if you want to remove the extra empty cell divider lines
// self.tableView.tableFooterView = UIView()

// This view controller itself will provide the delegate methods and row data for the table view.
tableView.delegate = self
tableView.dataSource = self
}

// number of rows in table view
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return self.excercises.count
}

// create a cell for each table view row
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

// create a new cell if needed or reuse an old one
let cell:UITableViewCell = self.tableView.dequeueReusableCell(withIdentifier: cellReuseIdentifier) as UITableViewCell!

// set the text from the data model
cell.textLabel?.text = self.excercises[indexPath.row]

return cell
}


// method to run when table view cell is tapped
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let excerciseChosen = "\(excercises[indexPath.row])"
print("You tapped cell number \(indexPath.row).")
print(excerciseChosen)
goBackToOneButtonTapped((Any).self)
Excercises.excChosen = excerciseChosen
print(Excercises.excChosen!)

// call function to update text

ViewController.changeText()


}





@IBAction func goBackToOneButtonTapped(_ sender: Any) {
performSegue(withIdentifier: "unwindToViewController", sender: self)

}

}

最佳答案

unwindToViewController 调用它,当第一个 View Controller 不可见时不需要调用它

关于ios - 从firstFile.swift/viewController 调用secondFile.swift/viewController 中的函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54200774/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com