Reputation: 14428
i am new to ios. I have a viewController, where in there is a notification observer. For example
-(void) myNotificationObFn:(Notification *)noti
{
/* Here i am trying to change the text of UILabel, which is not working.
ie:
NSString *ns = [NSString stringWithFormat:@"%d", 10];
mylabel.text = ns;
*/
}
How can i resolve this issue ?
Upvotes: 0
Views: 1552
Reputation: 31
-(void) myNotificationObFn:(Notification *)noti
{
dispatch_sync(dispatch_get_main_queue(), ^{
NSString *ns = [NSString stringWithFormat:@"%d", 10];
labelfromtag.text = ns;
[labelfromtag setNeedDisplay];
});
}
Upvotes: 3
Reputation: 442
try using the tag function if you want to change the label
examples :
if you create the label programmatically, set the tag of your label after you create it for the first time
UILabel *myLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 100, 50)];
[myLabel setTag:1234];
if you create the label in your XIB or storyboard, just change the "tag" field
this way, your label get a tag "1234"
after this, if you need to edit this label , on your notification method put this code
UILabel *labelfromtag = (UILabel*)[self.view viewWithTag:1234]; //this code direct label "labelfromtag" to your "myLabel", so your new label here is actually your old label
NSString *ns = [[NSString alloc] initWithFormat:@"%d", 10];
labelfromtag.text = ns;
good luck :]
edit : also, is this notification reside in the same class / controller as ur UILabel?
Upvotes: 1
Reputation: 16134
1 - Is your label properly wired up in IB? Can you change the text somewhere else in your code (say viewDidLoad for example)?
2 - You don't need to alloc the string since you are not retaining it outside the scope of this method. You can just do:
[myLabel setText:[NSString stringWithFormat:@"%d",10]];
Upvotes: 0