2012-12-23 37 views
0

我喜歡展示在標籤中最新的推送通知我的主要故事板我使用此代碼顯示在我的AppDelegate.m警告消息:是否可以在StoryBoard中的標籤中顯示最新的推送通知?

-(void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo { 

    NSDictionary *test =(NSDictionary *)[userInfo objectForKey:@"aps"]; 
    NSString *alertString =(NSString *) [test objectForKey:@"alert"]; 
    NSLog(@"String recieved: %@",alertString); 


    UIApplicationState state = [[UIApplication sharedApplication] applicationState]; 

    if (state == UIApplicationStateActive) { 
     UIAlertView *alertmessage=[[UIAlertView alloc]initWithTitle:@"Geier" 
                  message:alertString             delegate:self 
                cancelButtonTitle:@"OK" 
                otherButtonTitles:nil]; 


     [alertmessage show]; 

     AudioServicesPlaySystemSound(1002); 


    } 

} 

我在ViewController.m文件試過這種latestpush.text = @"%@",alertString;但它不起作用。

有人可以幫助我嗎?

謝謝:)

回答

1

你需要使文本提供給視圖控制器。

您可以通過發送一個定製NSNotification與所述提示消息,從內application:didReceiveRemoteNotification:

[[NSNotificationCenter defaultCenter] 
     postNotificationName:@"PushAlertNotification" 
     object:self 
     userInfo:@{@"alertString":alertString}]; 

在您的視圖控制器的viewDidLoad方法做到這一點,註冊作爲觀察員:

[[NSNotificationCenter defaultCenter] addObserver:self 
             selector:@selector(updateStoryboard:) 
             name:@"PushAlertNotification" 
             object:nil]; 

,並創建updateStoryboard:方法在視圖控制器:

- (void) updateStoryboard:(NSNotification *) notification { 
    self.latestpush.text = notification.userInfo[@"alertString"]; 
} 

另一種解決方案是在您的AppDelegate中創建一個以ViewController作爲觀察者的屬性。

AppDelegate.h(將ViewController更改爲VC的實際類型)。

@property (nonatomic, weak) ViewController *observer;

的視圖控制器中創建一個接受的NSString的方法和有方法更新您的故事板。

ViewController.m

-(void)updateStoryboard(NSString *alertString) { 
    self.latestpush.text = alertString; 
} 

此外,在您的ViewContoller的viewDidLoad方法,自己註冊用的appDelegate:

- (void)viewDidLoad { 
    [super viewDidLoad]; 
    AppDelegate *delegate = (AppDelegate *)[UIApplication sharedApplication].delegate; 
    delegate.observer = self; 
} 

呼叫updateStoryboard您application:didReceiveRemoteNotification:方法內:

[self.observer updateStoryboard:alertString];

+0

@j_Shapiro B其他方法不會爲我工作:(但我確切地說你是如何說的。 –

+0

給我更多信息繼續...他們怎麼不工作?在創建通知之前alertString爲零嗎?你看到什麼錯誤? –

+0

Push沒有錯誤,但標籤沒有填充Push消息。 –

相關問題