来自其他 viewController 的 func 的值
value from func from other viewController
我正在尝试编写简单的答题器,但我遇到了一些问题,在 "StartViewController" 我有 func value()
它每秒添加 1 + 单击 1 + 最后保存值并且我有 shopViewController
在 shopViewController
上我有按钮,当我按下它时,它必须给 +1(每次)才能单击,但我如何才能从 func value()
访问当前值?当我尝试获取当前值时,我得到 nil
func value() -> Float {
let endValue = appPerSec + valueToLoad + click
valueToSave = endValue
valueLable.text = "\(endValue)"
return valueToSave
}
// shopViewController
var StartView:StartViewController!
var currentValue:Float! = 0.0
@IBAction func testAdd(_ sender: Any) {
currentValue = StartView.value // here I get NIL
print(currentValue)
}
您可以使用委托和协议。在您的 shopView 上方添加:
protocol ShopProtocol{
func updateCount();
}
在您的商店视图中添加:
var delegateShop:ShopProtocol?
当您更改该变量时,转到:
delegateShop.updateCount()
在您的主视图控制器中,当您呈现该商店时ViewController(在呈现之前),添加:
shopController.delegateShop = self
并将您的 ViewController 定义更改为
class ...: UIViewController, ..., ShopProtocol{
当然,在那个视图控制器中创建:
func updateCount(){
//do Stuff
}
我不明白你的问题,但我可以根据我能阅读的条款给出部分解决方案,或者请改进你的问题,以便我能准确理解你在寻找什么。
与此同时,我将只提供一些我认为您正在寻找的概念,如果没有用,请在评级为无用之前编辑您的问题:
制作一个你可以使用的计时器
@IBOutlet weak var timeInTimer: UILabel!
var timer = Timer()
@IBAction func buttonPressed(_ sender: Any) //to stop the timer
{
timer.invalidate()
}
@IBAction func playButtonPressed(_ sender: Any) //to start the timer
{
timer = Timer.scheduledTimer(timeInterval: 1, target: self, selector: #selector(ViewController.processTimer), userInfo: nil, repeats: true)
}
将数据从一个视图传递到另一个视图:
let anyObjectName = storyboard?.instantiateViewController(withIdentifier: "IdentifierOfSecondViewController") as! IdentifierOfSecondViewController
anyObjectName.userName = userName //where username is a variable in second view
我正在尝试编写简单的答题器,但我遇到了一些问题,在 "StartViewController" 我有 func value()
它每秒添加 1 + 单击 1 + 最后保存值并且我有 shopViewController
在 shopViewController
上我有按钮,当我按下它时,它必须给 +1(每次)才能单击,但我如何才能从 func value()
访问当前值?当我尝试获取当前值时,我得到 nil
func value() -> Float {
let endValue = appPerSec + valueToLoad + click
valueToSave = endValue
valueLable.text = "\(endValue)"
return valueToSave
}
// shopViewController
var StartView:StartViewController!
var currentValue:Float! = 0.0
@IBAction func testAdd(_ sender: Any) {
currentValue = StartView.value // here I get NIL
print(currentValue)
}
您可以使用委托和协议。在您的 shopView 上方添加:
protocol ShopProtocol{
func updateCount();
}
在您的商店视图中添加:
var delegateShop:ShopProtocol?
当您更改该变量时,转到:
delegateShop.updateCount()
在您的主视图控制器中,当您呈现该商店时ViewController(在呈现之前),添加:
shopController.delegateShop = self
并将您的 ViewController 定义更改为
class ...: UIViewController, ..., ShopProtocol{
当然,在那个视图控制器中创建:
func updateCount(){
//do Stuff
}
我不明白你的问题,但我可以根据我能阅读的条款给出部分解决方案,或者请改进你的问题,以便我能准确理解你在寻找什么。
与此同时,我将只提供一些我认为您正在寻找的概念,如果没有用,请在评级为无用之前编辑您的问题:
制作一个你可以使用的计时器
@IBOutlet weak var timeInTimer: UILabel!
var timer = Timer()
@IBAction func buttonPressed(_ sender: Any) //to stop the timer
{
timer.invalidate()
}
@IBAction func playButtonPressed(_ sender: Any) //to start the timer
{
timer = Timer.scheduledTimer(timeInterval: 1, target: self, selector: #selector(ViewController.processTimer), userInfo: nil, repeats: true)
}
将数据从一个视图传递到另一个视图:
let anyObjectName = storyboard?.instantiateViewController(withIdentifier: "IdentifierOfSecondViewController") as! IdentifierOfSecondViewController
anyObjectName.userName = userName //where username is a variable in second view