How can call UIViewController to SwiftUI View - swiftui

I am try to learn SwiftUI . Try to implement SwiftUI code . I am not clear . please anyone help me.
1) Any view controller to other view Controller . it is no problem .
let vc = viewController.init(nibName: "VCNibName", bundle: nil)
self.navigationController?.pushViewController(vc, animated: true)
or
let newViewController = storyBoard.instantiateViewController(withIdentifier: "VCIdentifier")
self.navigationController?.pushViewController(newViewController, animated: true)
2) Any view controller to SwiftUI view .
how can push SwiftUIView to navigationController?

Use UIHostingController to wrap your View
let hostingController = UIHostingController(rootView: MySwiftUIView())
self.navigationController?.pushViewController(hostingController, animated: true)

Related

Navigation bar title appears with delay in mixed UIKit and SwiftUI project

I'm presenting a SwiftUI view from UIViewController.
let vc = UIHostingController(rootView: SwiftUIView())
navigationController?.pushViewController(vc, animated: true)
struct SwiftUIView: View {
var body: some View {
VStack {
}
.navigationBarTitle("Title")
}
}
The title appears only after the view has appeared. How to fix?
If you use UINavigationController then you need to set the title on the UIHostingController for it to appear instantly. Otherwise the navigation title will only appear after the SwiftUI view is rendered.
You can set the navigation title of any UIViewController using title
let vc = UIHostingController(rootView: SwiftUIView())
vc.title = "Title"
navigationController?.pushViewController(vc, animated: true)

How to navigate back to correct previous viewController

Hi I have the following scenario:
|---> VC1----->|
VC_Home ->|---> VC2----->|------> VCm
|---> VC3----->|
VC1, VC2 and VC3 can go to the same ViewController (VCm)
How to navigate back to the correct Previous VC from VCm
if I start from VC1 and go to VCm, then Vcm will return to VC1.
So, the same for VC2 and VC3.
all VC1, VC2, VC3 dont embed with NavigationController.
will this be able to identify which is the previous page?
#IBAction func goBack(_ sender: Any){
navigationController?.popViewController(animated: true)
}
Please help.
Thanks
Add UINavigationController in your storyBoard
Add new UIViewController [which is VC_HOME] and link it with UINavigationController as root View Controller (ctrl + drag)
Give it an ID (right utilities view -> Identity Inspector -> StoryBoard ID -> Name it "myHome")
Add 3 button [go to vc1, go to vc2, go to vc3] and of course add #IBAction for each of them
Add 3 different UIViewController for vc1, vc2, vc3 and like step 3 give them IDs myVc1, myVc2, myVc3
Now for #IBAction go to vc1 add following code :
let view = storyboard!.instantiateViewController(withIdentifier: "myVc1")
navigationController?.pushViewController(view, animated: true)
for #IBAction go to vc2 add following code :
let view = storyboard!.instantiateViewController(withIdentifier: "myVc2")
navigationController?.pushViewController(view, animated: true)
for #IBAction go to vc3 add following code :
let view = storyboard!.instantiateViewController(withIdentifier: "myVc3")
navigationController?.pushViewController(view, animated: true)
Now for each view add button go to VC_M like steps above
for back to previous viewController [vc1, vc2, vc3] use this code :
navigationController?.popViewController(animated: true)
if you back to the root [VC_Home] use this : navigationController?.popToRootViewController(animated: true)
just dismiss you final view controller. add following to you back action in your final view controller.
dismiss(animated: true, completion: nil)

How to start view controller from non viewcontroller class

I want to open present uiviewcontroller from class that inherts from nsobject .how this work?
I tried this code
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let initialViewController = storyboard.instantiateViewController(withIdentifier: "LoginSignupVC")
self.present (initialViewController, animated: false, completion: nil)
In your code self.present wont work because you're inside a NSObject class.
So self refers to NSObject class.
NSObject class doesn't have a method present in it.
For presenting a view controller you must have a navigation controller as root.
Solution 1: So get the navigation controller using self.navigationController and present your view controller to it.
let initialViewController = self.storyboard!.instantiateViewControllerWithIdentifier("LoginSignupVC") as! ViewController
self.navigationController!.presentViewController(initialViewController, animated: true)
Solution 2: Create a navigationcontroller manually and present in it.
let initialViewController = self.storyboard!.instantiateViewControllerWithIdentifier("LoginSignupVC") as! ViewController
let navController = UINavigationController(rootViewController: initialViewController)
self.presentViewController(navController, animated:true, completion: nil)

Swift 3 how to present splitview controller modally?

Hi I'm trying to present UiSplitViewController from UiViewController. I'm doing some search in UiViewController and based on result I display UiSplitViewController. How can I do it programatically? So far I tried 3 methods after googling
1.self.performSegue(withIdentifier: "SearchComplete", sender: self)
2.self.present(PrimarySplitViewController(), animated: true, completion: nil)
3.let detailViewController = self.storyboard?.instantiateViewController(withIdentifier: "SplitView") as! UISplitViewController
self.splitViewController?.viewControllers[1] = detailViewController
None of the above methods have worked how can I solve this. Thank you.
let mainStoryboard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
let viewController = mainStoryboard.instantiateViewController(withIdentifier: "SplitView") as! UISplitViewController
UIApplication.shared.keyWindow?.rootViewController = viewController
You have to set root view controller

Can't push from programmatically created UINavigationController

In a Swift 3 project using Xcode 8.3 and iOS 10.3, I get a navigation controller to push. The app runs with a navigation controller until I try to use it to push. All works until the last line which cause an app crash, fatal error: unexpectedly found nil while unwrapping an Optional value. I don't want to use the storyboard, the point of this question is how to accomplish this task without the storyboard.
App Delegate code
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
window = UIWindow(frame: UIScreen.main.bounds)
window?.makeKeyAndVisible()
window?.rootViewController = UINavigationController(rootViewController: MainViewController())
}
View Controller:
import UIKit
class MainViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
view.backgroundColor = .white
let navController = navigationController
let myProfileViewController = MyProfileViewController()
navController!.pushViewController(myProfileViewController, animated: true)
}
}
Move
let navController = navigationController
let myProfileViewController = MyProfileViewController()
navController!.pushViewController(myProfileViewController, animated: true)
from viewDidLoad to viewDidAppear.
In viewDidLoad there is no parent navigation controller set up and you should not start animations from there.
You can add navigation controller in storyboard,
in Storyboard click on MainViewController & from editor menu add navigation controller & push via navigation controller as below code.
let vc = self.storyboard?.instantiateViewController(withIdentifier: "MyProfileViewController") as! MyProfileViewController
self.navigationController?.pushViewController(vc, animated: true)
Make sure storyboard identifier is correct. & remove navigation controller code form AppDelegate class.
first give name to your navigation controller like this
then in your code initialise it like this
let nav = (UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "yourNavigation") as! UINavigationController);
nav.pushFrontViewController(MainViewController(), animated: true);
window?.rootViewController = nav