你能解释一下 Swift 中的 "receiver" 和 "view - superview" 吗?
Could you explain "receiver" and "view - superview" in Swift?
override func draw(_ rect: CGRect) {
let size: CGFloat = 20
let currencyLbl = UILabel(frame: CGRect(x: 5, y: (frame.size.height/2) - size, width: size, height: size))
currencyLbl.backgroundColor = #colorLiteral(red: 0.2549019754, green: 0.2745098174, blue: 0.3019607961, alpha: 0.5)
currencyLbl.textAlignment = .center
currencyLbl.textColor = #colorLiteral(red: 0.2549019754, green: 0.2745098174, blue: 0.3019607961, alpha: 1)
currencyLbl.layer.cornerRadius = 5.0
let formatter = NumberFormatter()
formatter.numberStyle = .currency
formatter.locale = .current
currencyLbl.text = formatter.currencySymbol
addSubview(currencyLbl)
}
addSubview 在 Xcode 中的解释是 "Adds a view to the end of the receiver’s list of subviews" 并且它说 "This method establishes a strong reference to view and sets its next responder to the receiver, which is its new superview "
你能解释一下 "receiver" 和 "view - superview" 在那种情况下的情况吗?
在这个例子中
let myView = UIView()
parentView.addSubview(myView) // here myView is a subview
parentView
被称为 myView
的 receiver 或 superView
这个
This method establishes a strong reference to view and sets its next responder to the receiver, which is its new superview.
意味着如果你在一个局部函数中添加了一个子视图到 prentView,子视图应该在函数执行结束时解除分配,那么 addSubview
将持有对该子视图的强引用并阻止解除分配进程
还有这个
Views can have only one superview. If view already has a superview and that view is not the receiver, this method removes the previous superview before making the receiver its new superview
意味着如果你将 myView 添加到 parentView1 ,那么它将是唯一的 superView ,如果你试图将它添加到 parentView2 ,那么它将自动从 parentView1 中删除并添加到 parentView2
还有这个
Adds a view to the end of the receiver’s list of subviews
表示它是一堆子视图,当您将 myView1 添加到 parentView ,然后添加 myView2 时,将导致 myView2 位于 myView1s 之上,因此它将对任何事件都处于活动状态,如果说两者具有相同的框架,则为 myView1 阻止它
"receiver" 是 objective-c 时代的倒退,当时 "messages" 被发送到 "objects"。所以我们老人家(哈)习惯叫[someView addSubview:anotherView]
而不叫someView.addSubview(anotherView)
。在这种情况下,"someView" 是接收方,因为它正在接收 "addSubview" 消息。
视图存储在层次结构中,基本上是一棵树,其中每个视图可以有很多子视图(子视图),但只有一个父视图(父视图)。在我的示例中,"someView" 是超级视图(父视图),其中 "anotherView" 将作为子视图(子视图)添加。
在您的示例中,您正在实施的 class 既有接收器又有超级视图。它是接收者,因为您的 addSubview 调用是隐含的 "self.addSubview",这意味着 "self" 是接收者。这是超级视图,因为 currencyLbl 作为子视图添加到它。
针对您的具体情况明确说明:
- 接收者=自己
- Superview = self
- 视图 = currencyLbl
(注意 - Receiver 和 Superview 不是同义词,在这种情况下它们恰好指的是同一个对象。它们是完全不相关的概念。)
override func draw(_ rect: CGRect) {
let size: CGFloat = 20
let currencyLbl = UILabel(frame: CGRect(x: 5, y: (frame.size.height/2) - size, width: size, height: size))
currencyLbl.backgroundColor = #colorLiteral(red: 0.2549019754, green: 0.2745098174, blue: 0.3019607961, alpha: 0.5)
currencyLbl.textAlignment = .center
currencyLbl.textColor = #colorLiteral(red: 0.2549019754, green: 0.2745098174, blue: 0.3019607961, alpha: 1)
currencyLbl.layer.cornerRadius = 5.0
let formatter = NumberFormatter()
formatter.numberStyle = .currency
formatter.locale = .current
currencyLbl.text = formatter.currencySymbol
addSubview(currencyLbl)
}
addSubview 在 Xcode 中的解释是 "Adds a view to the end of the receiver’s list of subviews" 并且它说 "This method establishes a strong reference to view and sets its next responder to the receiver, which is its new superview "
你能解释一下 "receiver" 和 "view - superview" 在那种情况下的情况吗?
在这个例子中
let myView = UIView()
parentView.addSubview(myView) // here myView is a subview
parentView
被称为 myView
这个
This method establishes a strong reference to view and sets its next responder to the receiver, which is its new superview.
意味着如果你在一个局部函数中添加了一个子视图到 prentView,子视图应该在函数执行结束时解除分配,那么 addSubview
将持有对该子视图的强引用并阻止解除分配进程
还有这个
Views can have only one superview. If view already has a superview and that view is not the receiver, this method removes the previous superview before making the receiver its new superview
意味着如果你将 myView 添加到 parentView1 ,那么它将是唯一的 superView ,如果你试图将它添加到 parentView2 ,那么它将自动从 parentView1 中删除并添加到 parentView2
还有这个
Adds a view to the end of the receiver’s list of subviews
表示它是一堆子视图,当您将 myView1 添加到 parentView ,然后添加 myView2 时,将导致 myView2 位于 myView1s 之上,因此它将对任何事件都处于活动状态,如果说两者具有相同的框架,则为 myView1 阻止它
"receiver" 是 objective-c 时代的倒退,当时 "messages" 被发送到 "objects"。所以我们老人家(哈)习惯叫[someView addSubview:anotherView]
而不叫someView.addSubview(anotherView)
。在这种情况下,"someView" 是接收方,因为它正在接收 "addSubview" 消息。
视图存储在层次结构中,基本上是一棵树,其中每个视图可以有很多子视图(子视图),但只有一个父视图(父视图)。在我的示例中,"someView" 是超级视图(父视图),其中 "anotherView" 将作为子视图(子视图)添加。
在您的示例中,您正在实施的 class 既有接收器又有超级视图。它是接收者,因为您的 addSubview 调用是隐含的 "self.addSubview",这意味着 "self" 是接收者。这是超级视图,因为 currencyLbl 作为子视图添加到它。
针对您的具体情况明确说明:
- 接收者=自己
- Superview = self
- 视图 = currencyLbl
(注意 - Receiver 和 Superview 不是同义词,在这种情况下它们恰好指的是同一个对象。它们是完全不相关的概念。)