6

I am trying to create a custom UIView that I can use in my other UIViewControllers.

The custom view:

import UIKit

class customView: UIView {

    override init(frame: CGRect) {

        super.init(frame:frame)

        let myLabel = UILabel(frame: CGRect(x: 0, y: 0, width: 250, height: 100))
        addSubview(myLabel)
    }

    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
    }
}

Then I want to add it in to a separate UIViewController:

let newView = customView(frame:CGRectMake(0, 0, 500, 400))
self.view.addSubview(newView)

This works to display the view, but what do I need to add to be able to change the properties (such as myLabel) from the UIViewController that is embedding the customView?

I'd like to be able to access and change the label from the viewController, allowing me to change text, alpha, font, or hide the label using dot notation:

newView.myLabel.text = "changed label!"

Trying to access the label now gives the error "Value of type 'customView' has no member 'myLabel'"

Thanks so much for any help!

2 Answers 2

6

This is because the property myLabel is not declared at the class level. Move the property declaration to class level and mark it public. Then you will be able to access it from outside.

Something like

import UIKit

class customView: UIView {

    public myLabel: UILabel?    
    override init(frame: CGRect) {

        super.init(frame:frame)

        myLabel = UILabel(frame: CGRect(x: 0, y: 0, width: 250, height: 100))
        addSubview(myLabel!)
    }

    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
    }
}
4
  • In this particular case, I would advise you to declare a function in your customView, updateText which would take string as parameter and then set the label string internally. This func should also be public.
    – Shamas S
    Commented Dec 4, 2015 at 3:34
  • Trying to put a 'public' statement inside the class declaration like in your example, the word public is being considered by Xcode as a separate statement as it errors 'Consecutive declarations on a line must be separated by ';'
    – RanLearns
    Commented Dec 4, 2015 at 5:07
  • Got it! I had to declare the class a public, then declare any variable or function as public (using "public var" as opposed to just public), and also declare the init?(coder) function as public to get it to work.
    – RanLearns
    Commented Dec 4, 2015 at 5:28
  • @RanLearns good to know it works for you. Please upvote/accept answer if it helped.
    – Shamas S
    Commented Dec 4, 2015 at 6:47
0

Make it simple!

import UIKit
import WebKit

class CustomWebView: UIView {
    var webview = WKWebView()

    override init(frame: CGRect) {
        super.init(frame: frame)
        
        webview.frame = bounds
        addSubview(webview)
    }

    required init?(coder aDecoder: NSCoder) {
        super.init(frame: .zero)
    }
    
    func loadWebView(with urlString: String) {
        guard let request = URLRequest(url:URL(string: urlString)) else { return }
        webview.load(request)
    }
}

To access it,

let customView = CustomWebView(frame: view.bounds)
customView.loadWebView(with: "https://www.google.com/")

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.