1
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's explanation in Xcode is "Adds a view to the end of the receiver’s list of subviews" and it says "This method establishes a strong reference to view and sets its next responder to the receiver, which is its new superview "

Could you explain "receiver" and "view - superview" in that context.

2 Answers2

3

"receiver" is a throwback to the days of objective-c when "messages" were sent to "objects". So us old people (hah) used to call [someView addSubview:anotherView] instead of someView.addSubview(anotherView). In that scenario, "someView" is the receiver because it's RECEIVING the "addSubview" message.

Views are stored in hierarchies, a tree basically, where each view can have many children (subviews), but only one parent (superview). In my example, "someView" is the superView (parent) in which "anotherView" will be added as a subview (child).

In your sample, the class you are implementing there is both the receiver AND the superview. It's the receiver because your addSubview call is implicitly "self.addSubview" which means "self" is the receiver. It's the super view because currencyLbl is being added to it as a subview.

Spelled out explicitly for your exact case:

  • Receiver = self
  • Superview = self
  • View = currencyLbl

(Note - Receiver and Superview are not synonyms, they just happen to refer to the same object in this case. They are completely unrelated concepts.)

ghostatron
  • 2,620
  • 23
  • 27
  • One more question. I want to familiar with swift documentation. in your example, did you mean addSubview is responder? – zeynelabidin sevgili Oct 05 '18 at 21:20
  • Definitely not. A responder is something else entirely. It generally refers to which view in the hierarchy will "respond" to a given gesture. If you call addSubview, then it's likely that the view you just added will be the first responder. – ghostatron Oct 08 '18 at 20:55
1

In this example

let myView = UIView()
parentView.addSubview(myView) // here myView is a subview

parentView is called either receiver or superView of myView

This

This method establishes a strong reference to view and sets its next responder to the receiver, which is its new superview.

means if you added a subview to a prentView in say a local function that the subview will be supposed to deallocate end of function execution , then addSubview will hold a strong reference to that subview and prevents the deallocation process

and this

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

means that if you added myView to parentView1 , then it will be it's one and only superView , if you tried to add it to parentView2 , then it will be automatically removed from parentView1 and added to parentView2

and this

Adds a view to the end of the receiver’s list of subviews

means it's a stack of subviews that when you add myView1 to parentView , then add myView2 , will cause myView2 to be above myView1s so it will be active for any event and block it for myView1 if say both have the same frame

Shehata Gamal
  • 98,760
  • 8
  • 65
  • 87