gpt4 book ai didi

ios - 不同类中的 Swift 对象不更新

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

我需要在我的变量重置的同时将步进器和标签重置回 0。问题是步进器和标签在不同的类中,并且在变量重置时不会重置。我尝试使用委托(delegate)(如果有人能告诉我最好的方法,那就太好了)而不是我的 View Controller 的实例,但我无法使任何东西工作。提前感谢您的帮助。

View Controller :

class ViewController: UIViewController 
{
var colors = CircleView()

@IBOutlet weak var circleView1: CircleView!
@IBOutlet weak var blueStepper: UIStepper!
@IBOutlet weak var greenStepper: UIStepper!
@IBOutlet weak var redStepper: UIStepper!

@IBAction func stepperChange(sender: UIStepper)
{
circleView1.redd1 = Int(redStepper.value);
redValue.text = Int(sender.value).description;
}
@IBAction func stepperChange1(sender: UIStepper)
{
circleView1.greenn1 = Int(greenStepper.value);
greenValue.text = Int(sender.value).description;
}
@IBAction func stepperChange2(sender: UIStepper)
{
circleView1.bluee1 = Int(blueStepper.value);
blueValue.text = Int(sender.value).description;
}
}

界面 View :

class CircleView: UIView 
{
var colors1=ViewController()
func updateStepper
{
if(redd1==Int(red1)&&greenn1==Int(green1)&&bluee1==Int(blue1))
{
redd1=0;
greenn1=0;
bluee1=0;
colors1.redStepper.value=0.0;//
colors1.greenStepper.value=0.0;//
colors1.blueStepper.value=0.0;//
}
}
}

最佳答案

我不太理解你的代码,比如你的 CircleView 中的“if”条件,方法“updateStepper”缺少参数。我假设你只是写了一些“swift-pseucode”,我会忽略它的某些部分来解释你如何为它实现一个委托(delegate)。下面是一个示例代码:

import UIKit

protocol CircleViewDelegate: class {
func updateStepper(view: CircleView)
}


class ViewController: UIViewController, CircleViewDelegate{

@IBOutlet weak var circleView1: CircleView!
@IBOutlet weak var blueStepper: UIStepper!
@IBOutlet weak var greenStepper: UIStepper!
@IBOutlet weak var redStepper: UIStepper!

var circleViewDelegate: CircleView!

override func viewDidLoad() {
super.viewDidLoad()
circleViewDelegate = circleView1
circleViewDelegate!.delegate = self
}

func updateStepper(view: CircleView) {
//code you want to execute when you call updateStepper() in the CircleView()
}

}

class CircleView: UIView {

weak var delegate: CircleViewDelegate?

func updateStepper() {
//whenever you want your viewController to updated other views based
//on a condition inside an element like UIView, you can use a delegate
//this way, your code is executed by the ViewController whenever you want
delegate?.updateStepper(self)
}

}

您的 UIView 中的回调必须设置为在需要时调用“updateStepper”。不幸的是,根据你的问题,我不太明白应该调用它的时间。

希望对您有所帮助!

关于ios - 不同类中的 Swift 对象不更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35350393/

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