Thursday, October 8, 2026
HomeiOS Developmentios - How do I exploit didSelectRowAt with a coordinator?

ios – How do I exploit didSelectRowAt with a coordinator?

[ad_1]

I’ve two sub sections, orders and objects for which I’m dealing with the navigation, one utilizing coordinators and different utilizing regular view controller:

Orders with using coordinators:

Orders Controller:

class OrdersViewController: UIViewController, OrdersBaseCoordinated {
    
    var coordinator: OrdersBaseCoordinator?
    var goToOrders2button: UIButton!
    
    init(coordinator: OrdersBaseCoordinator) {
        tremendous.init(nibName: nil, bundle: nil)
        self.coordinator = coordinator
        title = "Orders"
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been carried out")
    }
non-public func configureButton() {
        goToOrders2button = UIButton()
        view.addSubview(goToOrders2button)
        
        goToOrders2button.addTarget(self, motion: #selector(goToOrders2), for: .touchUpInside)
        
       
    }
    
    @objc non-public func goToOrders2() {
        coordinator?.moveTo(move: .orders(.secondScreen), userData: nil)
    }

Orders Coordinator:

class OrdersCoordinator: OrdersBaseCoordinator {
    
    var parentCoordinator: MainBaseCoordinator?
    
    var rootViewController: UIViewController = UIViewController()
    
    func begin() -> UIViewController {
        rootViewController = UINavigationController(rootViewController: OrdersViewController(coordinator: self))
        return rootViewController
    }
    
    func moveTo(move: AppFlow, userData: [String : Any]? = nil) {
        change move {
        case .orders(let display screen):
            handleOrdersFlow(for: display screen, userData: userData)
        default:
            parentCoordinator?.moveTo(move: move, userData: userData)
        }
    }
    
    non-public func handleOrdersFlow(for display screen: OrdersScreen, userData: [String : Any]? = nil) {
        change display screen {
        case .firstScreen:
            resetToRoot(animated: false)
        case .secondScreen:
            handleGoToSecondScreen()
        
    }
    
    non-public func handleGoToSecondScreen() {
        resetToRoot(animated: false)
      navigationRootViewController?.pushViewController(Orders2ViewController(coordinator: self), animated: false)

    }

This works wonderful and I’m able to click on the button and change to the following display screen utilizing the coordinators.

Merchandise View Controller:

class ItemsViewController: UITableViewController, ItemsBaseCoordinated{

var coordinator: ItemsBaseCoordinator?

init(coordinator: ItemsBaseCoordinator) {
    tremendous.init(nibName: nil, bundle: nil)
    self.coordinator = coordinator
    title = "Gadgets"
}

required init?(coder: NSCoder) {
    fatalError("init(coder:) has not been carried out")
}


non-public var myTableView: UITableView!



override func viewDidLoad() {
    tremendous.viewDidLoad()
    
     tableView.register(ItemCell.self, forCellReuseIdentifier: "MyCell")
}


override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    let nextVC = Items2ViewController()
    nextVC.objects = itemList[indexPath.row]
    coordinator?.moveTo(move: .objects(.secondItem), userData: nil)
    //navigationController?.pushViewController(nextVC, animated: true)
}   

}

I’ve skipped some elements of the above controller to cut back verbosity, beneath is the objects coordinator:

ItemsCoordinator:

class ItemsCoordinator: ItemsBaseCoordinator {
    
    var parentCoordinator: MainBaseCoordinator?
    
    var rootViewController: UIViewController = UIViewController()
    
    func begin() -> UIViewController {
        rootViewController = UINavigationController(rootViewController: ItemsViewController(coordinator: self))
        return rootViewController
    }
    
    func moveTo(move: AppFlow, userData: [String : Any]? = nil) {
        change move {
        case .objects(let display screen):
            handleItemsFlow(for: display screen, userData: userData)
        default:
            parentCoordinator?.moveTo(move: move, userData: userData)
        }
    }
    
    non-public func handleItemsFlow(for display screen: ItemsScreen, userData: [String : Any]? = nil) {
        change display screen {
        case .firstItem:
            resetToRoot(animated: false)
        case .secondItem:
            //resetToRoot(animated: false)
            handleGoToSecondScreen()
        
        }
    }
non-public func handleGoToSecondScreen() {
        resetToRoot(animated: false)
        navigationRootViewController?.pushViewController(Items2ViewController(), animated: false)

    }
}

How do I exploit the coordinator to create a transition for a UITableView utilizing the didSelectRowAt with the coordinator, proper now it really works wonderful with the pushviewcontroller however how do I make this work with a coordinator?

The place do I add the indexPath.row within the coordinator so it may possibly transition to the nextVC for every merchandise within the itemList?

[ad_2]

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments