programing tip

initWithRootViewController 이외의 메소드로 UINavigationController의 rootViewController 설정

itbloger 2020. 11. 7. 09:03
반응형

initWithRootViewController 이외의 메소드로 UINavigationController의 rootViewController 설정


다른 방법으로 rootViewController설정을 어떻게 합니까?UINavigationControllerinitWithRootViewController

initWithNavigationBarClass:toolbarClass:내 NavigationController에 대한 사용자 지정 도구 모음을 제공하는 데 사용하고 싶으 므로 initWithRootViewController.


을 호출하여이 문제를 해결할 수 있습니다 setViewControllers.

이렇게 :

UINavigationController *navigationController = [[UINavigationController alloc] initWithNavigationBarClass:[MyNavigationBar class] toolbarClass:[UIToolbar class]];

[navigationController setViewControllers:@[yourRootViewController] animated:NO];

Swift를 사용한 지식 공유 :

앱 delegate.swift 이외의 클래스에서 루트 뷰 컨트롤러 변경

let appdelegate = UIApplication.sharedApplication().delegate as! AppDelegate
let mainStoryboard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
var homeViewController = mainStoryboard.instantiateViewControllerWithIdentifier("HomeViewController") as! HomeViewController
let nav = UINavigationController(rootViewController: homeViewController)
appdelegate.window!.rootViewController = nav

이것이 누군가에게 도움이되기를 바랍니다.

편집 :

애니메이션으로 rootviewcontroller를 변경하려면 다음을 수행하십시오.

 UIView.transitionWithView(self.window!, duration: 0.5, options: UIViewAnimationOptions.TransitionFlipFromLeft, animations: {
    self.window?.rootViewController = anyViewController
}, completion: nil)

우리는 이것 과 너무 유사한 일반화 메소드를 작성할 수 있습니다 .


이것은 나를 위해 작동합니다. 도움이되기를 바랍니다.

let rootVC:LoginViewController = self.storyboard?.instantiateViewControllerWithIdentifier("LoginViewController") as! LoginViewController
let nvc:UINavigationController = self.storyboard?.instantiateViewControllerWithIdentifier("RootNavigationController") as! UINavigationController
nvc.viewControllers = [rootVC]
UIApplication.sharedApplication().keyWindow?.rootViewController = nvc

신속한 3.0 xcode8.1

일반 설정에서 메인 인터페이스에서 삭제 : 메인 <-메인 인터페이스 이후 :

class AppDelegate...

 var window: UIWindow?

    fun application...

      window = UIWindow(frame: UIScreen.main.bounds)
      window?.makeKeyAndVisible()
      window?.rootViewController = UINavigationController(rootViewController: NewYourController)

  let storyboard = UIStoryboard(name: "Main", bundle: nil)         
  let yourNewRootView = storyboard.instantiateViewControllerWithIdentifier("yourNewRootView") as? yourNewRootView


   self.window = UIWindow(frame: UIScreen.mainScreen().bounds)

    UIView.transitionWithView(self.window!, duration: 0.1, options: [UIViewAnimationOptions.TransitionFlipFromRight,UIViewAnimationOptions.TransitionFlipFromLeft], animations: 
    {
        // animation

        }, completion: { (finished: Bool) -> () in

            self.window?.rootViewController = nil
            self.window?.rootViewController = yourNewRootView
            self.window?.makeKeyAndVisible()
    })

참고URL : https://stackoverflow.com/questions/16215034/set-rootviewcontroller-of-uinavigationcontroller-by-method-other-than-initwithro

반응형