Swift 访问结构/类中的引用
我正在尝试使用数组上的循环来实现对iOS UI元素的控制。但是,在我将更改应用于框架项时,它似乎应用于值,而不是引用。如果我对UI元素本身进行相同的更改,它就会工作 在C语言中,我会说,Swift 访问结构/类中的引用,swift,pass-by-reference,Swift,Pass By Reference,我正在尝试使用数组上的循环来实现对iOS UI元素的控制。但是,在我将更改应用于框架项时,它似乎应用于值,而不是引用。如果我对UI元素本身进行相同的更改,它就会工作 在C语言中,我会说,&someElement,并在处理过程中取消对它的引用。我如何在Swift中实现同样的目标 class UIComponentLocationStruct { var control: CGRect // might be a UILabel, UIButton, or some other item va
&someElement
,并在处理过程中取消对它的引用。我如何在Swift中实现同样的目标
class UIComponentLocationStruct {
var control: CGRect // might be a UILabel, UIButton, or some other item
var xOffset: Int
var yOffset: Int
init( control:CGRect, xOffset:Int, yOffset:Int ){
self.control = control
self.xOffset = xOffset
self.yOffset = yOffset
}
}
class rowOfComponents {
var yOffset: Int
var components: [UIComponentLocationStruct]
init( yOffset:Int, components:[UIComponentLocationStruct]? ){
self.yOffset = yOffset
self.components = components!
}
}
func buildComponentArray() {
componentArray.append( rowOfComponents( yOffset: 190, components: [
UIComponentLocationStruct( control: secondsLabel.frame, xOffset: 0, yOffset: 0 ),
UIComponentLocationStruct( control: tenthsLabel.frame, xOffset: 250, yOffset: 0 )] ) )
}
func updateControlLocations () {
let width = self.view.bounds.width
let xOffset: Int = Int( (width - 280) / 2 )
for i in 0 ..< componentArray.count {
for ii in 0 ..< componentArray[i].components.count {
var frame:CGRect = componentArray[i].components[ii].control
frame.origin.x = CGFloat( xOffset + componentArray[i].components[ii].xOffset )
frame.origin.y = CGFloat( componentArray[i].yOffset + componentArray[i].components[ii].yOffset )
componentArray[i].components[ii].control = frame // <-- This is not updating the UI element
}
}
}
类UIComponentLocationStruct{
变量控件:CGRect//可能是UILabel、UIButton或其他项目
变量xOffset:Int
变量yOffset:Int
init(控件:CGRect,xOffset:Int,yOffset:Int){
自我控制
self.xOffset=xOffset
self.yOffset=yOffset
}
}
组件类{
变量yOffset:Int
变量组件:[UIComponentLocationStruct]
init(yOffset:Int,components:[UIComponentLocationStruct]?){
self.yOffset=yOffset
self.components=组件!
}
}
func buildComponentArray(){
componentArray.append(组件行)(yOffset:190,组件:[
UIComponentLocationStruct(控件:secondsLabel.frame,xOffset:0,yOffset:0),
UIComponentLocationStruct(控件:tenthLabel.frame,xOffset:250,yoOffset:0)])
}
func updateControlLocations(){
让宽度=self.view.bounds.width
设xOffset:Int=Int((宽度-280)/2)
对于0中的i..control:CGRect
更改为control:UIView
如何维护对控件本身的引用(而不仅仅是获取其布局框的副本)?因为我有UIPickerView、UIButton、UILabel、UISwitch等,它们都没有UIView立即作为一个超类。但是它们都有帧,都是CGRect。它们都有帧,因为它们都是UIView的子类。有些是UIControl的子类,是的,但UIControl本身是UIView的子类。你可以参考任何其中一个是UIView。最后回到这一点:将其更改为UIView有效。谢谢!将control:CGRect
更改为control:UIView
如何保持对控件本身的引用(而不仅仅是获取其布局框的副本)?因为我有UIPickerView、UIButton、UILabel、UISwitch等,它们都没有UIView立即作为一个超类。但是它们都有帧,都是CGRect。它们都有帧,因为它们都是UIView的子类。有些是UIControl的子类,是的,但UIControl本身是UIView的子类。你可以参考任何其中一个是UIView。最后回到这一点:将其更改为UIView有效。谢谢!