多个实例中的Swift唯一属性

时间:2014-09-21 09:07:30

标签: properties swift instance

我知道理论上可以使用对每个实例具有不同值的属性创建同一个类(或我的子类中的子类)的多个实例。

事情是,我不能让它发生!

每次我创建一个新实例时,它都会获取其他实例的属性值,当我为实例更改一个值时,它也会更改另一个实例。

所以我的猜测是我做错了(显然),比如访问类属性值而不是实例属性值......这是代码。

class CustomUIImageView : UIImageView { //subclass of UIImageView

var someParameter : Bool = false //this is the property I want to be different in each version of the instance. 


}


class ClassSiege : UIViewController, UIGestureRecognizerDelegate {



var myView: CustomUIImageView! //the instance declaration. 

func handleTap (sender: UITapGestureRecognizer) {

    println("value of someParameter \(self.myView.someParameter)") //I use this gesture recognizer to find out the value of the instance I'm tapping on. 

}

func handlePan(recognizer:UIPanGestureRecognizer) {
    let iv : UIView! = recognizer.view
    let translation = recognizer.translationInView(self.view)
    iv.center.x += translation.x
    iv.center.y += translation.y
    recognizer.setTranslation(CGPointZero, inView: self.view)

    var centerBoardX = BlackBoard.center.x //blackboard is a fixed image on the screen. 
    var centerBoardY = BlackBoard.center.y
    var centerRondX = iv.center.x
    var centerRondY = iv.center.y



    if centerRondY - centerBoardY < 100 { 

      self.myView.someParameter = true //if the distance between myView and the blackboard is under 100 I want the instance's property to become true. 



    } else {
        self.myView.someParameter = false //on the other hand, if the distance is greater than 100, I want it to be false. 


        }

}

  //when user pushes a button, it triggers this func that creates a new instance of myView and add it to the screen. 
 @IBAction func showContent(sender: AnyObject) {

    some code...
   //here I'm creating the instance of the view and I give it the gesture recognizer parameters. I don't think that relevant to the issue, so I'm not adding the code. 

}

很明显,这不是做这件事的好方法,但是有什么不对,怎么做呢? 谢谢!

2 个答案:

答案 0 :(得分:0)

根据您的相关question确定答案。

如果要实现的是使用您提供的值初始化属性,只需向初始化程序添加新参数即可。例如,如果您使用传入CGRect的初始化程序,那么您可以实现这样的初始化程序:

class CustomUIImageView : UIImageView {
    let someParameter : Bool

    required init(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

     init(frame: CGRect, someParameter: Bool) {
        self.someParameter = someParameter
        super.init(frame: frame)
    }
}

我希望这就是你要找的东西 - 让我知道。

答案 1 :(得分:0)

我找到了解决方案,如果您一直面临相同的问题,请按照以下方法处理。 秘诀是向下转换Recognizer.view以获取子类CustomUIImageView的参数。

这里是如何:

func handleTap (sender: UITapGestureRecognizer) {

println("value of someParameter \(self.myView.someParameter)") //I use this gesture recognizer to find out the value of the instance I'm tapping on. 

}

func handlePan(recognizer:UIPanGestureRecognizer) {
let iv : UIView! = recognizer.view
let translation = recognizer.translationInView(self.view)
iv.center.x += translation.x
iv.center.y += translation.y
recognizer.setTranslation(CGPointZero, inView: self.view)

var centerBoardX = BlackBoard.center.x //blackboard is a fixed image on the screen. 
var centerBoardY = BlackBoard.center.y
var centerRondX = iv.center.x
var centerRondY = iv.center.y

var myParameter = recognizer.view as CustomUIImageView //<- this is the key point. Downcasting let you access the custom subclass parameters of the object that is currently moved

if centerRondY - centerBoardY < 100 { 

  myParameter.someParameter = true //so now I'm really changing the parameter's value inside the object rather than changing a global var like I did before. 



} else {
    myParameter.someParameter = false 

    }

}

//when user pushes a button, it triggers this func that creates a new instance of myView and add it to the screen. 
@IBAction func showContent(sender: AnyObject) {

some code...
 //here I'm creating the instance of the view and I give it the gesture recognizer parameters. I don't think that relevant to the issue, so I'm not adding the code. 

 }