Present a specific ViewController from the TabBarController - swift

I want to open a specific ViewController from the TabBarController whenever a local notification is fired and their custom action is performed. I have used following line of code:
func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: #escaping () -> Void) {
switch response.actionIdentifier {
case "first":
DispatchQueue.main.async(execute: {
self.first()
})
case "second":
DispatchQueue.main.async(execute: {
self.second()
})
default:
break
}
completionHandler()
}
So it's a first() function:
func first() {
let storyboard = UIStoryboard.init(name: "Main", bundle: nil)
let tabBarController = storyboard.instantiateViewController(withIdentifier: "Root") as! UITabBarController
let navigationController = storyboard.instantiateViewController(withIdentifier: "First") as! UINavigationController
tabBarController.present(navigationController, animated: true) {
}
self.window = UIWindow.init(frame: UIScreen.main.bounds)
self.window?.tintColor = UIColor(red: 0.0, green: 0.5, blue: 0.0, alpha: 1.0)
self.window?.rootViewController = tabBarController
self.window?.makeKeyAndVisible()
}
Second function: second()
func second() {
let storyboard = UIStoryboard.init(name: "Main", bundle: nil)
let tabBarController = storyboard.instantiateViewController(withIdentifier: "Root") as! UITabBarController
let navigationController = storyboard.instantiateViewController(withIdentifier: "Second") as! UINavigationController
tabBarController.present(navigationController, animated: true) {
}
self.window = UIWindow.init(frame: UIScreen.main.bounds)
self.window?.tintColor = UIColor(red: 0.0, green: 0.5, blue: 0.0, alpha: 1.0)
self.window?.rootViewController = tabBarController
self.window?.makeKeyAndVisible()
}
And it's works well, but I cannot open the second ViewController while the first one is presented and the second notification is fired: In the console: Warning attempt to present ViewController...

Use this:
tabBarController.selectedIndex = 1
Or this:
tabBarController.selectedViewController = tabBarController.viewControllers![1]
Where 1 can be any of the viewcontrollers presented by your tabBarController

I had run into a similar issue and changing the selectedIndex did not work for me. Depending on the requirements of your project you can instantiate your ViewController and add it as a Subview and move to that Subview. When youre done make sure to remove that Subview.
let replyView = self.storyboard?.instantiateViewControllerWithIdentifier("replyView")
self.addChildViewController(replyView!)
self.view.addSubview(replyView!.view)
replyView!.didMoveToParentViewController(self)

Related

How to open a specific view controller from App Delegate on didReceive Push Notification

I'm trying to open a specific view controller when notification is tapped. and i'm unable to do so any one who can help me with it?
I have attached the view controller image which i'm trying to open.
func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: #escaping () -> Void) {
//when notification is tapped
let userInfo = response.notification.request.content.userInfo
print("USERINFO : \(userInfo)")
let storyboard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
let apptVC = storyboard.instantiateViewController(withIdentifier: "VideoChatViewController") as! VideoChatViewController
let navigationController = UINavigationController.init(rootViewController: apptVC)
self.window?.rootViewController = navigationController
self.window?.makeKeyAndVisible()
}
After set rootViewController you need to set navigation for that
func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject]) {
println("didReceiveRemoteNotification")
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let destinationViewController = storyboard.instantiateViewControllerWithIdentifier("MessageViewController") as MessageViewController
let navigationController = self.window?.rootViewController as! UINavigationController
navigationController?.pushViewController(destinationViewController, animated: false, completion: nil)
}

How to set the PageViewController to cover the whole screen and not be modal?

I am implementing a UIPageViewController to my app to try to build a UI like Tinder, in which you can swipe left and right to not only like or dislike a person, but to navigate different screens, i.e. chat screen, profile screen, matches screen etc.
In my case, after signing in, a UIPageViewController that contains 4 other UIViewControllers will pop up.
However, the UIPageViewController is modal and doesn't cover the whole screen(as there is a small gap at the top which allows the user to swipe the modal down and away).
I tried using code like this:
self.window = UIWindow(frame: UIScreen.main.bounds)
if let window = self.window {
window.rootViewController = PageViewController()
window.makeKeyAndVisible()
}
at my AppDelegate, or setting Full Screen at the storyboard, but did't work.
I wonder how I should do this?
Or maybe UIPageViewController is not the right choice to achieve this swipe from screen to screen navigation style Tinder has?
Anyway, here is the code of my PageViewController:
import UIKit
class PageViewController: UIPageViewController, UIPageViewControllerDataSource, UIPageViewControllerDelegate {
var controllers = [UIViewController]()
override func viewDidLoad() {
super.viewDidLoad()
let vc = TodayPicksViewController()
controllers.append(vc)
let vc1 = TopPicksViewController()
vc1.view.backgroundColor = .yellow
controllers.append(vc1)
let vc2 = ChatViewController()
vc2.view.backgroundColor = .gray
controllers.append(vc2)
let vc3 = (storyboard?.instantiateViewController(withIdentifier: String(describing: ProfileViewController.self)) as? ProfileViewController)!
controllers.append(vc3)
}
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
DispatchQueue.main.asyncAfter(deadline: .now()+2, execute: {
self.presentPageVC()
})
}
func presentPageVC() {
guard let first = controllers.first else {
return
}
let vc = UIPageViewController(transitionStyle: .scroll,
navigationOrientation: .horizontal,
options: nil)
vc.delegate = self
vc.dataSource = self
vc.setViewControllers([first],
direction: .forward,
animated: true,
completion: nil)
present(vc, animated: true)
}
func pageViewController(_ pageViewController: UIPageViewController, viewControllerBefore viewController: UIViewController) -> UIViewController? {
guard let index = controllers.firstIndex(of: viewController), index > 0 else {
return nil
}
let before = index - 1
return controllers[before]
}
func pageViewController(_ pageViewController: UIPageViewController, viewControllerAfter viewController: UIViewController) -> UIViewController? {
guard let index = controllers.firstIndex(of: viewController), index < (controllers.count - 1) else {
return nil
}
let after = index + 1
return controllers[after]
}
}
By default when you present a ViewController in Swift it doesn't cover the fullscreen. To make it cover the fullscreen you need to set the modalPresentationStyle on the ViewController.
So in your presentPageVC method you need to add the following line :
vc.modalPresentationStyle = .fullScreen
So your method will now look like this:
func presentPageVC() {
guard let first = controllers.first else {
return
}
let vc = UIPageViewController(transitionStyle: .scroll,
navigationOrientation: .horizontal,
options: nil)
vc.delegate = self
vc.dataSource = self
vc.setViewControllers([first],
direction: .forward,
animated: true,
completion: nil)
vc.modalPresentationStyle = .fullScreen // <- add this before presenting your ViewController
present(vc, animated: true)
}
To read more about the different presentation styles that you can have check out the documentation here.

Different view controller display at start app

I try to explain situation:
I have two view controllers: viewHome and viewStartTest.
When student start app first time, don't have any data about his test in table.
In this situation should be display viewStartTest controller after launch screen.
But when he start app again and condition "test is finished" is true, viewHome controller should be display at start.
I try to put this code in AppDelegate.swift and simulate finished test but still not working, thanks for help:
// 0 - false, 1 - True
var conditionTest = 1
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
if conditionTest == 1 {
self.window = UIWindow(frame: UIScreen.main.bounds)
let mainStoryboard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
let viewStartTest: UIViewController = mainStoryboard.instantiateViewController(withIdentifier: "viewStartTest")
self.window?.rootViewController = viewStartTest
self.window?.makeKeyAndVisible()
} else {
self.window = UIWindow(frame: UIScreen.main.bounds)
let mainStoryboard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
let ViewHome: UIViewController = mainStoryboard.instantiateViewController(withIdentifier: "viewHome")
self.window?.rootViewController = ViewHome
self.window?.makeKeyAndVisible()
}
}
Correct Scene delegate code after discussion below:
func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
// Use this method to optionally configure and attach the UIWindow `window` to the provided UIWindowScene `scene`.
// If using a storyboard, the `window` property will automatically be initialized and attached to the scene.
// This delegate does not imply the connecting scene or session are new (see `application:configurationForConnectingSceneSession` instead).
if conditionTest == 1 {
let mainStoryboard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
let viewStartTest: UIViewController = mainStoryboard.instantiateViewController(withIdentifier: "viewStartTest")
self.window?.rootViewController = viewStartTest
self.window?.makeKeyAndVisible()
} else {
let mainStoryboard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
let ViewHome: UIViewController = mainStoryboard.instantiateViewController(withIdentifier: "viewHome")
self.window?.rootViewController = ViewHome
self.window?.makeKeyAndVisible()
}
}
guard let _ = (scene as? UIWindowScene) else { return }
}
You need to store the value of conditionTest somewhere. I would suggest using UserDefaults . This is an example of how you could implement it:
NavigationController:
class MainNavigationControllerViewController: UINavigationController {
override func viewDidLoad() {
super.viewDidLoad()
if isLoggedIn() {
let homeController = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "HomeVC")
viewControllers = [homeController]
}
}
fileprivate func isLoggedIn() -> Bool {
return UserDefaults.standard.isLoggedIn()
}
}
Extension for UserDefaults:
extension UserDefaults {
func setIsLoggedIn(value: Bool) {
set(value, forKey: "isLoggedIn")
synchronize()
}
func isLoggedIn() -> Bool {
return bool(forKey: "isLoggedIn") }
}
How to use:
with the above code you can simply login/logout the user. Make sure to call .synchronize()
Login:
UserDefaults.standard.setIsLoggedIn(value: true)
UserDefaults.standard.synchronize()
Logout:
UserDefaults.standard.setIsLoggedIn(value: false)
UserDefaults.standard.synchronize()

not getting navigationController?.pushViewController to work but present does?

I do have a non-Storyboard, 100% coded UIViewController, UICollectionView and UICollectionViewCell - works perfect.
here's the code in question:
SceneDelegate
not sure if this is relevant, tho.
class SceneDelegate: UIResponder, UIWindowSceneDelegate {
var window: UIWindow?
func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
guard let windowScene = (scene as? UIWindowScene) else { return }
window = UIWindow(windowScene: windowScene)
let layout = UICollectionViewFlowLayout()
layout.scrollDirection = .horizontal
let myController = MyViewController(collectionViewLayout: layout)
window?.rootViewController = myController
window?.makeKeyAndVisible()
}
.
.
ViewController
very simple and straight forward...
class MyViewController: UICollectionViewController, UICollectionViewDelegateFlowLayout {
let data = loadOnboardingData()
.
.
override func viewDidLoad() {
super.viewDidLoad()
collectionView?.backgroundColor = .white
collectionView?.register(MyPageCell.self, forCellWithReuseIdentifier: "cellId")
collectionView?.isPagingEnabled = true
collectionView.showsHorizontalScrollIndicator = false
collectionView?.tag = myPageControl.currentPage
setupMyPageControl()
}
ViewControllerExtention
here's the problem: the pushViewController method just doesn't do anything but the modal present works like a charm and I'm not getting what's wrong and why:
extension MyViewController: MyPageCellDelegate {
.
.
func didTabOnActionButton(title: String) {
let storyboard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
guard let homeViewController = storyboard.instantiateViewController(withIdentifier: "HomeViewController") as? HomeViewController else {
print("Coun't find controller")
return
}
navigationController?.pushViewController(homeViewController, animated: true) <- NO EFFECT
//present(homeViewController, animated: true, completion: nil) <- WORKS PERFECT!
}
MyPageCell
I set up the Delegate via protocol and it seems that's fine too
protocol MyPageCellDelegate {
func didTabOnActionButton(title: String)
}
class MyPageCell: UICollectionViewCell {
var delegate: MyPageCellDelegate?
let myActionButton: UIButton = {
let button = UIButton(type: .system)
return button
}()
myActionButton.addTarget(self, action: #selector(self.doAction), for: .touchUpInside)
.
.
#objc private func doAction(_ sende: Any) {
delegate?.didTabEndOnboardingActionButton(title: "end Onboarding")
}
so, any Idea what's wrong with:
navigationController?.pushViewController(homeViewController, animated: true)
EDIT --------------------------------------------------------------------
As pointed out by #Michcio this here: window?.rootViewController = UINavigationController(rootViewController: myController) works half way and as far as I understand it, I'm embedding myController into an UINavigationController which adds the Navigation Bar to the current and following controllers.
But that's not what I need!
What I need is a clean and simple one for the onboarding i.e. MyViewController and the HomeViewController should be one with a Tab- and Navigation Bar
Basically starting from scratch after onboarding.
I used to solve this in the previous version editing the AppDelegate first Method like this (in this example I used Storyboards):
extension AppDelegate {
func showOnboarding() {
if let window = UIApplication.shared.keyWindow, let onboardingViewController = UIStoryboard(name: "Onboarding", bundle: nil).instantiateInitialViewController() as? OnboardingViewController {
onboardingViewController.delegate = self
window.rootViewController = onboardingViewController
}
}
func hideOnboarding() {
if let window = UIApplication.shared.keyWindow, let mainViewController = UIStoryboard(name: "Main", bundle: nil).instantiateInitialViewController() {
mainViewController.view.frame = window.bounds
UIView.transition(with: window, duration: 0.5, options: .transitionCrossDissolve, animations: {
window.rootViewController = mainViewController
}, completion: nil)
}
}
}
and in the Delegate itself like this:
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
let isFirstRun = true // logic to determine goes here
if isFirstRun {
showOnboarding()
}
return true
}
but I'm seriously not getting the new SceneDelegate or simply don't understand it
Really would appreciate if someone could past some code here for re-use.
It didn't work, because you are set MyViewController as window.rootViewController. Just change line in SceneDelegate to:
window?.rootViewController = UINavigationController(rootViewController: myController)

slideMenuController not working

i am using slideMenuController from github for left drawer effect.
i had implemented successfully.
but i am having one issue that from the menu i am navigate to viewController2 and viewController2 have one uibutton which push viewcontroller1.
now i am selecting viewcontroller2 from slideMenuController but it navigate to viewcontroller1 instead of viewcontroller2.
i am having navigation in appdelegate
let storyboard = UIStoryboard(name: "Main", bundle: nil)
var mainViewController : MainViewController = storyboard.instantiateViewControllerWithIdentifier("MainViewController") as MainViewController
let leftViewController = storyboard.instantiateViewControllerWithIdentifier("LeftViewController") as LeftViewController
let rightViewController = storyboard.instantiateViewControllerWithIdentifier("RightViewController") as RightViewController
nvc = UINavigationController(rootViewController: mainViewController)
leftViewController.mainViewController = nvc
let slideMenuController = SlideMenuController(mainViewController:nvc!, leftMenuViewController: leftViewController, rightMenuViewController: rightViewController)
println("\(nvc!.viewControllers.count)")
self.window?.backgroundColor = UIColor(red: 236.0, green: 238.0, blue: 241.0, alpha: 1.0)
self.window?.rootViewController = slideMenuController
self.window?.makeKeyAndVisible()<br>
and navigation from slideMenus this kind of navigation
func changeViewController(menu: LeftMenu) {
switch menu {
case .Main:
self.slideMenuController()?.changeMainViewController(self.mainViewController, close: true)
case .Swift:
self.slideMenuController()?.changeMainViewController(self.swiftViewController, close: true)
break
case .Java:
self.slideMenuController()?.changeMainViewController(self.javaViewController, close: true)
break
case .Go:
self.slideMenuController()?.changeMainViewController(self.goViewController, close: true)
break
case .NonMenu:
self.slideMenuController()?.changeMainViewController(self.nonMenuViewController, close: true)
break
default:
break
}
}
in slideMenuController the function
public func changeMainViewController(mainViewController: UIViewController, close: Bool) {
removeViewController(self.mainViewController)
self.mainViewController = mainViewController
setUpViewController(mainContainerView, targetViewController: mainViewController)
if (close) {
closeLeft()
closeRight()
}
}
i am trying to remove also navigation stack of appdelegate and assign it again while changing the menu. but it didn't work.how can i achieve. please help me out.
Error is in your JavaViewController , correct it like this :-
class JavaViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
var btn : UIButton = UIButton(frame: CGRectMake(150, 150, 60, 60))
btn.setTitle("Click", forState: UIControlState.Normal)
btn.addTarget(self, action: "btnClick:", forControlEvents: UIControlEvents.TouchUpInside)
view.addSubview(btn)
}
override func viewWillAppear(animated: Bool) {
super.viewWillAppear(animated)
self.setNavigationBarItem()
}
func btnClick(sender: UIButton) {
var storyboard = UIStoryboard(name: "Main", bundle: nil)
let mainViewController = storyboard.instantiateViewControllerWithIdentifier("MainViewController") as! MainViewController
let leftViewController = storyboard.instantiateViewControllerWithIdentifier("LeftViewController") as! LeftViewController
let nvc: UINavigationController = UINavigationController(rootViewController: mainViewController)
leftViewController.mainViewController = nvc
self.slideMenuController()?.changeMainViewController(nvc, close: true)
}
}