Ios 向新的ViewController发布通知

Ios 向新的ViewController发布通知,ios,uitabbarcontroller,push-notification,nsnotificationcenter,nsnotification,Ios,Uitabbarcontroller,Push Notification,Nsnotificationcenter,Nsnotification,我有5个标签的标签应用程序 应用程序在索引为0的选项卡上启动 当我的应用程序收到推送通知时,我想在索引为1的选项卡中推送新的视图控制器 我的代码: AppDelegate - (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)pushData { UITabBarController *tabb = (UITabBarController *)self.w

我有5个标签的标签应用程序

应用程序在索引为0的选项卡上启动

当我的应用程序收到推送通知时,我想在索引为1的选项卡中推送新的视图控制器

我的代码:

AppDelegate

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)pushData {
    UITabBarController *tabb = (UITabBarController *)self.window.rootViewController;
    tabb.selectedIndex = 1;
    [[NSNotificationCenter defaultCenter] postNotificationName:@"pushImage" object:@"this is my item id from pushData"];
}
ProfileViewController选项卡索引1

- (void)viewDidLoad
{
    [super viewDidLoad];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(pushImage:) name:@"pushImage" object:nil];
}

-(void) pushImage: (NSNotification*) notification {
    NSString* text = notification.object;
    NSLog(@"My id from pushData: %@", text);
}
我的问题是ProfileViewController无法响应通知,因为当AppDelegate触发通知时,初始化尚未完成

如果a手动打开选项卡1并再次切换回选项卡0,a然后发布通知,它将完全响应该通知。因此,我需要在加载选项卡1后发布通知,我如何才能传递此信息

我在TabBarApplication中从AppDelegate推送新VC的解决方案

    - (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)pushData {    
            // ...... 

            if([[pushData objectForKey:@"type"] integerValue] == 0){
                // ....
            }
            else if([[pushData objectForKey:@"type"] integerValue] == 2){
                [self handleLikePush:pushData applicationState:application.applicationState];
            }
}

-(void)handleLikePush:(NSDictionary *)pushData applicationState:(UIApplicationState) applicationState{

    //..

    DetailImageViewController *detailImage = [[DetailImageViewController alloc] initWithImageId:imageId];
        [self pushViewControllerToCurrentTab:detailImage];
    }

}

- (void)pushViewControllerToCurrentTab:(UIViewController *)vc{

    UITabBarController *tabBarController = (UITabBarController *)self.window.rootViewController;
    UINavigationController *selectedTabNC = (UINavigationController *)tabBarController.selectedViewController;

    if (selectedTabNC != nil) {
        [selectedTabNC pushViewController:vc animated:YES];
    }
    else{
        NSLog(@"NavigationController not found");
    }    
}
你可以用

addObserver:InstanceFotherClass

而不是addObserver:self

在appDelegate中添加以下行:

ProfileViewController *pvController=[ProfileViewController new];
[[NSNotificationCenter defaultCenter] addObserver:pvController selector:@selector(pushImage:) name:@"pushImage" object:nil];
对这种方法

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)pushData {
      UITabBarController *tabb = (UITabBarController *)self.window.rootViewController;
      tabb.selectedIndex = 1;
      [[NSNotificationCenter defaultCenter] postNotificationName:@"pushImage" object:@"this is my item id from pushData"];

     //****   add here

     ProfileViewController *pvController=[ProfileViewController new];
    //[[NSNotificationCenter defaultCenter] addObserver:pvController selector:@selector(pushImage:) name:@"pushImage" object:nil];

    [[NSNotificationCenter defaultCenter] postNotificationName:@"pushIamge" object:pvController];// userInfo:[NSDictionary dictionaryWithObject:@"1,2,3,4,5" forKey:@"categories_ids"]]; 
}
你可以用

addObserver:InstanceFotherClass

而不是addObserver:self

在appDelegate中添加以下行:

ProfileViewController *pvController=[ProfileViewController new];
[[NSNotificationCenter defaultCenter] addObserver:pvController selector:@selector(pushImage:) name:@"pushImage" object:nil];
对这种方法

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)pushData {
      UITabBarController *tabb = (UITabBarController *)self.window.rootViewController;
      tabb.selectedIndex = 1;
      [[NSNotificationCenter defaultCenter] postNotificationName:@"pushImage" object:@"this is my item id from pushData"];

     //****   add here

     ProfileViewController *pvController=[ProfileViewController new];
    //[[NSNotificationCenter defaultCenter] addObserver:pvController selector:@selector(pushImage:) name:@"pushImage" object:nil];

    [[NSNotificationCenter defaultCenter] postNotificationName:@"pushIamge" object:pvController];// userInfo:[NSDictionary dictionaryWithObject:@"1,2,3,4,5" forKey:@"categories_ids"]]; 
}

您是否尝试过将addObserver:方法添加到视图控制器的init方法中?

您是否尝试过将addObserver:方法添加到视图控制器的init方法中?

从其他类添加观察者。您能否更深入地描述此解决方案?可能是代码示例,thxAdd observer来自其他类。您能更深入地描述一下这个解决方案吗?可能是代码示例,我不是最好的。。。但是一天比一天好:ppushImage:不会推我的新ViewController;我创建了ProfileViewController的新实例。所以*pvController并不真正属于tabviewController索引1,对吗?pushImage:不是在推一个新的VC吗?我更新了我的代码,感到困惑是我以前的答案错了吗?我不是最好的。。。但是一天比一天好:ppushImage:不会推我的新ViewController;我创建了ProfileViewController的新实例。所以*pvController实际上不属于tabviewController索引1,对吗?pushImage:不是在推一个新的VC吗?我更新了我的代码,感到困惑是我以前的答案错了吗?tabIndex 1上的UITableViewController没有响应:-idIt{}抱歉,我的意思是,无论您的类的专用初始值设定项是什么,比如initWitCoder:或initWithNibName:…您也可以从viewcontroller方法调用viewdidload方法,对吗?-idinitWithNibName:NSString*nibNameOrNil bundle:NSBundle*nibBundleOrNil{self=[super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];如果self{[self viewDidLoad];}返回self;}否,不要自己调用viewDidLoad-在视图控制器将其视图层次结构加载到内存后调用此方法,因此,如果您自己也调用它,它将被调用两次。tabIndex 1上的UITableViewController没有响应:-idinit{}对不起,我的意思是,无论您的类的专用初始值设定项是什么,都可以从viewcontroller方法调用viewdidload方法,对吗idinitWithNibName:NSString*nibNameOrNil bundle:NSBundle*nibBundleOrNil{self=[super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];如果self{[self viewDidLoad];}返回self;}否,不要自己调用viewDidLoad-在视图控制器将其视图层次结构加载到内存后调用此方法,因此,如果你自己也这样称呼它,它将被称为两次。