I am changing the RootViewController of SwRevealViewController
My code:
let next = self.storyboard?.instantiateViewController(withIdentifier: "SWRevealViewController") as! SWRevealViewController
next.loadView()
let mainStoryboard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
let viewController = mainStoryboard.instantiateViewController(withIdentifier: "MessageViewController") as! MessageViewController
UIApplication.shared.keyWindow?.rootViewController = viewController;
self.present(next, animated: true, completion: nil)
But I get this error:
>Presenting view controllers on detached view controllers is discouraged <ios.MesajDetayViewController: 0x7f8718d646f0>.
How to solve?
solution
let sw = storyboard?.instantiateViewController(withIdentifier: "SWRevealViewController") as! SWRevealViewController
self.view.window?.rootViewController = sw
let mainStroyBoard:UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
let desController = mainStroyBoard.instantiateViewController(withIdentifier: "MessageViewController") as! MessageViewController
let newFrontViewController = UINavigationController.init(rootViewController:desController)
sw.pushFrontViewController(newFrontViewController, animated: true)
if you want to change SWRevealViewContoller front view controller, you should use
let newFronViewController = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "YourID") as! YourClass
self.revealViewController().setFront(newFronViewController, animated: false)
I can update my answer depending on where you would like to call this.
Related
I'm trying to set a Container Controller (Root View Controller) as the delegate for a Nav Bar Item for a View Controller in a Tab Bar Controller and cannot figure out how to do this without using:
let rootViewController = UIApplication.shared.keyWindow?.rootViewController
This code works as I'd like it to:
let rootViewController = UIApplication.shared.keyWindow?.rootViewController
guard let doghouseViewController = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "DoghouseVC") as? DoghouseViewController else { return }
doghouseViewController.delegate = rootViewController as? DoghouseViewControllerDelegate
let nav1 = UINavigationController(rootViewController: doghouseViewController)
guard let statsViewController = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "StatsVC") as? StatsViewController else { return }
statsViewController.delegate = rootViewController as? StatsViewControllerDelegate
let nav2 = UINavigationController(rootViewController: statsViewController)
dailyWrapUpViewController = DailyWrapUpViewController()
guard let calendarViewController = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "CalendarVC") as? CalendarViewController else { return }
calendarViewController.delegate = rootViewController as? CalendarViewControllerDelegate
let nav3 = UINavigationController(rootViewController: calendarViewController)
guard let settingsViewController = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "SettingsVC") as? SettingsViewController else { return }
settingsViewController.delegate = rootViewController as? SettingsViewControllerDelegate
let nav4 = UINavigationController(rootViewController: settingsViewController)
doghouseViewController.tabBarItem = UITabBarItem(title: "Doghouse", image: UIImage(named: "doghouse"), tag: 0)
statsViewController.tabBarItem = UITabBarItem(title: "Stats", image: UIImage(named: "stats"), tag: 1)
// Custom Doggy Bag Button
let controller2 = UIViewController()
calendarViewController.tabBarItem = UITabBarItem(title: "Calendar", image: UIImage(named: "calendar"), tag: 3)
settingsViewController.tabBarItem = UITabBarItem(title: "More", image: UIImage(named: "more"), tag: 4)
viewControllers = [nav1, nav2, controller2, nav3, nav4]
// Use the view controller reference to select the second tab
selectedViewController = nav1
But, 'keyWindow' was deprecated in iOS 13.0 and if I try to change it using the following as an example, the delegate functionality doesn't work (button gets pushed, but Container Controller instance does not receive):
let rootViewController = UIApplication.shared.windows.first!.rootViewController as! ContainerController
For more context, the Container Controller (Root View Controller) is a side menu that slides out on the tap of a NavBar Item within the TabBarController.
How do I solve this?
Figured it out:
let sceneDelegate = UIApplication.shared.connectedScenes.first!.delegate as! SceneDelegate
guard let doghouseViewController = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "DoghouseVC") as? DoghouseViewController else { return }
doghouseViewController.delegate = sceneDelegate.window!.rootViewController as? DoghouseViewControllerDelegate
let nav1 = UINavigationController(rootViewController: doghouseViewController)
guard let statsViewController = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "StatsVC") as? StatsViewController else { return }
statsViewController.delegate = sceneDelegate.window!.rootViewController as? StatsViewControllerDelegate
let nav2 = UINavigationController(rootViewController: statsViewController)
dailyWrapUpViewController = DailyWrapUpViewController()
guard let calendarViewController = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "CalendarVC") as? CalendarViewController else { return }
calendarViewController.delegate = sceneDelegate.window!.rootViewController as? CalendarViewControllerDelegate
let nav3 = UINavigationController(rootViewController: calendarViewController)
guard let settingsViewController = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "SettingsVC") as? SettingsViewController else { return }
settingsViewController.delegate = sceneDelegate.window!.rootViewController as? SettingsViewControllerDelegate
let nav4 = UINavigationController(rootViewController: settingsViewController)
doghouseViewController.tabBarItem = UITabBarItem(title: "Doghouse", image: UIImage(named: "doghouse"), tag: 0)
statsViewController.tabBarItem = UITabBarItem(title: "Stats", image: UIImage(named: "stats"), tag: 1)
// Custom Doggy Bag Button
let controller2 = UIViewController()
calendarViewController.tabBarItem = UITabBarItem(title: "Calendar", image: UIImage(named: "calendar"), tag: 3)
settingsViewController.tabBarItem = UITabBarItem(title: "More", image: UIImage(named: "more"), tag: 4)
viewControllers = [nav1, nav2, controller2, nav3, nav4]
// Use the view controller reference to select the second tab
selectedViewController = nav1
This is my code to navigate from one viewController to another viewControler but i can't navigate. data is rest api nsobject data that stored category from rest api as name, entertainment is a category . when i tap on category goto another viewController
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath)
{
print("hello welcome on this news page")
// dismiss(animated: true, completion: nil)
if data[indexPath.row].name == "Entertainment"
{
let storyboard = UIStoryboard(name: "Main", bundle: Bundle.main)
let destination = storyboard.instantiateViewController(withIdentifier: "WhiteViewController") as! FourthViewController
navigationController?.pushViewController(destination, animated: true)
dismiss(animated: true, completion: nil)
print("Welcome on Business..!")
}
}
If you are using storyboard use this -
if let navigationObject = UIStoryboard(name: "storyboard_name", bundle: nil).instantiateViewController(withIdentifier: "viewcontroller_storyboardId") as? viewController_name {
navigationController?.pushViewController(navigationObject, animated: true)
}
There could be come following reasons
Check if name contains entertainment Or Entertainment as string
value? means case sensitivity(as a test do following and check)
data[indexPath.row].name.lowerCased() == "Entertainment".lowerCased()
Missing navigationController (navigation controller not embedded) if missing embed
navigationController to your root controller.
try removing dismiss(animated: true, completion: nil)
Simply Use This,
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath)
{
if data[indexPath.row].name == "Entertainment" {
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let whiteVC = storyboard.instantiateViewController(withIdentifier: "WhiteViewController") as! WhiteViewController
self.navigationController?.pushViewController(whiteVC, animated: true)
}
}
I am receiving Push Notifications when i click on Notification i want to go to a particular ViewController opens when click i click on SlideMenu didSelectRow. I tried many SO links but below code i am able to go to a particular ViewController but problem is tabBar it not showing.
func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any]) {
let mainstoryboard = UIStoryboard(name: "Main", bundle: nil)
let ringingVC = mainstoryboard.instantiateViewController(withIdentifier: "RaffleResultViewController") as? RaffleResultViewController
window?.rootViewController = ringingVC
}
let mainstoryboard = UIStoryboard(name: "Main", bundle: nil)
let ringingVC = mainstoryboard.instantiateViewController(withIdentifier: "RaffleResultViewController") as? RaffleResultViewController
let selectedVC = window.rootViewController
if let nav = selectedVC as? UINavigationController{
nav.pushViewController(ringingVC, animated: true)
}else{
selectedVC.present(ringingVC, animated: true, completion: nil)
}
// USE this
I am using SWRevealViewController
When i was click on BackButton getting this error
fatal error: unexpectedly found nil while unwrapping an Optional value
my code
let nesne:SWRevealViewController = self.revealViewController()//the error here
let mainStroyBoard:UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
let desController = mainStroyBoard.instantiateViewController(withIdentifier: "MessageViewController") as! MessageViewController
let newFrontViewController = UINavigationController.init(rootViewController:desController)
nesne.pushFrontViewController(newFrontViewController, animated: true)
solution
let sw = storyboard?.instantiateViewController(withIdentifier: "SWRevealViewController") as! SWRevealViewController
self.view.window?.rootViewController = sw
let mainStroyBoard:UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
let desController = mainStroyBoard.instantiateViewController(withIdentifier: "MessageViewController") as! MessageViewController
let newFrontViewController = UINavigationController.init(rootViewController:desController)
sw.pushFrontViewController(newFrontViewController, animated: true)
I am having a perplexing problem in Swift 3. I'm presenting a ViewController and, for some reason, the parent ViewController gets loaded again thus causing an error. My code for loading my ViewController is:
let vc = self.storyboard?.instantiateViewController(withIdentifier: "jobcodes") as! JobCodesViewController
vc.badge=self.badgeNum
vc.jobnumber=self.keysPressed
vc.show=result as! String
self.present(vc, animated: true, completion: nil)
I made sure the identifier wasn't tied to the wrong view and it is not.
Try this
static func instantiate() -> JobCodesViewController
{
return UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "JobCodesViewController") as! JobCodesViewController
}
let vc = JobCodesViewController.instantiate()
self.present(vc, animated: true, completion: nil)
You can only present after viewdidAppear
I figured out what my problem was. The code below was the culprit:
class JobCodesViewController: ViewController {
It should have been:
class JobCodesViewController: UIViewController {