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
Related
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)
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)
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
I have 2 view controllers on my storyboard: viewController1(it input point) and viewController2. After loading the application i want to automatic presenting viewController2 after viewController1.
How i can do that?
if you want to go to second viewcontroller you can add this code to you viewDidLoad method:
let secondViewController = self.storyboard?.instantiateViewController(withIdentifier: "yourIdentifier") as! secondViewController
self.navigationController?.pushViewController(secondViewController, animated: true)
remeber to add a identifier for the second viewcontroller in storyboard and chenge the identifier that you use with "yourIdentifier"
if you don't want a animation put the animated: true to false.
if you dont want to show the fist viewcontroller, go into you storyboard and click on the second viewcontroller, then in the right side on attributes inspector select the box int the image:
For change viewcontroller you have to embed the first viewcontroller with a navigation controller, if you don't know how to do it, just select the fisrt viewcontroller and do like the image, click in navigation controller
Have you tried to show it in func viewDidLoad() {} like:
override func viewDidLoad() {
// ...
self.present(viewController, animated: true, completion: nil)
}
If you want to straight up load a different view controller you can do this in your app delegate:
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let vc = storyboard.instantiateViewController(withIdentifier: "put your identifier here")
self.window?.rootViewController = vc
return true
}
let storyboard = UIStoryboard(name: "ProductSearch_iPhone", bundle: nil)
let controller : RSearchResultController = storyboard.instantiateViewController(withIdentifier: "SResult")
as! RSearchResultController
searchController = UISearchController(searchResultsController:controller)
searchController.searchResultsUpdater = controller
searchController.dimsBackgroundDuringPresentation = true
controller.delegate = self
searchController.searchBar.delegate = controller
self.present(searchController, animated: true, completion: nil)
definesPresentationContext = true
I've added UISearchController by not integrated with UITableview like many Other tutorials.
Finally i found the solution.The issue was not device specific, ios 9 and ios8 shows wrecked design while in ios10 it is fine.
definesPresentationContext = true
The above line of code was the issue, which was written on the intention searchbar should not be there when i navigate to somewhere & comeback while UISearchController is active. But according to my requirement i have no choice of navigation when UISearchController is active.