我正在一个用Objective编写的应用程序(只有一个类)和用Swift编写的应用程序的其余部分。在我的应用程序中,我有3个故事板,这些故事板相应地有viewControllers
。
问题:有时候我使用beow代码更改
rootViewController
,推送segues动画在此之后停止工作。
// constant to share globally
let kApplicationDelegate = UIApplication.shared.delegate as? AppDelegate
let controller = UIStoryboard().getControllerInstance(storyBoardName: "Autograph", identifire: "loginSuccessVC")
kApplicationDelegate?.window?.rootViewController = controller
答案 0 :(得分:3)
I know it's bit late to reply for this Question! But better late than Never!
First things First:
For Push transition to work properly, you have to have UINavigationController
hosting your current ViewController as the rootViewController
. What the heck does that mean?
Example:
There are two or many ViewControllers (here onwards ViewController == VC) A, B, and more. If you are currently in VC A and you want to move to VC B doing a Push (or Detail) transition on a button action in VC A, then remember your VC A has to be the RootViewController of a UINavigationController
.
[ UI ] [ ] [ ]
[ Navigation ]-- RootViewController ==O>[ A ]--- Push -->[ B ]
[ Controller ] [ ] [ ]
Otherwise it doesn't work. It would appear as a Present Modally transition.
So far there are two ways I have recongised to achieve this. One is pure programmatically, the other is using StoryBoard and Code hand in hand.
Programmatically:
For the ease of explaining I would embed my UINavigationController
at the beginning of the UIViewController
stack. In order to achieve this, we have to do a small alteration to AppDelegate
's didFinishLaunchingWithOptions launchOptions
method. It should look like following:
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
window = UIWindow(frame: UIScreen.main.bounds)
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let initialVCIdentifier = "AViewController" // A ViewController **
let firstVC = storyboard.instantiateViewController(withIdentifier: initialVCIdentifier)
let nav = UINavigationController(rootViewController: firstVC) // Assigning the A VC as the RootViewController of the NavigationController
window?.rootViewController = nav // Look for the *** Note bellow
window?.makeKeyAndVisible()
return true
}
** You have to denote the ViewController Identifier for A VC within the InterfaceBuilder. And I assume that your storyboard name is Main.storyboard.
*** If you are not doing this for initial View controller, just present this UINavigationController as a modal VC within your VC workflow path
In the action for the button in VC A, it should look like following:
@IBAction func TestBtnAct(_ sender: Any) {
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let vc = storyboard.instantiateViewController(withIdentifier: "BViewController") as UIViewController
navigationController?.pushViewController(vc, animated: true)
}
That's it! I think if you have tried it out as I've mentioned above, it should do the magic already.
Storyboard and Code hand-in-hand:
This means your VC A is already being hosted by a UINavigationController
within the storyboard design. In this case you don't need to programmatically create the UINavigationController
and embed the VC A as the RootViewController. Just having the above Button action might be sufficient.
Hope this would be helpful to someone out there. And here is a sample project I have done and uploaded in GitHub for learning purposes of all of yours.
Cheers!
答案 1 :(得分:2)
当您更改rootViewController
尝试将navigationController
设置为根而不是navigationController
的{{1}}时。
假设您有一个viewController,我们将它命名为rootViewController
是VCA
UINavigationController
的rootViewController。然后,不要将Nav1
设为VCA
rootViewController
。尝试将kApplicationDelegate?.window?
作为Nav1
的{{1}}。
rootViewController
动画无效,因为它可能不是kApplicationDelegate?.window?
。你推动的kApplicationDelegate?.window?.rootViewController = Nav1
的{{1}}可能是零。