简体   繁体   English

如何在Object-C中将字符串发送到AppDelegate?

[英]How do I send the a String to AppDelegate in Objective-C?

I've create a class 'Datetool' inherit from NSObject to record the date when the application was launched. 我创建了一个从NSObject继承的类“ Datetool”,以记录启动应用程序的日期。 But I need to show the startTime string in the AppDelegate. 但是我需要在AppDelegate中显示startTime字符串。

I tried to use the Block to send the string with a variable declared in 'AppDelegate.h', however, I couldn't get it in Datetool's method initialize . 我尝试使用Block发送带有在'AppDelegate.h'中声明的变量的字符串,但是,我无法在Datetool的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]];
    }
}

Why not simply call a function in the AppDelegate when the app starts.(warning I have not tried compiling this). 为什么不在应用启动时简单地在AppDelegate中调用一个函数。(警告我还没有尝试编译它)。

- (NSString*)getStartDate {

    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateStyle:NSDateFormatterNoStyle];
    [formatter setTimeStyle:NSDateFormatterMediumStyle];
    return [formatter stringFromDate:[NSDate date]];
}

You can get at your AppDelegate from anywhere by using the UIApplication's singleton method: [UIApplication sharedApplication].delegate Just cast it as your custom class to access specific methods: (AppDelegate*)[UIApplication sharedApplication].delegate 您可以使用UIApplication的singleton方法从任何地方访问AppDelegate: [UIApplication sharedApplication].delegate只需将其转换为自定义类即可访问特定方法: (AppDelegate*)[UIApplication sharedApplication].delegate

That said, I don't think that's the right approach to take here. 也就是说,我认为这不是正确的方法。 Your Datetool is a utility class and shouldn't be reliant on references to the AppDelegate, which makes for very tightly coupled code. 您的Datetool是一个实用程序类,不应依赖对AppDelegate的引用,这会使代码耦合得非常紧密。 Instead, Datetool should be able to be called from the AppDelegate and return the desired value. 相反,应该能够从AppDelegate调用Datetool并返回所需的值。 Since you're already using a class method, it doesn't seem like Datetool needs an instance at all. 由于您已经在使用类方法,因此Datetool似乎根本不需要实例。 You can scrap storing the static value and just return what you need: 您可以取消存储静态值,然后只返回所需的内容:

+ (NSString*)startTime
{ 
   NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
   [formatter setDateStyle:NSDateFormatterNoStyle];
   [formatter setTimeStyle:NSDateFormatterMediumStyle];
   return [formatter stringFromDate:[NSDate date]];
}

Now you're pulling a value from the tool rather than trying to push it onto the AppDelegate, which makes it reusable for other purposes. 现在,您要从工具中提取一个值,而不是尝试将其推到AppDelegate上,这使其可以重用于其他目的。

将类定义导入AppDelegate.h并在AppDelegate中实例化对象会是个坏主意吗?

I suggest that you provide a delegate protocol in your DateTool which is implemented by your AppDelegate. 我建议您在您的DateTool中提供一个委托协议,该协议由您的AppDelegate实现。 For example (not tested): 例如(未测试):

DateTool.h: DateTool.h:

/** The delegate protocol of DateTool. */

@protocol DateToolDelegate <NSObject>

/** The start date was set. */
- (void)startDateSet:(NSDate)startDate;

@end

@interface DateTool : NSObject

/** The delegate of DateTool. */
@property (weak) id<DateToolDelegate> delegate;

@end

DateTool.m: 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 you need to add the protocol: 在AppDelegate.h中,您需要添加协议:

#import "DateTool.h"
@interface AppDelegate : UIResponder <DateToolDelegate>

In your AppDelegate.m you need to register itself as delegate: 在您的AppDelegate.m中,您需要将自己注册为委托:

...    
dateTool.delegate = self;
...

and implement the protocol: 并实现协议:

- (void)startDateSet:(NSDate)startDate
{
// Do something with startDate
}

Hope that helps. 希望能有所帮助。

PS: Why is initialize a class method? PS:为什么要初始化类方法? I'd suggest to instantiate an object. 我建议实例化一个对象。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将AppDelegate Swift代码放入Objective-C AppDelegate文件并使其工作? - How do I put AppDelegate Swift code into Objective-C AppDelegate file and make it work? 如何将AppDelegate与Swift 3和Objective-c混合使用 - How to mix AppDelegate with Swift 3 and Objective-c 如何测试 Objective-C 中的字符串是否为空? - How do I test if a string is empty in Objective-C? 如何在Objective-C中在运行时查找字符串常量? - How do I lookup a string constant at runtime in Objective-C? 如何在Objective-C中将字符串插入sqlite? - How do I insert string to sqlite in Objective-C? Objective-c:AppDelegate NSString - Objective-c: AppDelegate NSString 如何将appDelegate.m(Objective-C)中的内容导入到Swift项目中? - How do you import something in appDelegate.m(Objective-C) into a Swift project? Objective-C:如何从通用应用程序中的AppDelegate移出? - objective-c : How to move from AppDelegate in Universal app? 如何理解Objective-C AppDelegate 方法application:didFINishLauchingWithOptions? - How to understand Objective-C AppDelegate method application:didFInishLauchingWithOptions? iOS / Objective-C - 如何在不丢失初始会话的情况下发送多个NSURLSessionDataTask请求? - iOS/Objective-C - How do I send multiple NSURLSessionDataTask requests without losing initial session?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM