Cannot set UILabel text - ios

I want to have UILabel, but for some reason when I do theLabel.text it does not work. But my first label does.
Is there a issue in the code?
#interface ViewController : UIViewController
{
IBOutlet UILabel *Screen;
IBOutlet UILabel *Operations;
}
Then I would use it like the following:
Screen.text = [NSString stringWithFormat:#"%i", SelectNumber];
Operations.text = [NSString stringWithFormat:#"(The operation selected)"];

Make sure you have connected the two labels.
You can see that by looking at the little dots. In this case they are left to
line number 13 and 15.
If they are "filled" they have a connection.
If they are not filled like the picture below, it means there is no connection. In most cases you would have renamed the property. (Shame on apple, for not retaining the connection)
(In the picture you should notice that I have renamed the property)
One thing that can confuse people, is that even though there are no valid connection,
interface builder still says there is.
You should just click the little x button and start over.
You can do that by dragging the little dot from the property onto the interface builder object.

Related

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.

UIImageView.image is always nil after assigning a valid UIImage

I have a UIImageView in a view of a StoryBoard, IBOutlet connections are made properly (have double and triple checked this), the images to display are added to the proper target, etc. (Have been debugging this for a day now) the UIImageView display images if I set them in Interface Builder.
But if I set the UIImageView.image property to a valid UIImage (I can see even the preview when debugging) loaded with any image it always shows the first image, not the new one, or if I left it blank in IB it keeps nil value.
This only happens in this view if I try the same thing in another view of the StoryBoard and the UIImageView properly shows the content of the UIImage.
Any clues will we greatly appreciated.
Edited:
Thanks for the replies, has tested also setting it from a button, the code is this code:
The property declaration is:
#property (strong, nonatomic) IBOutlet UIImageView *theRecomendationImage;
Have Tested with weak and strong, just to be sure.
The IBAction implementation (tested it gets called)
- (IBAction)changeImage {
UIImage *theImageToTint = [UIImage imageNamed:#"Sunny"];
// theImageToTint = [theImageToTint imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate];
self.theRecomendationImage.tintColor = [UIColor yellowColor];
self.theRecomendationImage.image = theImageToTint;
}
When debugging if you check the value of theImageToTint it is a valid image (not nil) and you can even quicklook the image. But when I assign it to the UIImageView the value is always nil. No error, just keep nil.
Thanks for your interest, and sorry for the incomplete question.
Edited:
If you execute
po self.theRecomendationImage.image
in the debug console before and after executing the assignment: self.theRecomendationImage.image = theImageToTint; the value is nil.
Edited:
If you add the UIImageView by code, it works and get updated when you assign a new image to it, this takes me to point to some Storyboard issue.
Is there any tool to verify a storyboard?
Just make sure the image format is .png instead of .jpg or any other.

Using [NSString stringWithFormat] in UITAbleViewCell displaying as empty string

I must be missing something basic here. When I use [NSString stringWithFormat] to set a property of an object that displays in a UITableViewCell, it displays as empty. If I just set the property normally, i.e. property = #"Item One", it displays fine. The code, and result below.
Using this:
Results in:
Yet the log shows:
Which would indicate the property is set. Will someone please take me to school here.
EDIT
Underretaining was the issue.
My prop looked as such: #property (weak, nonatomic) NSString *name
Changing that to: #property (strong, nonatomic) NSString *name
Fixed the issue. I understand that this means I retain a strong reference to this property, but in this particular, is there a "I just graduated college and taught himself Obj-C" explanation as to what was happening in memory to cause this issue?
My first thought, since a constant string works, is that you're under-retaining the name property, although if that's true I'm surprised you aren't crashing.
Edit:
After seeing your code, here's the problem. A weak property only holds a value as long as the value is kept alive elsewhere. A constant string lives forever, so assigning a constant string here worked. But if you assign a calculated value, then nothing owns it, and therefore nothing keeps it alive, and therefore your weak property will end up with nil.
I'm assuming you're assigning name to the text property of a UILabel.
Either the label is:
nil
Hidden (check it's hidden property)
not in the view hierarchy (check it's superview property)
Not big enough to show the text (calling sizeToFit will fix this)
The same color as your background (not likely)
The hardest problem to solve will be if it is nil. You haven't told us where the cell is coming from but here are a few possibilities.
The IBOutlet isn't set in the xib/storyboard
You aren't instantiating it in the correct init method. If you are instantiating it, make sure that specific init variation is being called.

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.

How to programmatically set UISlider initial value, or, how does an iOS program start up?

I have been developing in xCode for exactly 3 days now. The last time I did UI development it was in Windows 3.1 with the Petzold book. Regardless of that I have my first iOS app up and running and it basically does what I need. I have two sliders to select hue and saturation, and in response to them I dynamically draw a bunch of gradient shaded circles. I got that much running between the Hello World example and stackoverflow, including caching the gradient in CGLayer (so thanks to all the stackoverflow people). There is one little piece that I can't quite get right though:
I want to set the initial value of one slider to 1.0 instead of the default 0.5. I know I can do that in IB, but I prefer to code it and I think I'd like to move away from IB altogether. I don't really understand how it makes connections between things and I'd like to understand the code. I have code like this to try to set it:
- (void)viewDidLoad
{
NSLog(#"viewDidLoad");
[super viewDidLoad];
[hue_slider setValue:0.5];
[sat_slider setValue:1.0];
self.led_view.hue_slider_value=0.5;
self.led_view.sat_slider_value=1.0;
// Do any additional setup after loading the view, typically from a nib.
}
sat_slider still ends up in the middle instead of at the end (1.0 is the max value). From stackexchange reading I understand that I am probably calling this at the wrong time, that the slider hasn't really been loaded when viewDidLoad is called, and my initial value is overwritten by the one specified in IB. What I haven't seen though is where the call should be made. So the question:
Where in my program should I put
[sat_slider setValue:1.0];
so that it sets the initial value of the slider, overwriting the default in IB? Can someone explain the order of how things start up in an iOS program? And a pointer to online or printed books regarding iOS and Objective C programming would be great.
Edit
When I check the value of sat_slider it is nil. So that means a connection is missing? I dragged it in the storyboard and created an IBOutlet in addition to an action.
#interface led_testViewController : UIViewController
- (IBAction)saturation_scroll:(id)sender;
- (IBAction)hue_scroll:(id)sender;
#property (retain, nonatomic) IBOutlet UISlider *hue_slider;
#property (retain, nonatomic) IBOutlet UISlider *sat_slider;
#property (strong, nonatomic) IBOutlet led_view *led_view;
#end
You may put the code in - (void)viewWillAppear:(BOOL)animated;
I followed the suggestions of ABC and NJones to check sat_slider and it was nil. There were properties for both sat_slider and hue_slider in the ViewController.h file, but something was still missing. I deleted the properties, re-dragged them in IB, and then I was able to set the slider position in viewDidLoad with no problems.
Check if sat_slider nil. Also make sure that it is properly connected in IB. If not, remove it and add it again in nib/storyboard. That should fix the issue.

Resources