[英]Swift Objects in different class not updating
I need the stepper and label to reset back to 0 at the same time that my variables reset. 在变量重置的同时,我需要步进器和标签将其重置为0。 The problem is the steppers and labels are in a different class and are not resetting when the variables do.
问题在于步进器和标签位于不同的类中,并且在变量起作用时不会重置。 I tried using delegates(if someone can show me the best way that would be great) instead of an instance of my view controller, but I can't get anything to work.
我尝试使用委托(如果有人可以向我展示最好的最佳方式),而不是使用我的视图控制器实例,但是我什么也做不了。 Thanks for any help in advance.
感谢您的任何帮助。
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: 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;//
}
}
}
I do not quite understand your code, like the "if" condition in your CircleView, the lack of parameters to the method "updateStepper". 我不太了解您的代码,例如CircleView中的“ if”条件,方法“ updateStepper”缺少参数。 I am assuming you just wrote some "swift-pseucode" and I will ignore some parts of it to explain how you could implement a delegate for it.
我假设您只是编写了一些“ swift-pseucode”,而我将忽略其中的某些部分以解释如何实现该代理。 Below is an example code:
下面是一个示例代码:
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)
}
}
A callback in your UIView must be set to call "updateStepper" when you want. 如果需要,必须将UIView中的回调设置为调用“ updateStepper”。 Unfortunately, I didn't quite understand the time it should be called according to your question.
不幸的是,根据您的问题,我不太了解应该调用它的时间。
I hope this helps! 我希望这有帮助!
Have you tried NSNotification
? 您是否尝试过
NSNotification
?
If it's always going to reset to zero, then create a func without the if statement in CircleView: 如果总是将其重置为零,则在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
}
Also in CircleView's createView func
, add: 同样在CircleView的
createView func
,添加:
NSNotificationCenter.defaultCenter().addObserver(self, selector: "resetStepper:", name: "ResetStepper", object: nil)
Then in the view controller, post a notification from whichever button is calling it. 然后在视图控制器中,从调用它的任何按钮发布通知。
@IBAction func callReset(sender: AnyObject) {
NSNotificationCenter.defaultCenter().postNotificationName("ResetStepper", anObject: nil)
}
That will send the notification that CircleView is listening for to call the function. 这将发送CircleView正在监听以调用该函数的通知。
Hope that works for you. 希望对您有用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.