I have a quick question regarding instantiating a particular view controller from my AppDelegate. I am building a simple chat app that needs to respond when a user opens the app via a remote notification. The desired behavior of my app is that when the user taps this notification the application will open to that particular chat screen.
The basic flow of the app is as follows:
The highest level UI component is a UITabBarController
Inside this tab bar is a navigation controller
Inside the nav controller is a view controller (let's call it the "list") which lists all the available chat windows
Once a user taps on one of the rows of my tableview that lists the chats a new view controller is pushed. This view controller is the chat window which automatically hides the tab bar but keeps the back button of the nav controller
When the user taps the back button it will return to the chat list and the tab bar will reappear
I am using the following code to get the correct chat screen to appear. I can tap the back button and it will return to the chat list as expected. The only problem is that the navigation controller isn't embedded in the tab controller. This code is being called from func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler:
let mainStoryboard : UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
let initialVC : UIViewController = mainStoryboard.instantiateViewController(withIdentifier: "ChatVC") as UIViewController
let detailVC : UIViewController = mainStoryboard.instantiateViewController(withIdentifier: "ChatDetailVC") as UIViewController
let navContr = UINavigationController(rootViewController:initialVC)
self.window = UIWindow(frame: UIScreen.main.bounds)
self.window?.rootViewController = navContr
self.window?.makeKeyAndVisible()
initialVC.navigationController?.pushViewController(detailVC, animated: true)
How can I configure my code so that the tab controller is the top level of the stack? It's labelled "TabController" in my storyboard.
You just need to create a flag which determine if user comes from notification tap or normally launch
struct NotificationEvent {
static var isFromNotification = false
}
The stack of controller is -
TabBarController->UINavigationController->ChatListViewController->ChatDetailViewController
In AppDelegate func userNotificationCenter -
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let vc = storyboard.instantiateViewController(withIdentifier: "TabViewController") as! TabViewController//Your Tabbarcontroller
NotificationEvent.isFromNotification = true//recognize is from notification
window?.rootViewController = vc
ChatListViewController
class ChatListViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
if NotificationEvent.isFromNotification {
openChatDetailScreen()//Jumps to that chat screen
NotificationEvent.isFromNotification = false
}
}
#IBAction func tapsOnChatList(_ sender: UIButton) {//consider it as didselect method in your tableView
openChatDetailScreen()
}
func openChatDetailScreen() {
let vc = self.storyboard?.instantiateViewController(withIdentifier: "ChatDetailViewController") as! ChatDetailViewController
navigationController?.pushViewController(vc, animated: true)
}
}
Or instead of NotificationEvent.isFromNotification you can also check with UIApplicationLaunchOptionsKey.remoteNotification/observer of notification event in particular viewcontroller.
Related
When the function is done executing I want to go back to my ProfileTabViewController. I want to get sent back to the view controller keeping the ProfileTabViewControllers same navigation bar and UITabBarController from before. The way I have it now it gets sent to the ProfileTavViewController but I lose my navigation bar as well as the tab bar. How do I just send it back to the original ProfileTabViewController. The first image is the original ViewController and the second image is when it gets sent back.
#IBAction func updateAction(_ sender: Any) {
let newInterests = options.joined(separator: " , ")
if newInterests == ""{
showAlert(message: "Please select at least one interest.")
}else{
updateFunction()
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let vc = storyboard.instantiateViewController(withIdentifier: "ProfileTabViewController") as! UIViewController
self.navigationController?.present(vc, animated: true, completion: nil)
}
}
While Presenting a ViewController it will not show navigationbar beacause presentation depends on UIViewController not NavigationViewcontroller.
You have to use popViewController method to get navigation bar.
for controller in self.navigationController?.viewControllers {
if controller is ProfileViewController {
self.navigationController!.popToViewController(controller, animated: true)
break
}
}
//Use Pop
First of all, let me introduce what I'm trying to do. Second... I'm new to Swift/iOS development.
What I'm trying to do is:
When I open the APP, it loads the LoginVC. When the user logs in, the TableSelectionVC loads. If the user selects a cell, it goes to the Home screen with the selected values. If the user taps on the bell (blue arrows) the app should go to AlarmVC.
It should work, but it doesn't. The AlarmVC says that self.revealViewController() is nil. BUT If I go to alarmVC through the menu option (red arrows) it loads normally and the Menu is shown. Also, if I choose the option in green, it goes to the TableSelectionVC and if I tap the icon, it goes to alarmVC and it doesn't crash. The problem is if I go from LoginVC to TableSelectionVC and tap on the icon, the it will crash.
I think it is the way that I'm setting the views, instantiating a controller and setting the window.rootViewController.
In my AppDelegate I have the following functions:
func changeRootViewControllerToSWRevealViewController () {
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let controller = storyboard.instantiateViewController(withIdentifier: "SWRevealViewController")
controller.modalTransitionStyle = .flipHorizontal
if let window = self.window {
window.rootViewController = controller
}
}
func changeRootViewControllerToPlantSelectionVC () {
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let controller = storyboard.instantiateViewController(withIdentifier: "navPlantSelectionVC")
controller.modalTransitionStyle = .flipHorizontal
if let window = self.window {
window.rootViewController = controller
}
}
When the user logs in the app, the following function is executed:
static func goToPlantSelection() {
guard let appDelegate = UIApplication.shared.delegate as? AppDelegate else {
//there should be an alert error
return
}
appDelegate.changeRootViewControllerToPlantSelectionVC()
}
After that, the PlantSelectionVC is shown and if the user taps on an cell the appDelegate.changeRootViewControllerToSWRevealViewController() is executed and the HomeVC is shown.
If the user taps on the icon, it tries to go to the AlarmVC but it crashes, like I said. I think I'm doing something wrong with binding the SWRevealViewController with LoginVC and TableSelectionVC.
The following code in AlarmVC tries to execute:
static func setupMenuToggle(button: UIBarButtonItem, viewController: UIViewController) {
button.target = viewController.revealViewController()
viewController.revealViewController().rearViewRevealWidth = viewController.view.bounds.size.width * 0.9
button.action = #selector(SWRevealViewController.revealToggle(_:))
}
But is shows the error in the third line: found nil while unwrapping an Optional value
Can anyone help ?
I fixed it. I set the login screen as sw_front for the swRevealViewController and after that, when the user logs in I would change the swRevealViewController front view controller with
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let controller = storyboard.instantiateViewController(withIdentifier: "navPlantSelectionVC")
self.revealViewController().setFront(controller, animated: true)
It is working now.
I have multiple tabs connected via separate navigation controllers. I want to to instantiate same End Pt VC, irrespective of which tab user chooses tabs 1 through 4.
When I select Tab 1 it shows End Pt VC because it is connected via Segue. However when I select other tabs I manually try to push EndPt VC as shown in the tab controller method. But it shows blank screen. How can I present the same End Pt VC irrespective of tab selection?
override func tabBar(_ tabBar: UITabBar, didSelect item: UITabBarItem) {
if let viewController = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "EndPointTVC") as? EndPtListTableViewController{
viewController.selectedTab = self.selectedTab
if let navigator = self.navigationController {
navigator.pushViewController(viewController, animated: true)
}
}
}
You can detect when the UITabBarController changes tab via its delegate UITabBarControllerDelegate. In the following delegate method, you can cast the selected view controller to a UINavigationController (which is what I can make out in the screenshot you attached).
extension MyTabBarController: UITabBarControllerDelegate {
func tabBarController(_ tabBarController: UITabBarController, didSelect viewController: UIViewController) {
let myVC = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "MyViewControllerStoryID") as! MyViewController
let navController = viewController as! UINavigationController
navController.viewControllers = [myVC]
print(viewController)
}
}
Now, no matter which tab you choose, a new MyViewController instance is created. Even hitting the same tab multiple times will replace the current instance for a new MyViewController instance.
Try to instantiate the viewcontroller from the storyboard and then present it. Also, make sure that you have given the target viewcontroller the storyboard Id "EndPointTVC" in the Identify section.
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let viewController = storyboard.instantiateViewController(withIdentifier : "EndPointTVC")
viewController.selectedTab = self.selectedTab
self.present(viewController, animated: true)
I want to create an app that lets the user click on a button on V1 on the navigation bar. This will segue to V2 where they can press another button on the navigation bar on V2 and it will bring them back to V1 which has the tab bar controller at the bottom. I don't want the tab bar on V2 and I don't want V2 as a tab bar item. When I try this the tab bar disappears on V1 when I segue back to V1 from V2.
TAB BAR CONTROLLER -> TAB BAR ITEM (V1) -> V2(Via navigation bar button item on V1) -> back to V1(Via navigation bar button item on V2)
I have added [self.navigationController, popViewControllerAnimated:YES]; to my button function but it comes up with an error - expected expression in container literal.
Apart from this code I have not got any other code in my app yet.
I am using Xcode 8.0 and Swift 3.0
First create subclass of UITabBarController, Then add properties to AppDelegate
var navController: UINavigationController?
var tabController: MyTabController?
If you want to show tab bar controller on app launch then put these code in AppDelegate in didFinishLaunchingWithOptions
self.window = UIWindow(frame: UIScreen.main.bounds)
let myStoryboard = UIStoryboard(name: "Main", bundle: nil) as UIStoryboard
self.tabController = myStoryboard.instantiateViewController(withIdentifier: "MyTabController") as? MyTabController
//self.navController = UINavigationController(rootViewController: self.tabController!)
//self.window?.rootViewController = self.navController
self.window?.rootViewController = self.tabController
self.window?.makeKeyAndVisible()
return true
If you want to jump on tab bar after login or something else then , add property to that controller
var appDelegate: AppDelegate!
in viewDidLoad
appDelegate = UIApplication.shared.delegate as? AppDelegate
And method should like
func logIntoApp() {
appDelegate.tabController = self.storyboard?.instantiateViewController(withIdentifier: "MyTabController") as? MyTabController
appDelegate.window?.rootViewController = appDelegate.tabController
}
Then in your tab item view controller , create property of AppDelegate and assign delegate as above.
And methods should be like :
#IBAction func showWithTab(_sender: AnyObject) {
let DefaultVC = self.storyboard?.instantiateViewController(withIdentifier: "DefaultViewController") as! DefaultViewController
self.navigationController?.pushViewController(DefaultVC, animated: true)
}
#IBAction func showWithoutTab(_sender: AnyObject) {
let DefaultVC = self.storyboard?.instantiateViewController(withIdentifier: "DefaultViewController") as! DefaultViewController
// You can create your own animation
UIView.transition(from: (appDelegate.tabController?.view)!, to: (appDelegate.navController?.view)!, duration: 0.3, options: UIViewAnimationOptions.curveEaseIn) { (finished) in
self.appDelegate.window?.rootViewController = self.appDelegate.navController
}
// OR you can use like this way
UIView.transition(from: self.view, to: DefaultVC.view, duration: 0.3, options: UIViewAnimationOptions.curveEaseIn) { (finished) in
self.appDelegate.window?.rootViewController = self.appDelegate.navController
}
}
Why not use a simple Tab Bar?
Something like this:
and in VC1 :
override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
myTab.removeFromSuperview()
}
I have solved my problem after days of trying to figure out an answer. You need to add this to any view controller.
#IBAction func unwindToViewController (sender: UIStoryboardSegue){
}
Then you can add a segue from bar button item to the exit icon on the view controller.
You can view an image of the VC scene here.
I'm trying to want to present/push a VC embedded in a NavController from AppDelegate. I previously used this code but somehow it's not working anymore:
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let VC = storyboard.instantiateViewControllerWithIdentifier("PendingRequest") as! PendingRequestVC
let navController = UINavigationController.self(rootViewController: VC)
let rootViewController = UIApplication.sharedApplication().keyWindow!.rootViewController
rootViewController!.presentViewController(navController, animated: false, completion: nil)
Other codes open my desired VC but not within a navigation pane. Any guidance would be appreciated.
Calling from AppDelegate after user interacts with push notification.
Edit:
I'm able to present the right VC by using this code:
let mainStoryboard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
if let viewController = mainStoryboard.instantiateViewControllerWithIdentifier("PendingNavController") as? UINavigationController {
if let yourViewController = viewController.topViewController as? PendingRequestVC {
//yourViewController.getRequestdata()
}
UIApplication.sharedApplication().keyWindow!.rootViewController = viewController;
}
But this code won't allow me to go back using the Close button on my NavBar.
My structure is as follow:
TabController -> NavController1 -> VC1 -> NavController1a -> VC1a
I'm trying to get to VC1a and be able to use the Closed button to go back to VC1
Add a UIButton in your presented ViewController's View. Following event will be performed by that button. You can dismiss your Navigation Controller this way
#IBAction func dismissViewController(sender: AnyObject) {
self.navigationController.dismissViewControllerAnimated(false, completion:nil);
}