gpt4 book ai didi

ios - 将 NSNotification 发布到新的 ViewController

转载 作者:行者123 更新时间:2023-12-01 17:57:48 24 4
gpt4 key购买 nike

我有带有 5 个选项卡的选项卡式应用程序。

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

当我的应用程序收到推送通知时,我想在索引为 1 的选项卡中推送新的 View Controller 。

我的代码:

应用委托(delegate)

- (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 触发通知时。

如果手动打开选项卡 1 并再次切换回选项卡 0,然后发布通知,它会完美响应。所以我需要在标签 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:instanceOfOtherClass
而不是 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"]];
}

关于ios - 将 NSNotification 发布到新的 ViewController,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14317639/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com