0

I know it may be the basic question but I am new to Swift. Also, I have tried various solutions on SO but could not resolve the issue. So if anyone can help me with my problem.

I have a custom UIVIEW class as follows:

class SearchTextFieldView: UIView, UITextFieldDelegate{
    public var searchText = UITextField()


        override init(frame: CGRect) {
            super.init(frame: frame)
            initializeUI()
        }


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



        func initializeUI() {
            searchText.placeholder = "Enter model no"
            searchText.backgroundColor = UIColor.white
            searchText.textColor = UIColor.darkGray
            searchText.layer.cornerRadius = 5.0
            searchText.delegate=self
            self.addSubview(searchText)

        }

        override func layoutSubviews() {
            searchText.frame = CGRect(x: 20.0, y: 5.0, width: self.frame.size.width - 40,height : self.frame.size.height - 10)
        }
}

Now I want to set text to SearchText textfield from another class which is as follows:

override func viewDidLoad() {
        super.viewDidLoad()
        setupUI()
        // Do any additional setup after loading the view.
    }

    func setupUI() {

        let searchTextFieldView = SearchTextFieldView()
        self.view.addSubview(searchTextFieldView)    //adding view containing search box view at the top
        **searchTextFieldView.searchText.text = "My Text"**
    }

I am using Storyboard. Also, I can see the textfield with placeholder text.only problem is I can not set text to it. Can anybody help. Whats wrong in my code.

1
  • Make sure that "initializeUI" method called on your SearchTextFieldView. Because you just do normal init(SearchTextFieldView()) instead of "init(frame: CGRect)". Commented Nov 19, 2018 at 7:45

4 Answers 4

2

It is needed to call searchTextFieldView.setNeedsDisplay(), this will in turn call override func draw(_ rect: CGRect) in class SearchTextFieldView.

Add override func draw(_ rect: CGRect) {} in SearchTextFieldView, and try setting searchText.text = <someValue> in draw(). You can use a String property in SearchTextFieldView, to get <someValue> from the client (one who is using SearchTextFieldView) class.

Sign up to request clarification or add additional context in comments.

Comments

1

You are creating you view via SearchTextFieldView(), while you have 2 available initializers init(frame:) and init?(coder:).

If you change

let searchTextFieldView = SearchTextFieldView()

with

let searchTextFieldView = SearchTextFieldView(frame: CGRect(x: 50, y: 50, width: 100, height: 100))

you will see the text.

2 Comments

This did not work. I forgot to mention in my question that I am using storyboard also I can see the textfield with placeholder text in it. Only problem is can not set text to it. Anything else you can suggest. Thanks.
So, are you trying to change text in view that was created programmatically, or that was added in storyboard. If it is the storyboard one, then you need to make an outlet and address it in your code.
1

You are not setting frame to the view. Also you are not loading the .xib in the view class. It should be like:-

class SearchTextFieldView: UIView, UITextFieldDelegate{

    //MARK:- Initializer
    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        initialize(withFrame: self.bounds)
    }

    override init(frame : CGRect) {
        super.init(frame: frame)
        initialize(withFrame: frame)
    }

    //MARK: -  View Initializers
    func initialize(withFrame frame : CGRect) {

        Bundle.main.loadNibNamed("SearchTextFieldView", owner: self, options: nil)
        view.frame = frame
        view.autoresizingMask = [.flexibleWidth, .flexibleHeight]
        self.addSubview(view)
        initializeUI()
    }
}

Now you can call the below code in view controller:-

    override func viewDidLoad() {
        super.viewDidLoad()
        setupUI()
       // Do any additional setup after loading the view.
    }

    func setupUI() {
        let searchTextFieldView = SearchTextFieldView(frame: ?*self.view.bounds)
        self.view.addSubview(searchTextFieldView)    
        //adding view containing search box view at the top
        searchTextFieldView.searchText.text = "My Text"
    }

Don't forget to create an xib with name "SearchTextFieldView.xib" as you are loading that nib in your initialize function.

Hope it helps :)

Comments

1

add frame for the searchTextFieldView inside setupUI() method. because the View got loaded on the view but its doesn't have a frame (x,y position, width and height). Change your UIViewController's colour to grey and u can see the your view loaded on the left corner (0,0). set frame size for the view that will solve this problem.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.