Skip to content

Instantly share code, notes, and snippets.

@acrookston
Created October 25, 2016 19:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save acrookston/4fb486830ddcb42236d0a480d3e6b710 to your computer and use it in GitHub Desktop.
Save acrookston/4fb486830ddcb42236d0a480d3e6b710 to your computer and use it in GitHub Desktop.
wip
protocol NavButtonProtocol: RawRepresentable {
var title : String { get }
}
//extension NavButtonProtocol where Self : RawRepresentable, Self.RawValue == String {
// static var count: Int {
// var max: Int = 0
// while let _ = self.init(rawValue: max) { max += 1 }
// return max
// }
//}
//extension NavButtonProtocol {
// func performSegueWithIdentifier(_ segueIdentifier: SegueIdentifier, sender: Any?) {
// performSegue(withIdentifier: segueIdentifier.rawValue, sender: sender)
// }
//
// func segueIdentifierForSegue(_ segue: UIStoryboardSegue) -> SegueIdentifier {
//
// guard let identifier = segue.identifier, let segueIdentifier = SegueIdentifier(rawValue: identifier) else {
// fatalError("Invalid segue identifier \(segue.identifier).")
// }
//
// return segueIdentifier
// }
//}
protocol NavBarButtonDelegate: class {
func navBarButtonTapped(button: NavButtonProtocol)
}
class NavBarButton : UIButton {
var type: NavBarButton.RawValue?
}
class NavBarView : UIView {
weak var delegate : NavBarButtonDelegate?
func create(buttons: [NavButtonProtocol]) {
// this is where you setup the buttons and add them to the view
for button in buttons {
let b = NavBarButton(frame: CGRect.zero)
b.setTitle(button.title, for: UIControlState.normal)
b.addTarget(self, action: #selector(buttonTapped), for: UIControlEvents.touchUpInside)
}
}
@objc func buttonTapped(sender: Any) {
guard let button = sender as? NavBarButton else {
return
}
guard let type = button.type else {
return
}
if let value = NavButtonProtocol(rawValue: type) {
delegate?.navBarButtonTapped(button: value)
}
// delegate?.navBarButtonTapped(button: type)
}
}
class SomeController : UIViewController, NavBarButtonDelegate {
enum NavButtons : NavButtonProtocol {
case first, second, third
var title : String {
switch self {
case .first:
return "First"
case .second:
return "Second"
case .third:
return "third"
}
}
}
override func viewDidLoad() {
super.viewDidLoad()
let n = NavBarView()
n.create(buttons: [NavButtons.first, NavButtons.second, NavButtons.third])
n.delegate = self
}
// MARK: - NavBarButtonDelegate
func navBarButtonTapped(button: NavButtonProtocol) {
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment