2017-08-31 109 views
0

我正在創建一個iOS靜態庫,其中用戶將在其中傳遞Viewontroller和一些parameters的名稱,我在didReceiveRemoteNotification中獲取這些詳細信息,並從此處獲得了一個字符串假設NSString *vcName = @"ViewController2"和參數假設NSString *param1= @"UserName"NSString *param2= @"email"現在我想將這些參數傳遞給viewController從push接收哪個名稱的字符串。但我不想寫#import ViewController2如何在不創建對象的情況下將數據傳遞給viewController

我能夠重定向到ViewController2沒有進口,但不知道如何將這些參數從下面的代碼傳遞給ViewController2

我可以重定向到ViewController。

NSString *vcName = @"ViewController2"; 
NSString *param1= @"UserName"; 
NSString *param2= @"user_email"; 
UIStoryboard * storyboard = [[[UIApplication sharedApplication] keyWindow] rootViewController].storyboard; 
UIViewController *vcToOpen = [storyboard instantiateViewControllerWithIdentifier:vcName]]; 

vcToOpen.modalPresentationStyle =UIModalPresentationFullScreen; 

[[[[UIApplication sharedApplication]keyWindow] rootViewController] presentViewController:vcToOpen animated:YES completion:nil]; 

現在我想要得到這兩個參數的值在ViewController2。任何人都可以幫助我如何去做。無需編寫#import ViewController2,因爲應用程序可以有很多ViewControllersvcName可以是其中的任何一個。

+0

你可以使用全局字符串或在appdelegate方法中定義 –

+0

我認爲不可能,或者如果你想強制執行它,那麼你必須使用NSUserDefault中的存儲參數值或其他地方的低效方法來做到這一點,從NSUserDefault,或者在ViewDidLoad上的哪個位置,並將該值分配給viewcontroller的特定變量。 –

+0

你可以使用共享類 –

回答

0

AppDelegate.h

-(NSString *)getEmail; 
-(NSString *)getName; 
-(void)setEmail:(NSString *)email Name:(NSString *)name; 
+(AppDelegate *)sharedAppDelegate; 

AppDelegate.m

@interface AppDelegate() 
{ 
    NSString *strEmail, *strName; 
} 

-(NSString *)getEmail 
{ 
    return strEmail; 
} 

-(NSString *)getName 
{ 
    return strName; 
} 

-(void)setEmail:(NSString *)email Name:(NSString *)name 
{ 
    strEmail = email; 
    strName = name; 
} 

+(AppDelegate *)sharedAppDelegate 
{ 
    return (AppDelegate *)[[UIApplication sharedApplication] delegate]; 
} 

ViewController1.m

#import "AppDelegate.h" 

-(void)gotoViewController2 
{ 
    [[AppDelegate sharedAppDelegate] setEmail:@"[email protected]" Name:@"name1234"]; 
    [self performSegueWithIdentifier:@"segueToViewController2" sender:nil]; 
} 

ViewController2.m

#import "AppDelegate.h" 

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    NSString *name = [[AppDelegate sharedAppDelegate]getName]; 
    NSString *email = [[AppDelegate sharedAppDelegate]getEmail]; 
    NSLog(@"name = %@ and email = %@",name, email); //name = name1234 and email = [email protected] 
} 
+0

另一個選項是取全局常量和NSUserDefault(首選項) –

0

將值存儲在您的應用程序委託中只是一團糟。

可以從推送通知啓動的每個UIViewController都可以符合自定義的「啓動」協議。

每個UIViewController例如'UIViewController2'符合這個協議。

你可以寫這樣的協議:

@protocol LaunchProtocol <NSObject> 
- (void) launchParams:(NSDictionary *)params; 
@end 

每個UIViewController中可以符合該協議中,像這樣:

@interface ViewController2 : UIViewController <LaunchProtocol> 
@end 

@implementation ViewController2 
- (void) launchParams:(NSDictionary *)params { 

} 
@end 

你的應用程序代理只需要知道有關協議,它不不關心你的UIViewControllers。

當您收到推送通知時,檢查視圖控制器是否符合啓動協議。

... 
    vcToOpen.modalPresentationStyle =UIModalPresentationFullScreen; 

    if ([vcToOpen conformsToProtocol:@protocol(LaunchProtocol)]) { 
     UIViewController <LaunchProtocol> *launchController = (UIViewController <LaunchProtocol> *) vcToOpen; 

     NSDictionary* params = @{ /* create your param dict */ }; 
     [launchController launchParams:params]; 
    } 

    [[[[UIApplication sharedApplication] keyWindow] rootViewController] presentViewController:vcToOpen animated:YES completion:nil]; 
... 

您將包括推送通知在「PARAMS」字典的信息,和UIViewController中會提取它從它需要launchParams什麼樣的信息:

- (void) launchParams:(NSDictionary *)params { 
    NSLog(@"Username: %@", params[@"username"]); 
} 
0

其實你可以使用Singleton設計模式來實現這一點。創建一個共享實例類來存儲值。

+ (instancetype)sharedInstance 
{ 
    static dispatch_once_t once; 
    static id sharedInstance; 
    dispatch_once(&once, ^{ 
     sharedInstance = [[self alloc] init]; 
    }); 
    return sharedInstance; 
} 

在需要保存的管理器類中創建屬性,然後從管理器類訪問值。

相關問題