ios - How do I send the a String to AppDelegate in Objective-C? -
i've create class 'datetool' inherit nsobject record date when application launched. need show starttime string in appdelegate.
i tried use block send string variable declared in 'appdelegate.h', however, couldn't in datetool's method initialize
.
datetool.m
static nsstring *starttime2; +(void)initialize { if (self == [datetool self]) { nsdateformatter *formatter = [[nsdateformatter alloc] init]; [formatter setdatestyle:nsdateformatternostyle]; [formatter settimestyle:nsdateformattermediumstyle]; starttime2 = [formatter stringfromdate:[nsdate date]]; } }
i suggest provide delegate protocol in datetool implemented appdelegate. example (not tested):
datetool.h:
/** delegate protocol of datetool. */ @protocol datetooldelegate <nsobject> /** start date set. */ - (void)startdateset:(nsdate)startdate; @end @interface datetool : nsobject /** delegate of datetool. */ @property (weak) id<datetooldelegate> delegate; @end
datetool.m:
+(void)initialize { if (self == [datetool self]) { nsdateformatter *formatter = [[nsdateformatter alloc] init]; [formatter setdatestyle:nsdateformatternostyle]; [formatter settimestyle:nsdateformattermediumstyle]; starttime2 = [formatter stringfromdate:[nsdate date]]; if ([_delegate conformstoprotocol:@protocol(datetooldelegate)]) { [_delegate startdateset:starttime2]; } } }
in appdelegate.h need add protocol:
#import "datetool.h" @interface appdelegate : uiresponder <datetooldelegate>
in appdelegate.m need register delegate:
... datetool.delegate = self; ...
and implement protocol:
- (void)startdateset:(nsdate)startdate { // startdate }
hope helps.
ps: why initialize class method? i'd suggest instantiate object.
Comments
Post a Comment