1 Replies - 887 Views - Last Post: 12 March 2016 - 12:20 AM

#1 drayarms  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 199
  • Joined: 18-May 11

How to access UI object from a calss member function

Posted 13 February 2016 - 05:28 PM

I created a text view programatically and I have an external function outside viewdidload within which I want to change the contents of that textview. How do I achieve that? Here's the code I'm working with
As things stand. I'm currently getting this error "Value of type 'UIView' has no member 'textView' " on the line
self.view.textView.text = "Yo Dawg!!"

class rootViewController: UIViewController {
        override func viewDidLoad() {
             super.viewDidLoad()

             let myTextView = UITextView(frame: CGRectMake(0,0,100,50))
             myTextView.backgroundColor = UIColor.clearColor()
             myTextView.textColor = UIColor.whiteColor()
             myTextView.font = UIFont (name: "Helvetica Neue", size: 20)
             myTextView.textAlignment = NSTextAlignment.Center
             myTextView.text = "Hello World"

             self.view.addSubview(timerView)

        }//End view did load



        func changeText(){//This function updates the text within the textview
            self.view.textView.text = "Yo Dawg!!"
       }//End change text function

}//End rootViewController



Is This A Good Question/Topic? 0
  • +

Replies To: How to access UI object from a calss member function

#2 Alcep  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 4
  • Joined: 09-March 16

Re: How to access UI object from a calss member function

Posted 12 March 2016 - 12:20 AM

This is a really old post :( This is a fairly easy fix, you just need a better understanding of how subviews work. I'm going to assume instead of `timerView` you mean `myTextView`.

First, it's far more efficient to maintain a reference to the subview as a property of the current class. This serves two purposes 1. Allows you to easy modify the subview internally and externally 2. Maintains a strong reference to prevent deallocation (always remember ARC). So your class might look something like this:

class rootViewController: UIViewController {
        var textView:UITextView?;

        override func viewDidLoad() {
             super.viewDidLoad()

             let myTextView = UITextView(frame: CGRectMake(0,0,100,50))
             myTextView.backgroundColor = UIColor.clearColor()
             myTextView.textColor = UIColor.whiteColor()
             myTextView.font = UIFont (name: "Helvetica Neue", size: 20)
             myTextView.textAlignment = NSTextAlignment.Center
             myTextView.text = "Hello World"

             self.view.addSubview(myTextView);
             self.textView = myTextView;
        }


        func changeText(){//This function updates the text within the textview
            if let myTextView = self.textView {
                 myTetView.text = "Sup Diggity";
           }
       }
}



But lets say you don't like things to be easy and and enjoy unnecessary complexity. You don't want a reference, you only want to add it as a subview. Sure, but you have to remember a few things. It doesn't magically become a property of the view so `self.view.textView` isn't valid, hence the error "Value of type 'UIView' has no member 'textView'".

So how do you access this subview after it's been added you ask? You need to tag it first http://stackoverflow...bview-of-uiview
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1