简体   繁体   English

如何“正确”加载iOS视图

[英]How can I load a view “properly” iOS

My app consists of a navigation controller and view controllers. 我的应用程序包含导航控制器和视图控制器。 Some parts of my UI is done on storyboard and some are initiated from code (in viewDidLoad). 我的UI的某些部分是在情节提要上完成的,而某些部分是通过代码(在viewDidLoad中)启动的。 My goal is to load childViewController X (consists of a back button, nag bar, label and table) when the app is launched from a push notification "properly" through this method in appDelegate: 我的目标是通过appDelegate中的此方法从推送通知“适当”启动应用程序时加载childViewController X(包括后退按钮,导航栏,标签和表格):

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

This might be a simple question to you guys but I've asked several questions the pass few days (you can look at my questions history). 对你们来说,这可能是一个简单的问题,但过了几天我已经问了几个问题(您可以查看我的问题历史记录)。 After trying several methods, my app either: 在尝试了几种方法之后,我的应用要么:

  • crashes 崩溃
  • loads without navigation bar 没有导航栏的加载
  • loads with navigation bar but no label (back button does not work) 加载导航栏但没有标签(后退按钮不起作用)
  • loads with navigation bar but with black screen underneath 加载带有导航栏,但下方有黑屏

Only the tableView is dragged onto storyboard. 仅将tableView拖到情节提要上。 The navigation bar is inferred but I have code that dictates its back button and title. 可以推断出导航栏,但是我有指示其后退按钮和标题的代码。 The rest is all done through code in the .m file. 其余全部通过.m文件中的代码完成。

I know people have asked this question before but none of the methods worked. 我知道人们以前曾问过这个问题,但没有一种方法有效。 How can I load this childViewController properly through a push notification? 如何通过推送通知正确加载此childViewController?

EDIT/UPDATE: 编辑/更新:

My code so far: 到目前为止,我的代码:

 - (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);
}}

Error Code: 错误代码:

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

SWRevealViewController is my library for my sidebar menu view controller. SWRevealViewController是我的侧边栏菜单视图控制器的库。

UPDATE2: I've also tried this method: UPDATE2:我也尝试过这种方法:

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];

This loads the correct viewController but without a navigation bar. 这将加载正确的viewController,但没有导航栏。

Try this: 尝试这个:

- (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;
}

You had some problem in your code: 您的代码有问题:

  1. You need to have a UINavigationController in order to have a navigation bar and manage the push/pop actions, so your window root controller should be a UINavigationController . 您需要具有UINavigationController才能具有导航栏并管理push / pop操作,因此您的窗口根控制器应为UINavigationController

  2. If you need to initialize a UINavigationController the best and simple way is to initialize it with a root controller directly, so leave the push/pop for user actions: an application has always a root controller, so why don't you create it immediately? 如果您需要初始化UINavigationController ,最好的简单方法是直接用根控制器初始化,因此将push / pop留给用户操作:应用程序始终具有根控制器,那么为什么不立即创建它呢?

  3. The " addChildController " is another thing: it is used to create custom container controller, it means that you have to implement all the business logic manually. addChildController ”是另一回事:它用于创建自定义容器控制器,这意味着您必须手动实现所有业务逻辑。 I suggest you to use it only if you are experienced enough, since it could be difficult - Cocoa has enough components to leave it for a small set of applications. 我建议您仅在有足够经验的情况下使用它,因为这可能很困难-Cocoa有足够的组件将其留给少量应用程序使用。

  4. You are using a third part controller, the " SWRevealViewController ". 您正在使用第三方控制器“ SWRevealViewController ”。 They should have an example project, you can try to "copy" it and them customize it for your own purpose. 他们应该有一个示例项目,您可以尝试“复制”该项目,并根据自己的目的对其进行自定义。

Let me know if your code works well, otherwise post the new error and I'll try to help you 让我知道您的代码是否运行良好,否则请发布新错误,我会尽力帮助您

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM