[英]Objective-c: AppDelegate NSString
我有一個 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)];
}
我需要能夠在視圖 controller 中使用此令牌變量。目前這就是我所擁有的:
distributedLCAViewController.m 中的部分:
- (IBAction)switchWasActivated:(id)sender
{
NSString *token2 = [[[distributedLCAAppDelegate alloc] token] autorelease];
}
但是,token2 =“無效的 cfstringref”。
我最初嘗試聲明一個名為 getToken 的公共方法,它只返回令牌。 但在那種情況下我也遇到了同樣的問題。
任何幫助,將不勝感激!
試試這個:(更新)(固定)
NSString* token2 = ((distributedLCAAppDelegate*)[[UIApplication sharedApplication] delegate]).token;
嘗試以下 -
- (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_);
試試這個,它應該工作
如果其委托出口設置為您的 distributedLCAAppDelegate,您可以檢查“MainWindow.xib”中的文件所有者嗎? 只需按住 Ctrl 鍵並單擊 Interface Builder 中的黃色框圖標。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.