diff --git a/StatefulViewController/StatefulViewController.swift b/StatefulViewController/StatefulViewController.swift index ea43248..95bf22a 100644 --- a/StatefulViewController/StatefulViewController.swift +++ b/StatefulViewController/StatefulViewController.swift @@ -12,7 +12,7 @@ public enum StatefulViewControllerState: String { public protocol BackingViewProvider { /// The backing view, usually a UIViewController's view. /// All placeholder views will be added to this view instance. - var backingView: UIView { get } + var backingView: UIView! { get } } /// StatefulViewController protocol may be adopted by a view controller or a view in order to transition to diff --git a/StatefulViewController/StatefulViewControllerImplementation.swift b/StatefulViewController/StatefulViewControllerImplementation.swift index 26b9b27..b24ddfa 100644 --- a/StatefulViewController/StatefulViewControllerImplementation.swift +++ b/StatefulViewController/StatefulViewControllerImplementation.swift @@ -4,13 +4,13 @@ import UIKit // MARK: Default Implementation BackingViewProvider extension BackingViewProvider where Self: UIViewController { - public var backingView: UIView { + public weak var backingView: UIView! { return view } } extension BackingViewProvider where Self: UIView { - public var backingView: UIView { + public weak var backingView: UIView! { return self } } diff --git a/StatefulViewController/ViewStateMachine.swift b/StatefulViewController/ViewStateMachine.swift index cd1dca9..6608805 100644 --- a/StatefulViewController/ViewStateMachine.swift +++ b/StatefulViewController/ViewStateMachine.swift @@ -52,8 +52,8 @@ public class ViewStateMachine { }() /// The view that should act as the superview for any added views - public let view: UIView - + public weak var view: UIView? + /// The current display state of views public fileprivate(set) var currentState: ViewStateMachineState = .none @@ -162,9 +162,14 @@ public class ViewStateMachine { // MARK: Private view updates fileprivate func showView(forKey state: String, animated: Bool, completion: (() -> ())? = nil) { + guard let superview = self.view else { + completion?() + return + } + // Add the container view - containerView.frame = view.bounds - view.addSubview(containerView) + containerView.frame = superview.bounds + superview.addSubview(containerView) let store = viewStore