简体   繁体   English

Objective-c:AppDelegate NSString

[英]Objective-c: AppDelegate NSString

I have an NSString that as an instance variable within my appdelegate as below:我有一个 NSString 作为我的 appdelegate 中的实例变量,如下所示:

distributedLCAAppDelegate.h:分布式LCAAppDelegate.h:

@class distributedLCAViewController;

@interface distributedLCAAppDelegate : NSObject <UIApplicationDelegate> {
UIWindow *window;
distributedLCAViewController *viewController;
NSString *token;
}

@property (nonatomic, retain) IBOutlet UIWindow *window;
@property (nonatomic, retain) IBOutlet distributedLCAViewController *viewController;
@property (nonatomic, copy) NSString *token;

@end

section from distributedLCAAppDelegate.m:来自 distributedLCAAppDelegate.m 的部分:

@implementation distributedLCAAppDelegate

@synthesize window;
@synthesize viewController;
@synthesize token;

- (void)application:(UIApplication *)app didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken 
{ 
token = [NSString stringWithFormat:@"%@",deviceToken];
token = [token stringByReplacingOccurrencesOfString:@" " withString:@""];
token = [token substringWithRange:NSMakeRange(1, [token length]-2)];
}

I need to be able to use this token variable within the view controller. Currently this is what I have:我需要能够在视图 controller 中使用此令牌变量。目前这就是我所拥有的:

section from within distributedLCAViewController.m: distributedLCAViewController.m 中的部分:

- (IBAction)switchWasActivated:(id)sender
{
    NSString *token2 = [[[distributedLCAAppDelegate alloc] token] autorelease];
}

However, token2 = "invalid cfstringref".但是,token2 =“无效的 cfstringref”。

I initially tried declaring a public method called getToken, which just returned token.我最初尝试声明一个名为 getToken 的公共方法,它只返回令牌。 But I was getting the same problem in that case as well.但在那种情况下我也遇到了同样的问题。

Any help would be appreciated!任何帮助,将不胜感激!

Try this: (UPDATED) (fixed)试试这个:(更新)(固定)

NSString* token2 = ((distributedLCAAppDelegate*)[[UIApplication sharedApplication] delegate]).token;

Try following -尝试以下 -

- (IBAction)switchWasActivated:(id)sender {
    distributedLCAAppDelegate *delegate = (distributedLCAAppDelegate *) [[UIApplication sharedApplication] delegate];
    NSString *token2 = delegate.token;

} }

distributedLCAAppDelegate* delegateobj = [(distributedLCAAppDelegate*)[UIApplication sharedApplication] delegate];
NSString *token_ = delegateobj.token;
NSLog(@"token_ :%@",token_);

Try This, it should work试试这个,它应该工作

Can you check the Files' Owner in "MainWindow.xib" if its delegate outlet is set to your distributedLCAAppDelegate?如果其委托出口设置为您的 distributedLCAAppDelegate,您可以检查“MainWindow.xib”中的文件所有者吗? Just Ctrl-click on the yellow box-icon in Interface Builder.只需按住 Ctrl 键并单击 Interface Builder 中的黄色框图标。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM