label and textview not responding to setText:
I have two properties setup properly in Objective-C:
@property (nonatomic, retain) IBOutlet UILabel *label;
@property (nonatomic, retain) IBOutlet UITextView *textView;
I have synthesised them and linked them in the NIB file however when I come t开发者_StackOverflowo use them like:
if (row==0) {
NSLog(@"First text");
[self.label setText:@"LABEL 1"];
[self.textView setText:@"LABEL 1"];
}
else if (row==1) {
NSLog(@"Second text");
[self.label setText:@"LABEL 2"];
[self.textView setText:@"LABEL 2"];
}
The text is not changing however the NSLog is being called and not the setText: and I was wondering why...
It should be a IBOutlet for you to link them in xib.
Now that you updated your question, the problem may be you have not connected it to your viewcontroller's property. Hence the result. Add more details to your question.
- Make sure that Xib and IBOutlet were correct linked.
- Make sure that your label and testView are visible. You can set their background color red or other any color different with bg to check their visibility.
- Make sure that the line "[self.label setText:@"LABEL 1"];" had ran. you can check the NSLog.
- Make sure that the line "[self.label setText:@"LABEL 1"];" was running in main thread.
If your properties are outlets you should declare them as follows:
@property (nonatomic, retain) IBOutlet UILabel *label;
@property (nonatomic, retain) IBOutlet UITextView *textView;
The behaviour you are seeing suggests that you haven't set up the outlet correctly.
FIRST METHOD:
It should be declared as an IBOutlet and you should link it to a label and textView in XIB as Praveen S has said.
SECOND METHOD:
Other option if it is not a label in XIB then you need to allocate and initialize the label to set its text i.e. to access its properties/methods/attributes.
UILabel *label = [[UILabel alloc] init];
[label setText:@"Text"];
label.frame = CGRectMake(0,0,20,20);
Hope this helps.
精彩评论