Skip to content

Commit

Permalink
Navigation added with Bar Buttons between screens
Browse files Browse the repository at this point in the history
  • Loading branch information
tsjamm committed May 27, 2019
1 parent 31c7854 commit 5946025
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 14 deletions.
12 changes: 8 additions & 4 deletions AppNavigatorExample/Navigator/AppNavigator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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
Expand All @@ -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"
}
}
}
}
34 changes: 33 additions & 1 deletion AppNavigatorExample/View/HomeViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}

Expand All @@ -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"
}
}

}

14 changes: 13 additions & 1 deletion AppNavigatorExample/View/LoginViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ class LoginViewController: UIViewController {

override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.

addHomeButton()
addLabel()
}

Expand All @@ -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)
}
}


28 changes: 21 additions & 7 deletions AppNavigatorExample/View/NewsViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
2 changes: 1 addition & 1 deletion AppNavigatorExample/View/ProfileViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class ProfileViewController: UIViewController {

override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.

addLabel()
}

Expand Down

0 comments on commit 5946025

Please sign in to comment.