不同类中的Swift对象不更新

时间:2016-02-11 21:27:54

标签: ios swift class object delegates

我需要步进器和标签在我的变量重置的同时重置为0。问题是步进器和标签属于不同的类,并且在变量没有重置时。我尝试使用代理(如果有人可以向我展示最好的方式)而不是我的视图控制器的实例,但我无法获得任何工作。感谢您提前提供任何帮助。

的ViewController:

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

的UIView:

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;//
    }
  }
}

2 个答案:

答案 0 :(得分:2)

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

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”。不幸的是,我不太明白根据你的问题应该调用的时间。

我希望这有帮助!

答案 1 :(得分:1)

您是否尝试过NSNotification

如果它总是重置为零,那么在CircleView中创建一个没有if语句的函数:

func resetStepper(not: NSNotification) {
  r1 = 0
  g1 = 0
  b1 = 0
  c1.rStep.value = 0.0
  c1.bStep.value = 0.0
  c1.gStep.value = 0.0
}

同样在CircleView的createView func中,添加:

NSNotificationCenter.defaultCenter().addObserver(self, selector: "resetStepper:", name: "ResetStepper", object: nil)

然后在视图控制器中,从任何调用它的按钮发布通知。

@IBAction func callReset(sender: AnyObject) {
  NSNotificationCenter.defaultCenter().postNotificationName("ResetStepper", anObject: nil)
}

这将发送CircleView正在侦听的通知来调用该函数。

希望对你有用。