gpt4 book ai didi

ios - 如何加载 View "properly"iOS

转载 作者:行者123 更新时间:2023-11-29 03:06:25 24 4
gpt4 key购买 nike

我的应用程序由导航 Controller 和 View Controller 组成。我的 UI 的某些部分是在 Storyboard上完成的,而某些部分是从代码(在 viewDidLoad 中)启动的。我的目标是在通过 appDelegate 中的此方法“正确地”从推送通知启动应用程序时加载 childViewController X(由后退按钮、导航栏、标签和表格组成):

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions

这对你们来说可能是一个简单的问题,但过去几天我已经问了几个问题(您可以查看我的问题历史记录)。在尝试了几种方法后,我的应用要么:

  • 崩溃
  • 加载时没有导航栏
  • 加载导航栏但没有标签(后退按钮不起作用)
  • 加载导航栏但下方黑屏

只有 tableView 被拖到 Storyboard上。导航栏是推断的,但我有指示其后退按钮和标题的代码。其余的都是通过 .m 文件中的代码完成的。

我知道以前有人问过这个问题,但没有一种方法有效。如何通过推送通知正确加载此 childViewController?

编辑/更新:

到目前为止我的代码:

 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
NSDictionary *dictionary = [launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey];

if (dictionary != nil)
{

UIStoryboard *mainstoryboard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil];
UIViewController* firstVC = [mainstoryboard instantiateViewControllerWithIdentifier:@"NotificationsViewController"];
[self.window.rootViewController addChildViewController:firstVC];
[(UINavigationController *)self.window.rootViewController pushViewController:firstVC animated:NO];
self.window.rootViewController.childViewControllers);
}}

错误代码:

'-[SWRevealViewController pushViewController:animated:]: unrecognized selector sent to instance 0x14575f20'

SWRevealViewController 是我的侧边栏菜单 View Controller 库。

更新 2:我也试过这个方法:

UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil];
UIViewController *initViewController = [storyboard instantiateViewControllerWithIdentifier:@"NotificationsViewController"];

self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
self.window.rootViewController = initViewController;
[self.window makeKeyAndVisible];

这加载了正确的 viewController 但没有导航栏。

最佳答案

试试这个:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
NSDictionary *dictionary = [launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey];

if(dictionary != nil) {
UIStoryboard *mainstoryboard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil];
UIViewController *firstVC = [mainstoryboard instantiateViewControllerWithIdentifier:@"NotificationsViewController"];
UINavigationController *navigationController = (UINavigationController *)self.window.rootViewController;

// Navigation already created
if(navigationController) {
[navigationController pushViewController:firstVC animated:NO];

// Navigation not created
} else {
UINavigationViewController *navigationController = [[UINavigationViewController alloc] initWithRootController:firstVC];

[self.window setRootViewController:firstVC];
[self.window makeKeyAndVisible];
}
}

return YES;
}

你的代码有问题:

  1. 您需要有一个 UINavigationController 才能拥有导航栏并管理推送/弹出操作,因此您的窗口根 Controller 应该是 UINavigationController .

  2. 如果您需要初始化UINavigationController,最好且简单的方法是直接使用根 Controller 初始化它,因此将推送/弹出留给用户actions:应用程序总是有一个根 Controller ,那么为什么不立即创建它呢?

  3. addChildController”是另一回事:它用于创建自定义容器 Controller ,这意味着您必须手动实现所有业务逻辑。我建议您只有在经验足够的情况下才使用它,因为这可能很困难 - Cocoa 拥有足够的组件,可以将其留给一小部分应用程序。

  4. 您正在使用第三方 Controller “SWRevealViewController”。他们应该有一个示例项目,您可以尝试“复制”它,然后他们根据您自己的目的对其进行自定义。

请告诉我您的代码是否运行良好,否则请发布新错误,我会尽力帮助您

关于ios - 如何加载 View "properly"iOS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22719015/

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