Trying MVVM design pattern.
Created a tableView in storyboard and in FirstViewController added below code :
#IBOutlet weak var menuTblView: UITableView!
let menuViewModel = MenuTableViewModel()
override func viewDidLoad() {
super.viewDidLoad()
self.menuTblView.dataSource = menuViewModel
self.menuTblView.delegate = menuViewModel
menuViewModel.delegate = self
menuTblView.register(UINib.init(nibName: "MenuTableViewCell", bundle: nil), forCellReuseIdentifier: "MenuTableViewCell")
menuTblView.separatorStyle = .none
}
and created a MenuTableViewModel having the tableviewDelegate and tableViewDatasource methods.
In MenuTableViewModel added the didSelectRowAt:
extension MenuTableViewModel:UITableViewDelegate{
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
delegate.present()
}
}
protocol MenuTableViewModelDelegate : class {
func present()
}
and created a prototcol to present a SecondViewController.
In FirstViewController added below code:
extension FirstViewController : MenuTableViewModelDelegate {
func present() {
let asd = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "SecondViewController") as! SecondViewController
self.present(asd, animated: true, completion: nil)
}
}
The problem is the SecondViewController appears after clicking the tableViewCell twice.
EDIT:
MenuTableViewModel code :
class MenuTableViewModel: NSObject {
var delegate:MenuTableViewModelDelegate!
}
and in FirstViewController - ViewDidLoad added :
menuViewModel.delegate = self
Thanks #koropok for the solution. Added below code and it worked :
DispatchQueue.main.async {
self.delegate.present()
}
but why I have to do this.
Infact If I am adding below code:
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
tableView.deselectRow(at: indexPath, animated: false)
//DispatchQueue.main.async {
self.delegate.present()
//}
}
then also it's working fine.
My approach is wrong or I am missing something here.
Related
Let's say we have two view controllers, a parent with a label and a modally presented child with a table view. How would I pass the user's selection in the table view back to the parent using delegation?
ViewController1:
var delegate: vc2delegate?
override func viewDidLoad {
super.viewDidLoad()
let label.text = ""
}
ViewController2:
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 5
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "Cell") as! Cell
let selections = ["1", "2", "3", "4", "5"]
cell.selections.text = selections[indexPath.row]
return cell
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
if let cell = tableView.cellForRow(at: indexPath) as? Cell {
cell.didSelect(indexPath: indexPath as NSIndexPath)
}
dismiss(animated: true, completion: nil)
}
//wherever end of class is
protocol vc2delegate {
// delegate functions here
}
Do I even have the right approach? I never really got down this pattern and I think it's crucial for me to learn for iOS. Another tricky caveat may be that viewDidLoad() doesn't get called when you dismiss a modal view controller.
Take a look at the UIViewController life cycle docs: ViewDidLoad only gets called once.
There are plenty of guides on how to do this, just do a quick search.
You'll need to update the dataSource logic as I added a quick string array, and you'll most likely have something a bit more complex, but the idea is still the same.
BTW, I used your naming convention of vc1/vc2, but I hope you have more meaningful names for your controllers.
In your code you have the delegate on the wrong VC. Here is a quick code sample of what it should look like:
class VC1: UIViewController {
let textLabel = UILabel()
// whenever you're presenting the vc2
func presentVC2() {
var vc2 = VC2()
vc2.delegate = self
self.present(vc2, animated: true, completion: nil)
}
}
extension VC1: VC2Delegate {
func updateLabel(withText text: String) {
self.textLabel.text = text
}
}
protocol VC2Delegate: class {
func updateLabel(withText text: String)
}
class VC2: UIViewController {
weak var delegate: VC2Delegate?
let dataSource = ["string 1", "tring 2"]
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let string = dataSource[indexPath.row]
self.delegate?.updateLabel(withText: string)
dismiss(animated: true, completion: nil)
}
}
You can use callback function also to update your label from tableview:
1) Declare callback function into your VC2:
var callback:((String) -> Void)?
2) Call this function in your tableview CellForRowAt method in VC2:
let dataSource = ["string 1", "tring 2"]
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let string = dataSource[indexPath.row]
var cell = tableView.dequeueReusableCell(withIdentifier: "yourCell") as! YourCell
//here you can call callback function & pass string to VC1
cell.callback?(dataSource[indexPath.row])
}
3) Now you can call callback closure in VC1 in anywhere you call your VC2:
class VC1: UIViewController {
let textLabel = UILabel()
//I'm calling this(presentVC2()) function on ViewDidLoad you can call anywhere you want
func viewDidLoad() {
super.viewDidLoad()
presentVC2()
}
// whenever you're presenting the vc2
func presentVC2() {
var vc2 = VC2()
vc2.callback = { text in
self.textLabel.text = text
}
self.present(vc2, animated: true, completion: nil)
}
}
I am trying to retrieve selected item in tableView from ViewController which has a list from sqlite like checkmark selection. My FirstViewController have tableView and on clicking selective cell it opens ViewController to select items.
Once selection has been made I have to reload FirstViewController tableview. Actually I am trying to do but not accurately doing. Anybody help me please to select item and set in tableview. I am confused and not able to do that passing selection via segue.
My github project link is below:
My Project: https://github.com/MasamMahmood/SqliteDataList/tree/master/SqliteDataList
Reference: https://blog.apoorvmote.com/how-to-pass-selection-via-segue/
Updated One FirstViewController:
class FirstViewController: UIViewControlller, ListDelegate {
var selectedIndex: Int?
var selectedSection: Int?
//Click event for navigation from FirstViewController to SecondViewController
#IBAction func BackButtonAction(_ sender: Any) {
let vc = self.storyboard?.instantiateViewController(withIdentifier: "SecondViewController") as! SecondViewController
vc.delegate = self
self.navigationController?.pushViewController(vc, animated: true)
}
func listFunc(listValue: String) {
AppData?.sectionList?[selectedSection!].items?[selectedIndex!].textField = listValue
tableView.reloadData()
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
//Add these two line in your code
selectedIndex = indexPath.row
selectedSection = indexPath.section
}
}
Updated SecondViewController:
protocol ListDelegate {
func listFunc(listValue: String)
}
class SecondViewController: UIViewControlller {
var delegate: ListDelegate?
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let indexPath = tableView.indexPathForSelectedRow
let currentCell = tableView.cellForRow(at: indexPath!)!
//print(currentCell.textLabel?.text as Any)
currentCell.accessoryType = .checkmark
delegate?.listFunc(listValue: currentCell.textLabel?.text ?? "")
self.navigationController?.popViewController(animated: true)
}
}
What you need is a delegate to communicate back to the FirstViewController. You can do that like so...
protocol ViewControllerDelegate: AnyObject {
func viewControllerDidMakeSelection(at index: Int)
}
class ViewController: UIViewController {
#IBOutlet weak var tableView: UITableView!
weak var delegate: ViewControllerDelegate?
override func viewDidLoad() {
super.viewDidLoad()
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
delegate?.viewControllerDidMakeSelection(at: indexPath.row)
}
}
class FirstViewController: UIViewController, ViewControllerDelegate {
#IBOutlet weak var tableView: UITableView!
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let vc = self.storyboard?.instantiateViewController(withIdentifier: "ViewController") as! ViewController
vc.delegate = self
self.navigationController?.pushViewController(vc, animated: true)
}
func viewControllerDidMakeSelection(at index: Int) {
// This is where the magic happens
}
}
I have two viewControllers:
1. CheckoutVC
2. DeliveryTimeVC
In DeliveryTimeVC I have the following variable:
class DeliveryTimeVC: UIViewController {
var tableViewDay:String = ""
}
I use the following push to go from CheckoutVC to DeliveryTimeVC:
let storyboard = UIStoryboard(name: Storyboard.DeliveryTimeStoryboard, bundle: nil)
let controller = storyboard.instantiateViewController(withIdentifier: StoryboardId.DeliveryTimeVC)
navigationController?.pushViewController(controller, animated: true)
In DeliveryTimeVC I have a tableView where on didSelect I go back to CheckoutVC and in didSelect func I have the following code to append my variable before leaving the controller:
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
tableViewDay.removeAll()
tableViewDay.append(cell.weekDayLbl.text!)
navigationController?.popViewController(animated: true)
}
How can I transfer the String variable data from DeliveryTimeVC to another variable in CheckoutVC? Lets say I create a variable in CheckoutVC as:
class CheckoutVC: UIViewController, CartProductCellDelegate {
var tableViewDayTransferedData:String = ""
}
How can I transfer the data from tableViewDay:String to tableViewDayTransferedData:String
well you can use this lines
let storyboard = UIStoryboard(name: "NAMEOFYOURSTORYBOARD", bundle: nil)
let vc = storyboard.instantiateViewController(withIdentifier: "IDOFYOURVIEW") as! CheckoutVC
vc.tableViewDayTransferedData = self. tableViewDay
self.navigationController!.pushViewController(vc, animated: true)
Based on #Hassan Shahbazi comment above I was able to change his code slightly and get the final answer that works for me:
protocol DeliveryTimeDelegate: class {
func didGetData(tableViewDayTransferedData: String)
}
class DeliveryTimeVC: UIViewController {
weak var delegate: DeliveryTimeDelegate?
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
tableViewDay.removeAll()
tableViewDay.append(cell.weekDayLbl.text!)
delegate?.didGetData(tableViewDayTransferedData: tableViewDay)
navigationController?.popViewController(animated: true)
}
}
class CheckoutVC: UIViewController, DeliveryTimeDelegate {
func ...() {
let storyboard = UIStoryboard(name: Storyboard.DeliveryTimeStoryboard, bundle: nil)
let controller = storyboard.instantiateViewController(withIdentifier: StoryboardId.DeliveryTimeVC) as! DeliveryTimeVC
controller.delegate = self
navigationController?.pushViewController(controller, animated: true)
}
func didGetData(tableViewDayTransferedData: String) {
tableViewDayTransferedData = tableViewDayTransferedData
}
}
in the last func you see two same variables; they just have the same name: tableViewDayTransferedData = tableViewDayTransferedData first one is from CheckoutVC. the second one is from the protocol.
There are generally 2 main patterns for data/variable passing in iOS.
Delegate Pattern
The popular pattern for Objective-c days, but still useful and easy to do. You'll need to define a delegate protocol and use it for passing variables.
protocol DeliveryTimeDelegate: class {
func didGetData(tableViewDay: String)
}
class DeliveryTimeVC: UIViewController {
weak var delegate: DeliveryTimeDelegate?
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
tableViewDay.removeAll()
tableViewDay.append(cell.weekDayLbl.text!)
delegate?.didGetData(tableViewDay: "YOUR_VALUE")
navigationController?.popViewController(animated: true)
}
}
class CheckoutVC: UIViewController, CartProductCellDelegate {
func ...() {
let storyboard = UIStoryboard(name: Storyboard.DeliveryTimeStoryboard, bundle: nil)
let controller = storyboard.instantiateViewController(withIdentifier: StoryboardId.DeliveryTimeVC)
controller.delegate = self
navigationController?.pushViewController(controller, animated: true)
}
}
extension CheckoutVC: DeliveryTimeDelegate {
func didGetData(tableViewDay: String) {
tableViewDayTransferedData = tableViewDay
}
}
Closure Pattern
The pattern could be seen as the swift friendly implementation of delegate. It's important to pay enough attention to memory management and retain cycle issues when you're using closure.
class DeliveryTimeVC: UIViewController {
var onDataTransfered: ((String) -> Void)?
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
tableViewDay.removeAll()
tableViewDay.append(cell.weekDayLbl.text!)
self.onDataTransfered?("YOUR_VALUE")
navigationController?.popViewController(animated: true)
}
}
class CheckoutVC: UIViewController, DeliveryTimeDelegate {
func ...() {
let storyboard = UIStoryboard(name: Storyboard.DeliveryTimeStoryboard, bundle: nil)
let controller = storyboard.instantiateViewController(withIdentifier: StoryboardId.DeliveryTimeVC)
controller.onDataTransfered = { [weak self] tableViewDayTransferedData in
self?.tableViewDay = tableViewDayTransferedData
}
navigationController?.pushViewController(controller, animated: true)
}
}
I am a beginner to iOS coming from the android background and just learned about table view (for me it's an Android ListView). I am trying to separate data source & delegate from view controller. I found some tutorials on how to do so but stuck at figuring out how to send the clicked item to another view controller. The code is below:
class PictureTableViewController: UIViewController {
#IBOutlet weak var pictureTableView: UITableView!
private let picsDataSource: PicturesDataSource
required init?(coder aDecoder: NSCoder) {
self.picsDataSource = PicturesDataSource()
super.init(coder: aDecoder)
}
override func viewDidLoad() {
super.viewDidLoad()
pictureTableView.dataSource = picsDataSource
pictureTableView.reloadData()
pictureTableView.delegate = picsDataSource
}
}
class PicturesDataSource: NSObject, UITableViewDataSource, UITableViewDelegate{
private var pictureModels = [PictureModel]()
override init(){
let picModelsDataController = PictureModelsDataController()
pictureModels = picModelsDataController.pictureModels
}
func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return pictureModels.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: String(describing: PictureCell.self)) as! PictureCell
let picModel = pictureModels[indexPath.row]
cell.pictureName = picModel.pictureName
cell.imageItem = picModel.imageItem
return cell
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
//1 - try loading the "Detail" view controller and typecasting it to be DetailViewController
if let detailViewController = storyboard.instantiateViewController(withIdentifier: "PictureDetailView") as? PictureDetailViewController {
//2 - success! set its selecteImage property
detailViewController.selectedImgName = pictureModels[indexPath.row].pictureName
//3 - now push it onto the navigation controller
navigationController?.pushViewController(detailViewController, animated: true)
}
}
}
Error in: func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath){ }. since "storyboard" & "navigationController" are not available in PicturesDataSource class, how can I send clicked item(picture name) to the DetailsViewController
There are StackOverflow answers about separating data source and delegate but did not solve my problem.
Using: Xcode 8.3 beta 6
You can include a reference to main view controller at your table view events handler. Below is a playground code I derived from your example:
import UIKit
// MARK: - Model
struct Picture {
let title: String
let image: UIImage
}
struct PictureModelsDataSource {
let pictures = [
Picture(title: "exampleTitle", image: UIImage(named: "exampleImage")!),
Picture(title: "exampleTitle", image: UIImage(named: "exampleImage")!)
]
}
// MARK - View
class PictureCell: UITableViewCell {
#IBOutlet weak var pictureTitleLabel: UILabel!
#IBOutlet weak var pictureImage: UIImageView!
}
// MARK: - Controller
class PictureTableViewController: UIViewController {
// MARK: - Properties
#IBOutlet weak var pictureTableView: UITableView!
private var pictureListController: PictureListController?
// MARK: - View lifecycle
override func viewDidLoad() {
super.viewDidLoad()
pictureListController = PictureListController()
pictureListController?.viewController = self
pictureTableView.dataSource = pictureListController
pictureTableView.delegate = pictureListController
pictureTableView.reloadData()
}
}
class PictureDetailViewController: UIViewController {
var selectedPictureTitle: String?
}
class PictureListController: NSObject, UITableViewDataSource, UITableViewDelegate {
// MARK: - Properties
weak var viewController: PictureTableViewController?
private let pictures: [Picture] = {
let pictureModelsDataSource = PictureModelsDataSource()
return pictureModelsDataSource.pictures
}()
// MARK: - View setup
func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
// MARK: - Event handling
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return pictures.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
guard let cell = tableView.dequeueReusableCell(withIdentifier: String(describing: PictureCell.self)) as? PictureCell else {
return UITableViewCell()
}
let picture = pictures[indexPath.row]
cell.pictureTitleLabel.text = picture.title
cell.pictureImage.image = picture.image
return cell
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let pictureTitle = pictures[indexPath.row].title
let storyboard = UIStoryboard(name: "exampleStoryboard", bundle: nil)
if let pictureDetailViewController = storyboard.instantiateViewController(withIdentifier: "PictureDetailView") as? PictureDetailViewController {
pictureDetailViewController.selectedPictureTitle = pictureTitle
viewController?.navigationController?.pushViewController(pictureDetailViewController, animated: true)
}
}
}
See StoryBoard object can be obtained by using this
let storyboard : UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
Now your second question is about how to get navigation controller. It means how to get currentViewController in your case. This can be get by below code
func getCurrentViewController() -> UIViewController? {
if let rootController = UIApplication.shared.keyWindow?.rootViewController {
var currentController: UIViewController! = rootController
while( currentController.presentedViewController != nil ) {
currentController = currentController.presentedViewController
}
return currentController
}
return nil
}
Now your didSelectRowAt code will look like this
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let storyboard : UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
if let detailViewController = storyboard.instantiateViewController(withIdentifier: "PictureDetailView") as? PictureDetailViewController
detailViewController.selectedImgName = pictureModels[indexPath.row].pictureName
self.getCurrentViewController()!.pushViewController(detailViewController, animated: true)
}
You are trying to adhere to MVC, but you are confusing what your actual data source is.
PicturesDataSource is not your data source. It's the code that tells your table how to set itself up.
PictureModelsDataController() is the source from which you get the data that actually populates that table.
All of your posted code should be in the same class:
class PictureTableViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {
Change these lines:
pictureTableView.dataSource = picsDataSource
pictureTableView.delegate = picsDataSource
to
pictureTableView.dataSource = self // Note use of self because this is now the dataSource, not another class
pictureTableView.delegate = self // Note use of self because this is now the delegate, not another class
and remove:
private let picsDataSource: PicturesDataSource
required init?(coder aDecoder: NSCoder) {
self.picsDataSource = PicturesDataSource()
super.init(coder: aDecoder)
}
I have two controllers and I'd like to pass datas between them:
This is the first controller, a tableviewcontroller.
class BooksVC: UITableViewController {
var books: [String] = []
override func viewDidLoad() {
super.viewDidLoad()
navigationItem.title = "Books"
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return books.count
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
cell.textLabel?.text = books[indexPath.row]
return cell
}
}
And this is the second controller, a viewcontroller
class AddController: UIViewController {
#IBOutlet weak var inputField: UITextField!
override func viewDidLoad() {
super.viewDidLoad()
}
#IBAction func done(_ sender: Any) {
let myVC = storyboard?.instantiateViewController(withIdentifier: "BooksVC") as! BooksVC
myVC.books.append(inputField.text!)
myVC.tableView.reloadData()
dismiss(animated: true, completion: nil)
}
}
Reloaddata doesn't work, can you help me I would be very glad.
You need to have a reference to the first view controller to do what you need.
You can do something like this:
class AddController: UIViewController {
....
weak var booksVC: BooksVC?
#IBAction func done(_ sender: Any) {
booksVC.books.append(inputField.text!)
booksVC.tableView.reloadData()
dismiss(animated: true, completion: nil)
}
}
And when you instatiate an AddController, you need to pass a reference of your BooksVC. Something like this:
addController.booksVC = self
Alright for second ViewController when doneButton pressed you can
dissmisstheview
trasffer data
self.parentviewController. books.apped(inputField.text!)
self.dissmissviewcontroller
than in the first VC you can do something like this
in viewWillAppear func{
tableview.reloadData()
}