1

我有一个 NSString 作为我的 appdelegate 中的实例变量,如下所示:

分布式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

来自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)];
}

我需要能够在视图控制器中使用这个令牌变量。目前这就是我所拥有的:

DistributedLCAViewController.m 中的部分:

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

但是,token2 =“无效的 cfstringref”。

我最初尝试声明一个名为 getToken 的公共方法,它只返回令牌。但在那种情况下我也遇到了同样的问题。

任何帮助,将不胜感激!

4

4 回答 4

2

试试这个:(更新)(固定)

NSString* token2 = ((distributedLCAAppDelegate*)[[UIApplication sharedApplication] delegate]).token;
于 2012-04-10T05:05:56.103 回答
2

尝试以下 -

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

}

于 2012-04-10T05:06:58.520 回答
0
distributedLCAAppDelegate* delegateobj = [(distributedLCAAppDelegate*)[UIApplication sharedApplication] delegate];
NSString *token_ = delegateobj.token;
NSLog(@"token_ :%@",token_);

试试这个,它应该工作

于 2012-04-10T05:16:45.817 回答
0

如果其委托出口设置为您的分布式LCAAppDelegate,您能否检查“MainWindow.xib”中的文件所有者?只需按住 Ctrl 键单击 Interface Builder 中的黄色框图标。

于 2012-04-10T06:12:47.707 回答