UITextView setText is not updating the text - ios

In my nib, I have a UITextView component.
In my code I have a UITextView field and I have used Interface Builder to make sure that the two are connected (at least I think I did that by dragging from "Files Owner" and dropping it on the UITextView in Interface Builder and selecting my member variable).
Now, when I update the text via setText, the UITextView still remains blank.
Thanks
DeShawn

Have you used
#property(nonatomic,retain) UITextView *yourTextViewName;
then
#synthesize yourTextViewName;
in .m file?
If yes, use the following code in viewDidLoad after updating the value to check:
NSLog(#"Text View Value = %#",yourTextViewName.text);

If it isn't figured it out yet;
check out iOS Text View Not Updating
The referencing yourTextViewName must be an IBOutlet UITextView *yourTextViewname
Then add the #property (nonatomic, retain) UITextView *yourTextViewName.
Add the #synthesize yourTextViewName in the corresponding .m file.
To set the text, use
yourTextViewName.text = #"some text";

This may sound crazy, but I had to explicitly set the hidden property of UITextView to NO in code and then it started showing up. See if this works for you.

Related

Can't change view from WatchKit controller

I added a bunch of WKInterfaceLabels to the storyboard, added them as IBOutlets in InterfaceController.h, and used the SetText method to set some text on them. However, I get no change on the view. The console gives the message "Unknown property in Interface description" for each label. How do I fix this?
In InterfaceController.h, I define the label as follows:
IBOutlet WKInterfaceLabel *hdate;
In InterfaceController.m, I set it's text as follows:
- (void)willActivate {
// This method is called when watch view controller is about to be visible to user
[super willActivate];
[hdate setText:#"blah"];}
Since the correct answer is in the comments of the question, I will just post it here again:
You can't create the IBOutlet in the .h file with just
IBOutlet WKInterfaceLabel *hdate
in brackets, I have to set them as properties.
(Credits to user3261697)
Change
IBOutlet
To
#IBOutlet
Or reconnect the label to the view.
In my case, it ran successfully.

Connect two labels to one outlet

Now I understand that this question has been asked before, but the answers were unsatisfactory. My issue is that I have a view controller with a view and stuff in it including a label. I added a bunch of code for it and now I'm expanding on it. I now have an issue where I've decided to add another UIView to my interface and it has a label and that label is going to function EXACTLY like a label I have in my first UIView. My problem is that I don't want to have to go in my view controller method and add another line of code each time I manipulate that first label. Is there anyway I can link another label to my initial IBOutlet I have set for my first label? Or do I have to go in my code and add an extra line of code everytime I manipulate that first label?
It depends on what you want to do to that label. If you're looking to change some of the attributes of the label in the same way (e.g., font, text colour, alignment) then you can put both labels in an IBOutletCollection and iterate over the collection in your view controller.
If you want to have different data in the label, but other attributes the same, then you'll need a separate IBOutlet for that label.
You can combine the two techniques as well. e.g.
(interface)
#property (weak, nonatomic) IBOutlet UILabel *firstName;
#property (weak, nonatomic) IBOutlet UILabel *lastName;
#property (strong, nonatomic) IBOutletCollection(UILabel) NSArray *labels;
(implementation)
- (void)viewDidLoad {
[super viewDidLoad];
for (UILabel *aLabel in self.labels) {
// Set all label in the outlet collection to have center aligned text.
[aLabel setTextAlignment = NSTextAlignmentCenter;
}
[self.firstName setText:#"First Name"];
[self.lastName setText:#"Last Name"];
}
Basically the simple answer is no. Whether you use outlets or an outlet collection or tags or whatever, you ultimately have one reference in your code to one label in your interface, and another reference in your code to another reference in your interface. You can compress your mode of expression so as to cycle readily through those references (as suggested in a different answer), but the basic fact is inescapable that, ultimately, the only way to "talk to" a label is through the one reference that points to that label and to that label alone.
The only way of getting around that is not to use direct references at all. For example, a single message can be sent to multiple recipients by using an NSNotification. So you could have two instances of some UILabel subclass of your own, and "shout" to both instances simultaneously by posting a notification from your view controller - the notification is then automatically passed on to both labels, because you have arranged beforehand for them to register for it.
Similarly, another alternative is that you could use key-value observing so that a change in your view controller is automatically propagated to both labels automatically because they "observe" the change, meaning they are sent notifications - really just an inverted form of NSNotification. (If this were Mac OS X, you could make a simpler, safer version of this arrangement by using "bindings".)
However, I really cannot actually recommend that approach. The truth is that we still live in an excruciatingly primitive world of text-based programming, one line at a time, one command at a time, one reference at a time, and we must just bite the bullet and get on with it.
Swift 3, Xcode 8
Create a prototype cell with objects
then add another prototype
It will copy the objects from the first prototype cell.
The new objects will be connected to the same IBOutlet
Also, copy and pasting objects maintains IBActions, but does not maintain IBOutlets.
I hope this answers your question, as none of the other answers had this work around.

Can't get UITextField variable

I'm trying to change the color of some placeholder text, of a UITextField, but i having problems reaching the text field. I've created a property with Referencing Outlets, like this:
#property (retain, nonatomic) IBOutlet UITextField *usernameField;
But can't reach it with either usernameField or _usernameField. What am i missing?
If you have a property, do NOT synthesize it. That just complicates things, and is no longer needed in Objective C 2.0.
Don't use _usernameField. That bypasses the property getter/setter and accesses the iVar directly.
Use self.usernameField instead. Until you understand the difference, use the property except in the code of a custom getter/setter or dealloc method.
first you should use self.usernameField and second just make sure you assigned the outlet to the UITextField you need to access it in the Interface Builder.
You can check it. Have you connected the outlet of the textfield in the xib file?

Change content in UITextView

Quite a simple one I assume but searching has failed me.
I have a UITextView I set up in a Storyboard with some dummy text. Dynamically I would like to change the content of this, but I don't know how. Searching for this seems to only returns results in which the UITextView has been created programmatically as opposed to via a drag and drop on the Storyboard, hence they have direct access to a variable representing it.
Add an outlet to UITextView then changed it dynamically!
Like this:
#property (weak, nonatomic) IBOutlet UITextView *yourText;
self.yourText.text = // ANY TEXT HERE

Can't make URL clickable in UITextView

I'm using Interface Builder to layout my app. I have a UITextView that contains some text, part of which is a URL that I'd like to make clickable (e.g. launches a browser). I've read up on how to do this and believe I'm doing it correctly, however while the URL appears blue/clickable, clicking it in the iPhone emulator doesn't work. Nothing happens.
My controller:
#interface FirstViewController : UIViewController <UISearchBarDelegate>
#property (nonatomic, retain) IBOutlet UITextView *review;
#end
In my implementation I #synthesize review; (and am able to manipulate the view's text, so don't think that's the issue).
In IB I have:
..then later when I go to set the text (and attempt to make URLs in the view clickable) I do:
self.review.text = content;
// My understanding is that this makes URLs clickable...
self.review.dataDetectorTypes = UIDataDetectorTypeLink;
...which displays something like:
...which really looks like it wants to work, however when clicking the URL nothing happens. What am I missing?
--UPDATE--
Tried adding self.review.editable = NO; in response to Dixit Patel's answer, but it didn't fix the issue:
self.review.text = content;
// My understanding is that this makes URLs clickable...
self.review.editable = NO;
self.review.dataDetectorTypes = UIDataDetectorTypeLink;
Check the "Selectable" and "Links" checkboxes in the textview Attributes Inspector:
The issue was that User Interaction Enabled wasn't checked at the bottom of the View section of IB's Attributes Inspector.

Resources