2014-04-16 26 views
1

我有一個基本的應用程序,登錄/註冊屏幕和主控制器,當用戶登錄後顯示。此外,當通過URL打開應用程序時,我想顯示模態控制器。如何定義登錄/註冊和主控制器工作流程?

我已經嘗試了幾種模式,如何做到這一點,並已解決下面的模式。它的工作,但是,我覺得應該有一個更好的方式來做到這一點比我下面做的。用下面的問題是,當應用程序通過URL

這裏開不開模態控制器是我在做什麼WelcomeController

class WelcomeController < UIViewController 
    include AFNetworkingClient 
    def viewDidLoad 
    super 

    rmq.stylesheet = WelcomeControllerStylesheet 
    rmq(self.view).apply_style :root_view 

    display_login unless AppHelper.user_set? 

    end 
    def display_login 
    @login = LoginController.alloc.init 
    @login.delegate = self 
    self.presentViewController(@login, animated:false, completion:nil) 
    end 
.... 
end 

class AppDelegate 
    attr_reader :window 
    include AFNetworkingClient 

    def application(application, didFinishLaunchingWithOptions:launchOptions) 
    @window = UIWindow.alloc.initWithFrame(UIScreen.mainScreen.bounds) 
    if AppHelper.user_set? 
     initAFNetworkingClient 
     Color.fetch(AFMotion::Client) do |data| 
     main_controller = ColorController.alloc.initWithData(data) 
     @window.rootViewController = UINavigationController.alloc.initWithRootViewController(main_controller) 
     end 
    else 
     main_controller = WelcomeController.alloc.initWithNibName(nil, bundle: nil) 
     @window.rootViewController = UINavigationController.alloc.initWithRootViewController(main_controller) 
    end 
    @window.makeKeyAndVisible 
    true 
    end 

    # This doesn't show the modal controller for some reason 
    def application(application, openURL:url, sourceApplication:sourceApp, annotation:annotation) 
    Color.fetch(AFMotion::Client) do |data| 
     main_controller = ColorController.alloc.initWithData(data) 
     ctlr = UINavigationController.alloc.initWithRootViewController(self.add_color_controller) 
     ctlr.modalTransitionStyle = UIModalTransitionStyleCoverVertical 
     ctlr.delegate = self 
     self.presentViewController(ctlr, animated:true, completion:nil) 
    end 
    end 
    def add_color_controller 
    @add_color_controller ||= MyModalController.new.tap do |ctlr| 
     ctlr.navigationItem.leftBarButtonItem = UIBarButtonItem.alloc.initWithBarButtonSystemItem(
      UIBarButtonSystemItemCancel, 
      target: self, 
      action: :cancel) 

     ctlr.navigationItem.rightBarButtonItem = UIBarButtonItem.alloc.initWithBarButtonSystemItem(
      UIBarButtonSystemItemDone, 
      target: self, 
      action: :done) 

     ctlr.navigationItem.rightBarButtonItem.enabled = false 
    end 
    end 

    def cancel 
    self.dismissViewControllerAnimated(true, completion:nil) 
    end 
end 

部分

問題

  • 有沒有更好的方法來管理我上面提到的工作流程?
  • 如何解決上面的代碼,這樣,當我打開應用程序的URL也不會引發錯誤:Application windows are expected to have a root view controller at the end of application launch

回答

0

所缺少的東西是同時Color.fetch運行,顯示控制器。因此該方法返回並且@window.rootViewController尚未設置爲任何內容,直到調用Color.fetch中的塊爲止。這也是爲什麼presentViewController(..)有麻煩 - 沒有控制器來提交!

所以第一次迭代將是一個「加載」視圖控制器。這不是偉大的用戶體驗,但是...接下來我將修改ColorController來存儲Color.fetch的最後結果並顯示該值,然後在新數據更新UI時進行顯示。我想,還有更多的工作,但更好的用戶體驗。當沒有緩存的數據時,它可以有自己的「加載」屏幕,或者它可以將LoadingViewController作爲模式呈現,就像那樣。

這還包括移動代碼爲AppDelegate的代碼,我強烈地敦促(分離關注點)。我認爲控制器應該負責加載他們自己的數據。