簡體   English   中英

如何“正確”加載iOS視圖

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

我的應用程序包含導航控制器和視圖控制器。 我的UI的某些部分是在情節提要上完成的,而某些部分是通過代碼(在viewDidLoad中)啟動的。 我的目標是通過appDelegate中的此方法從推送通知“適當”啟動應用程序時加載childViewController X(包括后退按鈕,導航欄,標簽和表格):

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

對你們來說,這可能是一個簡單的問題,但過了幾天我已經問了幾個問題(您可以查看我的問題歷史記錄)。 在嘗試了幾種方法之后,我的應用要么:

  • 崩潰
  • 沒有導航欄的加載
  • 加載導航欄但沒有標簽(后退按鈕不起作用)
  • 加載帶有導航欄,但下方有黑屏

僅將tableView拖到情節提要上。 可以推斷出導航欄,但是我有指示其后退按鈕和標題的代碼。 其余全部通過.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是我的側邊欄菜單視圖控制器的庫。

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

這將加載正確的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才能具有導航欄並管理push / pop操作,因此您的窗口根控制器應為UINavigationController

  2. 如果您需要初始化UINavigationController ,最好的簡單方法是直接用根控制器初始化,因此將push / pop留給用戶操作:應用程序始終具有根控制器,那么為什么不立即創建它呢?

  3. addChildController ”是另一回事:它用於創建自定義容器控制器,這意味着您必須手動實現所有業務邏輯。 我建議您僅在有足夠經驗的情況下使用它,因為這可能很困難-Cocoa有足夠的組件將其留給少量應用程序使用。

  4. 您正在使用第三方控制器“ SWRevealViewController ”。 他們應該有一個示例項目,您可以嘗試“復制”該項目,並根據自己的目的對其進行自定義。

讓我知道您的代碼是否運行良好,否則請發布新錯誤,我會盡力幫助您

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM