objective c - NSTimer in AppDelegate does not update UILabel in UIViewController -


my goal start timer in app delegate , use call methods in other view controllers. when these methods called update text of uilabel. i'm performing method on main thread can't figure out why uilabel not being updated. know method in view controller being called uilabel doesn't updated. verified iboutlet connection in interface builder. using storyboard lay out views , attach view controllers views. doing wrong?

in appdelegate.m:

#import "appdelegate.h" #import "greyviewcontroller.h"  @interface appdelegate ()  @property (nonatomic) int counter; @property (strong, nonatomic) greyviewcontroller *greyclass;  @end  @implementation appdelegate  - (bool)application:(uiapplication *)application didfinishlaunchingwithoptions:(nsdictionary *)launchoptions {      nstimer *timer;     timer = [nstimer scheduledtimerwithtimeinterval:2.0                                              target:self                                            selector:@selector(performonmain)                                            userinfo:nil                                             repeats:yes];      return yes; }  - (greyviewcontroller *)greyclass {     if (!_greyclass) {         _greyclass = [[greyviewcontroller alloc] init];     }      return _greyclass; }  - (void)performonmain {     [self performselectoronmainthread:@selector(updatelabel) withobject:nil waituntildone:yes]; }  - (void)updatelabel {      self.counter++;     nslog(@"appdelegate counter %i", self.counter);      [self.greyclass updategreylabel:[nsstring stringwithformat:@"%i", self.counter]]; }  @end 

the greyviewcontroller.h is:

#import <uikit/uikit.h>  @interface greyviewcontroller : uiviewcontroller  @property (strong, nonatomic) nsstring *greystring; @property (weak, nonatomic) iboutlet uilabel *greylabel;  - (void)updategreylabel:(nsstring *)string;  @end 

the method in greyviewcontroller.m:

- (void)updategreylabel:(nsstring *)string {      self.greylabel.text = string;     nslog(@"greyviewcontroller string %@", string); } 

every time call:

greyviewcontroller *greyclass = [[greyviewcontroller alloc] init]; 

in "updatelabel" method (i.e. every 2 seconds) you're instantiating new greyviewcontroller. it's not want that.

instead, instantiate/create once (or create in storyboard or xib file) , update label connected outlet.


Comments

Popular posts from this blog

c# - DetailsView in ASP.Net - How to add another column on the side/add a control in each row? -

javascript - firefox memory leak -

Trying to import CSV file to a SQL Server database using asp.net and c# - can't find what I'm missing -