From 59460254bfb6b9474d401178b10ac046a8b60ccf Mon Sep 17 00:00:00 2001 From: Sai Teja Jammalamadaka Date: Mon, 27 May 2019 17:49:51 +0530 Subject: [PATCH] Navigation added with Bar Buttons between screens --- .../Navigator/AppNavigator.swift | 12 ++++--- .../View/HomeViewController.swift | 34 ++++++++++++++++++- .../View/LoginViewController.swift | 14 +++++++- .../View/NewsViewController.swift | 28 +++++++++++---- .../View/ProfileViewController.swift | 2 +- 5 files changed, 76 insertions(+), 14 deletions(-) diff --git a/AppNavigatorExample/Navigator/AppNavigator.swift b/AppNavigatorExample/Navigator/AppNavigator.swift index a10e14c..9d1ac54 100644 --- a/AppNavigatorExample/Navigator/AppNavigator.swift +++ b/AppNavigatorExample/Navigator/AppNavigator.swift @@ -8,14 +8,14 @@ import UIKit -fileprivate protocol NavigatorDestination { +protocol NavigatorDestination { associatedtype PresentationStyle static var `default`: Self { get } var presentationStyle: PresentationStyle { get } var viewController: UIViewController { get } } -fileprivate protocol Navigator { +protocol Navigator { associatedtype Destination: NavigatorDestination associatedtype PresentationStyle func navigate(to destination: Destination, @@ -114,7 +114,7 @@ class AppNavigator: Navigator { func navigate(basedOn url: URL, with presentationStyle: PresentationStyle? = nil, animated: Bool = true) -> UIViewController? { - guard url.scheme == "AppNavigatorExample", + guard url.scheme?.lowercased() == "AppNavigatorExample".lowercased(), let host = url.host, let destination = Destination(rawValue: host) else { return nil @@ -130,7 +130,11 @@ class AppNavigator: Navigator { static func navigate(basedOn url: URL, with appDelegate: UIApplicationDelegate) { if let navigationController = appDelegate.window??.rootViewController as? UINavigationController { let appNavigator = AppNavigator(with: navigationController) - appNavigator.navigate(basedOn: url) + let viewController = appNavigator.navigate(basedOn: url) + + if let newsViewController = viewController as? NewsViewController { + newsViewController.newsLabelText = "News - Opened from URL" + } } } } diff --git a/AppNavigatorExample/View/HomeViewController.swift b/AppNavigatorExample/View/HomeViewController.swift index 9cfc686..d56af70 100644 --- a/AppNavigatorExample/View/HomeViewController.swift +++ b/AppNavigatorExample/View/HomeViewController.swift @@ -10,9 +10,15 @@ import UIKit class HomeViewController: UIViewController { + var appNavigator = AppNavigator() + override func viewDidLoad() { super.viewDidLoad() - // Do any additional setup after loading the view. + + appNavigator.navigationController = navigationController + + addProfileButton() + addNewsButton() addLabel() } @@ -30,5 +36,31 @@ class HomeViewController: UIViewController { ]) } + private func addProfileButton() { + let profileButton = UIBarButtonItem(title: "Profile", + style: UIBarButtonItem.Style.plain, + target: self, + action: #selector(openProfile)) + navigationItem.rightBarButtonItem = profileButton + } + + private func addNewsButton() { + let newsButton = UIBarButtonItem(title: "News", + style: UIBarButtonItem.Style.plain, + target: self, + action: #selector(openNews)) + navigationItem.leftBarButtonItem = newsButton + } + + @objc private func openProfile() { + appNavigator.navigate(to: .profile) + } + + @objc private func openNews() { + if let newsViewController = appNavigator.navigate(to: .news) as? NewsViewController { + newsViewController.newsLabelText = "News - Opened from Home" + } + } + } diff --git a/AppNavigatorExample/View/LoginViewController.swift b/AppNavigatorExample/View/LoginViewController.swift index 146ecc3..235cec3 100644 --- a/AppNavigatorExample/View/LoginViewController.swift +++ b/AppNavigatorExample/View/LoginViewController.swift @@ -12,7 +12,8 @@ class LoginViewController: UIViewController { override func viewDidLoad() { super.viewDidLoad() - // Do any additional setup after loading the view. + + addHomeButton() addLabel() } @@ -30,6 +31,17 @@ class LoginViewController: UIViewController { ]) } + private func addHomeButton() { + let homeButton = UIBarButtonItem(title: "Home", + style: UIBarButtonItem.Style.plain, + target: self, + action: #selector(openHome)) + navigationItem.rightBarButtonItem = homeButton + } + + @objc private func openHome() { + AppNavigator(with: navigationController).navigate(to: .home) + } } diff --git a/AppNavigatorExample/View/NewsViewController.swift b/AppNavigatorExample/View/NewsViewController.swift index 6def603..fdd51f8 100644 --- a/AppNavigatorExample/View/NewsViewController.swift +++ b/AppNavigatorExample/View/NewsViewController.swift @@ -10,24 +10,38 @@ import UIKit class NewsViewController: UIViewController { + var newsLabelText: String = "News" + override func viewDidLoad() { super.viewDidLoad() - // Do any additional setup after loading the view. + addLabel() + addCloseButton() } private func addLabel() { - let label = UILabel() - label.text = "News" + let newsLabel = UILabel() + newsLabel.text = newsLabelText - view.addSubview(label) + view.addSubview(newsLabel) view.backgroundColor = .white - label.translatesAutoresizingMaskIntoConstraints = false + newsLabel.translatesAutoresizingMaskIntoConstraints = false NSLayoutConstraint.activate([ - label.centerXAnchor.constraint(equalTo: view.centerXAnchor), - label.centerYAnchor.constraint(equalTo: view.centerYAnchor) + newsLabel.centerXAnchor.constraint(equalTo: view.centerXAnchor), + newsLabel.centerYAnchor.constraint(equalTo: view.centerYAnchor) ]) } + private func addCloseButton() { + let closeButton = UIBarButtonItem(title: "Close", + style: UIBarButtonItem.Style.plain, + target: self, + action: #selector(closeViewController)) + navigationItem.leftBarButtonItem = closeButton + } + + @objc private func closeViewController() { + dismiss(animated: true, completion: nil) + } } diff --git a/AppNavigatorExample/View/ProfileViewController.swift b/AppNavigatorExample/View/ProfileViewController.swift index 8f48fa6..8f65231 100644 --- a/AppNavigatorExample/View/ProfileViewController.swift +++ b/AppNavigatorExample/View/ProfileViewController.swift @@ -12,7 +12,7 @@ class ProfileViewController: UIViewController { override func viewDidLoad() { super.viewDidLoad() - // Do any additional setup after loading the view. + addLabel() }