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
Related
I am using cbpowell/MarqueeLabel class in my project. I changed the class of a Label from UILabel to MarqeeLabel. This got my Label to scroll. But it is in its Default MLLeftRight. I need to change it to MLContinous. How can I make this happen? I tried add a User Defined RunTime attribute, but that doesn't work. I cannot add "marqueeType" in the program, because my label is still in the UILabel class instead of MarqueeLabel, so can only access the methods for UILabel.
Anyone with experience in MarqueeLabel, please advice.
Or if you can tell me, how I can access the methods of a custom class I set(here- MarqueeLabel) for my label (instead of- UILabel)
You can create an IBOutlet for MarqueeLabel like
#property (weak, nonatomic) IBOutlet MarqueeLabel *mLabel;
Since it deoesn't support IBInspectable for marqueeType, you can configure label like
self.mLabel.marqueeType = MLLeftRight;
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.
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?
I am following a tutorial HERE on creating a table-view controller in Xcode. I am using the latest version of Xcode (Xcode 5). At the top of the tutorial it says its for iOS 5.
In the tutorial a cell is created and an UIImage and a few labels are dragged onto the cell. It goes on to say to Ctrl-Click the white space of the cell and then drag to the labels and image to establish the outlet connection. See screen grab from tutorial.
When I do this, I dont get the same options as per the tutorial. The only options I get are shown below in this screen grab (note I chose to only add one label and leave out image for the purpose of my application).
Can anyone please let me know what Im doing wrong or how else I can create this connection? I have Googled it for the last hour and all I could see was how to create IBOutlets.
Alternatively, does anyone know of any tutorials similar to this one? This one suits my needs perfect as I will be creating a view similar to the second part of this tutorial. Like I mentioned Im working with Xcode 5.
Although this might be a little late, I recently just stumbled upon this problem myself and figure out the answer. I used the same tutorial as you, and could not seem to generate the proper options when I did the control + drag.
Instead of dragging the labels to the whitespace of the custom cell, you will be dragging from the Connections tab of the custom cell. To do this:
First, make sure your labels are properly declared in CarTableViewCell.h and synthesize them in CarTableViewCell.m.
Second, change the custom class in the storyboard for the prototype cell from UITableViewCell to CarTableViewCell.
Next, click on the cell prototype in storyboard. Using Utilities, go to the connections tab (the last one with the arrow). If everything is linked properly in your header files, you should see "makeLabel", "modelLabel", and "carImage" under the Outlets section.
Finally, click the little circle next makeLabel and drag it over to the proper label in your custom cell. Do this for modelLabel as well as carImage, just using the associated outlet and label or imageView for each.
This solved my connection problem, and after following the rest of the tutorial, everything worked!
Is your cell a custom cell. If not click on your cell and go to the attribute inspector then under table view cell choose the style as custom
Is your TableViewCell connected to the "CarTableViewCell"?
Click on the cell and make sure the custom class is set to CarTableViewCell.
And your .h file matches the one in the tutorial
#import <UIKit/UIKit.h>
#interface CarTableViewCell : UITableViewCell
#property (nonatomic, strong) IBOutlet UIImageView *carImage;
#property (nonatomic, strong) IBOutlet UILabel *makeLabel;
#property (nonatomic, strong) IBOutlet UILabel *modelLabel;
#end
Try again, and it should be like the tutorial.
-Cong
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.