RxSwift modelSelected Drive model on model View & Get that model on DetailView - swift

This is my FirstView ( Parent VIew)
tableView.rx.modelSelected(Kinder.self)
.asDriver()
.drive(self.detailKinderViewModel.currentKinder)
.disposed(by: disposeBag)
This is ViewModel ( BehaviorRelay )
lazy var currentKinder = BehaviorRelay<Kinder>(value: Kinder())
This is My SecondView ( Child View )
override func viewDidLoad() {
super.viewDidLoad()
detailKinderViewModel.currentKinder
.asDriver(onErrorJustReturn:Kinder())
.map{$0.kinder_name}
.drive(self.navigationItem.rx.title)
.disposed(by: disposeBag)
}
I can get Only Default Model Data.
I want to Get current Data on Child View

There isn't enough code to figure out what your problem might be. Here is how I would do it using my Cause_Logic_Effect library.
import Cause_Logic_Effect
import RxCocoa
import RxSwift
import UIKit
extension FirstView {
func connect() {
Observable.just([
Kinder(name: "Ben"),
Kinder(name: "Mia"),
Kinder(name: "Leon"),
Kinder(name: "Emma")
])
.bind(to: tableView.rx.items(cellIdentifier: "Cell")) { _, kinder, cell in
if #available(iOS 14.0, *) {
var configuration = cell.defaultContentConfiguration()
configuration.text = kinder.name
cell.contentConfiguration = configuration
}
else {
cell.textLabel?.text = kinder.name
}
}
.disposed(by: disposeBag)
// when the user selects a cell, load its Kinder into a newly created
// SecondView and push it onto the navigation stack
tableView.rx.modelSelected(Kinder.self)
.bind(onNext: pushScene(on: navigationController!, animated: true) { kinder in
SecondView().scene { $0.connect(kinder: kinder) }
})
.disposed(by: disposeBag)
}
}
extension SecondView {
func connect(kinder: Kinder) -> Observable<Never> {
// use the Kinder
title = kinder.name
// this controller doesn't send any info back to its parent.
return .never()
}
}
// The view controllers.
final class FirstView: UIViewController {
var tableView: UITableView!
let disposeBag = DisposeBag()
override func loadView() {
super.loadView()
title = "Main"
tableView = UITableView(frame: view.bounds)
tableView.autoresizingMask = [.flexibleWidth, .flexibleHeight]
tableView.register(UITableViewCell.self, forCellReuseIdentifier: "Cell")
view.addSubview(tableView)
}
}
final class SecondView: UIViewController {
override func loadView() {
super.loadView()
view.backgroundColor = .white
}
}
// The models
struct Kinder {
let name: String
}
#main
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
window = UIWindow(frame: UIScreen.main.bounds)
// Create a FirstView, configure it with a connector, wrap it in a
// navigation controller and make that the root.
window?.rootViewController = UINavigationController(rootViewController: FirstView().configure { $0.connect() })
window?.makeKeyAndVisible()
return true
}
}

Related

Can't restore state from code created ViewController

I'm trying to restore ViewController created from code. I'm trying to make very simple example but I don't understand why it doesn't work. An application changes background color of view by clicking on image and should save it when app is stopped. I'm trying to store and restore Bool property, and basing on it app changes views background color. When I'm testing it I'm sending app to background by cmd + shift + h, after that stop app from xcode and open it again, but it doesn't save selected background color.
I've enabled restoration option in AppDelegate
In ViewController I've set restorationIdentifier and restorationClass
Adopted protocol UIViewControllerRestoration in extension to ViewController and implemented encoding and decoding methods
Implemented ViewController restoring method
AppDelegate class
#UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
window = UIWindow(frame: UIScreen.main.bounds)
window?.rootViewController = ViewController()
window?.makeKeyAndVisible()
return true
}
func application(_ application: UIApplication, shouldSaveApplicationState coder: NSCoder) -> Bool {
return true
}
func application(_ application: UIApplication, shouldRestoreApplicationState coder: NSCoder) -> Bool {
return true
}
}
ViewController class
class ViewController: UIViewController {
var sunny: Bool = true {
didSet {
setColor()
}
}
lazy var colorView: UIView = {
let view = UIView()
view.translatesAutoresizingMaskIntoConstraints = false
return view
}()
lazy var changeColor: UIButton = {
let button = UIButton(type: .system)
button.setTitle("Change", for: .normal)
button.addTarget(self, action: #selector(change), for: .touchUpInside)
button.translatesAutoresizingMaskIntoConstraints = false
return button
}()
override func viewDidLoad() {
super.viewDidLoad()
self.restorationIdentifier = "myVC"
self.restorationClass = ViewController.self
view.backgroundColor = .white
setupUI()
}
#objc func change() {
sunny.toggle()
}
func setColor() {
colorView.backgroundColor = sunny == true ? .yellow : .darkGray
}
}
extension ViewController: UIViewControllerRestoration {
func setupUI() {
view.addSubview(colorView)
NSLayoutConstraint.activate([
colorView.centerXAnchor.constraint(equalTo: view.centerXAnchor),
colorView.centerYAnchor.constraint(equalTo: view.centerYAnchor),
colorView.widthAnchor.constraint(equalToConstant: view.frame.width),
colorView.heightAnchor.constraint(equalToConstant: 500)
])
view.addSubview(changeColor)
NSLayoutConstraint.activate([
changeColor.topAnchor.constraint(equalTo: colorView.bottomAnchor, constant: 10),
changeColor.centerXAnchor.constraint(equalTo: colorView.centerXAnchor),
changeColor.widthAnchor.constraint(equalToConstant: 100),
changeColor.heightAnchor.constraint(equalToConstant: 45)
])
}
override func encodeRestorableState(with coder: NSCoder) {
coder.encode(sunny, forKey: "isSunny")
super.encodeRestorableState(with: coder)
}
override func decodeRestorableState(with coder: NSCoder) {
guard let isSunny = coder.decodeBool(forKey: "isSunny") as? Bool else {return}
sunny = isSunny
setColor()
super.decodeRestorableState(with: coder)
}
static func viewController(withRestorationIdentifierPath identifierComponents: [String], coder: NSCoder) -> UIViewController? {
let vc = ViewController()
return vc
}
}

How I can load self.navigationController before `viewDidLoad` called

I want to do some stuff before the view controller calls viewDidLoad. I call loadViewIfNeeded() but self.navigationController is still nil. How can I load it?
AppDelegate.swift
func someMethod() {
var viewController = Storyboard.instantiate()
let viewModel = SomeViewModel()
vc.bind(to: viewModel)
}
BindableType.swift
protocol BindableType {
associatedtype ViewModelType
var viewModel: ViewModelType! { get set }
func bindViewModel()
}
extension BindableType where Self: UIViewController {
mutating func bind(to model: Self.ViewModelType) {
viewModel = model
loadViewIfNeeded()
// PROBLEM HERE: navigationController is nil, but view have been loaded
navigationController?.navigationBar.prefersLargeTitles = true
navigationController?.navigationItem.largeTitleDisplayMode = .automatic
bindViewModel()
}
}
SomeViewController.swift
class SomeViewController: BindableType {
override func viewDidLoad() {
super.viewDidLoad()
// navigationController?.navigationBar.prefersLargeTitles = true
// navigationController?.navigationItem.largeTitleDisplayMode = .automatic
}
func bindViewModel() {
...
}
}
I found the problem. I must put the line with pushing controller above viewController.bind(to: viewModel)
self.navigationViewController.pushViewController(viewController, animated: true)
viewController.bind(to: viewModel)

loading childViewControllers first time logging in

I have been struggling with an issue for loading child ViewControllers. The first time I log in it shows the containerView but without the childViewControllers loaded. When I close the app and re-open the app with the logged in state saved the childViews in the containerView are displayed. I know it has something to do with my window hierarchy and rootviewController. I have researched this a bunch but still am having issues solving. Thanks in advance!
// app delegate
#UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
window = UIWindow(frame: UIScreen.main.bounds)
window?.makeKeyAndVisible()
window?.rootViewController = MainNavigationController()
return true
}
// mainNavigationController - RootViewController
class MainNavigationController: UINavigationController {
override func viewDidLoad() {
super.viewDidLoad()
let vc1 = TravelersFeedVC()
let vc2 = ProfileVC()
if isLoggedIn() {
// assume user is logged in
let homeController = HomeController()
homeController.firstViewController = vc1
homeController.secondViewController = vc2
viewControllers = [homeController]
} else {
perform(#selector(showLoginController), with: nil, afterDelay: 0.01)
}
}
fileprivate func isLoggedIn() -> Bool {
return UserDefaults.standard.isLoggedIn()
}
func showLoginController() {
let loginController = LoginController()
present(loginController, animated: true, completion: {
// perhaps do something here later
})
}
}
// log in function
func finishLoggingIn() {
let rootViewController = UIApplication.shared.keyWindow?.rootViewController
guard let mainNavigationController = rootViewController as? MainNavigationController else { return }
mainNavigationController.viewControllers = [HomeController()]
let vc1 = TravelersFeedVC()
let vc2 = ProfileVC()
let homeController = HomeController()
homeController.firstViewController = vc1
homeController.secondViewController = vc2
UserDefaults.standard.setIsLoggedIn(value: true)
dismiss(animated: true, completion: nil)
}
// HomeController
class HomeController: UIViewController, FBSDKLoginButtonDelegate {
// child view controllers to put inside content view
var firstViewController: TravelersFeedVC?
var secondViewController: ProfileVC?
private var activeViewController: UIViewController? {
didSet {
removeInactiveViewController(inactiveViewController: oldValue)
updateActiveViewController()
}
}
private func removeInactiveViewController(inactiveViewController: UIViewController?) {
if let inActiveVC = inactiveViewController {
// call before removing child view controller's view from hierarchy
inActiveVC.willMove(toParentViewController: nil)
inActiveVC.view.removeFromSuperview()
// call after removing child view controller's view from hierarchy
inActiveVC.removeFromParentViewController()
}
}
private func updateActiveViewController() {
if let activeVC = activeViewController {
// call before adding child view controller's view as subview
addChildViewController(activeVC)
activeVC.view.frame = contentView.bounds
contentView.addSubview(activeVC.view)
// call before adding child view controller's view as subview
activeVC.didMove(toParentViewController: self)
}
}
// UI elements
lazy var contentView: UIView = {
let tv = UIView()
tv.backgroundColor = UIColor.blue
tv.translatesAutoresizingMaskIntoConstraints = false
tv.layer.masksToBounds = true
return tv
}()
var segmentedController: UISegmentedControl!
override func viewDidLoad() {
super.viewDidLoad()
activeViewController = firstViewController
checkIfUserIsLoggedIn()
view.addSubview(contentView)
setupProfileScreen()
let items = ["Travelers", "Me"]
segmentedController = UISegmentedControl(items: items)
navigationItem.titleView = segmentedController
segmentedController.tintColor = UIColor.black
segmentedController.selectedSegmentIndex = 0
// Add function to handle Value Changed events
segmentedController.addTarget(self, action: #selector(HomeController.segmentedValueChanged(_:)), for: .valueChanged)
navigationItem.leftBarButtonItem = UIBarButtonItem(title: "Sign Out", style: .plain, target: self, action: #selector(handleSignOut))
navigationItem.leftBarButtonItem?.tintColor = UIColor.black
}
// reference to collectionViewController
var travelersFeedVC: TravelersFeedVC!
func segmentedValueChanged(_ sender:UISegmentedControl!)
{
switch segmentedController.selectedSegmentIndex {
case 0:
activeViewController = firstViewController
case 1:
activeViewController = secondViewController
default: // Do nothing
break
}
}
In your finishLoggingIn() it looks like you are using 2 separate HomeController instances.
Something like this should fix it.
func finishLoggingIn() {
let rootViewController = UIApplication.shared.keyWindow?.rootViewController
guard let mainNavigationController = rootViewController as? MainNavigationController else { return }
// create it
let homeController = HomeController()
// use it
mainNavigationController.viewControllers = [homeController]
let vc1 = TravelersFeedVC()
let vc2 = ProfileVC()
// use it again
homeController.firstViewController = vc1
homeController.secondViewController = vc2
UserDefaults.standard.setIsLoggedIn(value: true)
dismiss(animated: true, completion: nil)
}

viewWillAppear/DidAppear called twice when changing rootViewController with an Animation

Here is the code of my animation/transition :
if let window = self.window where animated {
window.rootViewController?.dismissViewControllerAnimated(true, completion: {
UIView.transitionFromView(window.rootViewController!.view, toView: tabBarController.view, duration: 0.8, options: .TransitionFlipFromLeft, completion: { _ in
window.rootViewController = tabBarController
})
})
}
Without the animation code, (just setting the rootViewControlller), the problem does not appear.
Any idea why it makes the view appear twice and how I could fix this ?
using UIViewController.transitionFromViewController1 ,instead of your UIView.transitionFromView.act like it :
//
// AppDelegate.swift
// helloapp
//
// Created by quota on 2/18/16.
// Copyright © 2016 liu. All rights reserved.
//
import UIKit
class ViewController1: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
view.backgroundColor = UIColor.greenColor()
let button = UIButton()
button.setTitle("transit", forState: .Normal)
button.frame = CGRectMake(20, 20, 100,20)
button.addTarget(self, action: "click:", forControlEvents: .TouchDown)
view.addSubview(button)
}
func click(sender:UIButton!){
let appDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
if let window = appDelegate.window {
// let v2 = ViewController2()
if let r = window.rootViewController{
r.transitionFromViewController(
appDelegate.v1!
,toViewController:appDelegate.v2!,duration:0.8,options: .TransitionFlipFromLeft,animations:nil){_ in }
}
}
}
}
class ViewController2: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
view.backgroundColor = UIColor.blueColor()
}
override func viewWillAppear(animated: Bool) {
print(animated)
}
}
class ViewController3: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
view.backgroundColor = UIColor.redColor()
}
}
#UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
var v1 : ViewController1?
var v2 : ViewController2?
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
self.window = UIWindow(frame: UIScreen.mainScreen().bounds)
self.window!.rootViewController = ViewController3()
v1 = ViewController1()
v2 = ViewController2()
v1!.view.frame = self.window!.frame
self.window!.rootViewController?.addChildViewController(v1!)
self.window!.rootViewController?.view.addSubview(v1!.view)
self.window!.rootViewController?.addChildViewController(v2!)
self.window?.makeKeyAndVisible()
return true
}
}

'UIStoryboard.type' does not have member named 'centerViewController'

I am following THIS tutorial but I didn't downloaded starter project file from that tutorial because I want to make it differently but I am stuck here since an hour because I got this Error:
'UIStoryboard.type' does not have member named 'centerViewController'
Here I am trying to add subView (CenterViewController) into ContainerViewController.
Here is my code for ContainerViewController.swift
import UIKit
import QuartzCore
class ContainerViewController: UIViewController, CenterViewControllerDelegate {
var centerNavigationController: UINavigationController!
var centerViewController: CenterViewController!
override init() {
super.init(nibName: nil, bundle: nil)
}
required init(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
}
override func viewDidLoad() {
super.viewDidLoad()
centerViewController = UIStoryboard.centerViewController()
//'UIStoryboard.type' does not have member named 'centerViewController'
centerViewController.delegate = self
// wrap the centerViewController in a navigation controller, so we can push views to it
// and display bar button items in the navigation bar
centerNavigationController = UINavigationController(rootViewController: centerViewController)
view.addSubview(centerNavigationController.view)
addChildViewController(centerNavigationController)
centerNavigationController.didMoveToParentViewController(self)
}
}
This is my CenterViewController.swift
import UIKit
#objc
protocol CenterViewControllerDelegate {
optional func toggleLeftPanel()
optional func collapseSidePanels()
}
class CenterViewController: UIViewController {
var delegate: CenterViewControllerDelegate?
#IBAction func tableTapped(sender: AnyObject) {
}
}
And this is my AppDelegate.swift
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
window = UIWindow(frame: UIScreen.mainScreen().bounds)
let containerViewController = ContainerViewController()
window!.rootViewController = containerViewController
window!.makeKeyAndVisible()
return true
}
Can anyOne give me any Idea what I am missing?
You are missing the extension the tutorial author has provided that makes the UIStoryboard.centerViewController() method exist. The code is at the bottom of ContainerViewController.swift in his downloadable starter project, and I've copied it down below as well:
private extension UIStoryboard {
class func mainStoryboard() -> UIStoryboard { return UIStoryboard(name: "Main", bundle: NSBundle.mainBundle()) }
class func leftViewController() -> SidePanelViewController? {
return mainStoryboard().instantiateViewControllerWithIdentifier("LeftViewController") as? SidePanelViewController
}
class func rightViewController() -> SidePanelViewController? {
return mainStoryboard().instantiateViewControllerWithIdentifier("RightViewController") as? SidePanelViewController
}
class func centerViewController() -> CenterViewController? {
return mainStoryboard().instantiateViewControllerWithIdentifier("CenterViewController") as? CenterViewController
}
}
Add this to the bottom of your ContainerViewController.swift and it should work. (That is, as long as you have these view controllers set up in the right storyboard files with the right identifiers.)