Protocol Delegate Does not Trigger Swift - ios

I want to trigger MultipleAnswerTableViewCell's mainSaveTapped func when the IBAction saveButton tapped.
Here is my class with the protocol
protocol AddQuestionViewControllerDelegate: AnyObject {
func mainSaveTapped()
}
class AddQuestionViewController: BaseViewController, MultipleAnswerTableViewCellDelegate, UITextFieldDelegate {
weak var delegate: AddQuestionViewControllerDelegate?
#IBAction func saveTapped(_ sender: UIButton) {
delegate?.mainSaveTapped()
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
switch cellTypes[indexPath.row] {
case .fixedTop: return configureFixedTop(indexPath: indexPath)
case .multiAnswer: return configureMultiAnswerCell(indexPath: indexPath)
case .singleAnswer: return configureSingleAnswerCell(indexPath: indexPath)
case .textAnswer: return configureTextAnswerCell(indexPath: indexPath)
}
}
func configureMultiAnswerCell(indexPath: IndexPath) -> MultipleAnswerTableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "MultipleAnswerTableViewCell") as! MultipleAnswerTableViewCell
cell.parentVC = self
cell.answerDelegate = self
cell.setupMultipleAnswerView()
return cell
}
And this is my MultipleAnswerTableViewCell
protocol MultipleAnswerTableViewCellDelegate: AnyObject {
func sendAnswers(surveyAnswers: [SurveyAnswer])
}
class MultipleAnswerTableViewCell: UITableViewCell, AddQuestionViewControllerDelegate, UITextFieldDelegate {
weak var answerDelegate: MultipleAnswerTableViewCellDelegate?
func mainSaveTapped() {
checkPostShare()
}
My func mainSaveTapped never triggered hence checkPostShare never called. What should I do to trigger mainSaveTapped func inside the MultipleAnswerTableViewCell

You don't assign a value to the delegate property inside the vc
weak var delegate: AddQuestionViewControllerDelegate?
So here delegate? is nil
#IBAction func saveTapped(_ sender: UIButton) {
delegate?.mainSaveTapped() // delegate? is nil
}
You need to send a message from the vc to the cell , here you don't need a delegate ,instead
#IBAction func saveTapped(_ sender: UIButton) {
(tableView.visibleCells as? [MultipleAnswerTableViewCell])?.forEach { cell in
cell.mainSaveTapped()
}
}

Related

index of button in custom cell

I create a custom cell that contains a button, I need to create segue from this button to other VC but first of all, I would like to push an object with that segue.
I already try to use cell.button.tag, but I did not succeed.
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "showMap" {
let mapVC = segue.destination as! MapViewController
//guard let indexPath = tableView.indexPathForSelectedRow else { return }
mapVC.place = places[] // <- "here I need index of button in cell"
}
}
Instead of using the segue, handle the navigation programatically through a closure in UITableViewCell.
class CustomCell: UITableViewCell {
var buttonTapHandler: (()->())?
#IBAction func onTapButton(_ sender: UIButton) {
self.buttonTapHandler?()
}
}
In the above code, I've create a buttonTapHandler - a closure, that will be called whenever the button inside the cell is tapped.
Now, in cellForRowAt method when you dequeue the cell, set the buttonTapHandler of CustomCell.
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "CustomCell", for: indexPath) as! CustomCell
cell.buttonTapHandler = {[weak self] in
if let mapVC = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "MapViewController") as? MapViewController {
mapVC.place = places[indexPath.row]
self?.navigationController?.pushViewController(mapVC, animated: true)
}
}
return cell
}
In the above code, buttonTapHandler when called will push a new instance of MapViewController along with the relevant place based on the indexPath.
if you don't want to execute your code in didSelectRowAt method, another good approach in my opinion is to create a delegate of your custom cell. See the code below
// This is my custom cell class
class MyCustomCell: UITableViewCell {
// The button inside your cell
#IBOutlet weak var actionButton: UIButton!
var delegate: MyCustomCellDelegate?
#IBAction func myDelegateAction(_ sender: UIButton) {
delegate?.myCustomAction(sender: sender, index: sender.tag)
}
// Here you can set the tag value of the button to know
// which button was tapped
func configure(index: IndexPath){
actionButton.tag = index.row
}
}
protocol MyCustomCellDelegate {
func myDelegateAction(sender: UIButton, index: Int)
}
Delegate the ViewController where you use your custom cell.
class MyViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "MyCellIdentifier", for: indexPath) as! MyCustomCell
cell.configure(index: indexPath)
cell.delegate = self
return cell
}
}
And at the end customize your method extending your custom cell delegate
extension MyViewController: MyCustomCellDelegate {
func myDelegateAction(sender: UIButton, index: Int) {
// Do your staff here
}
}
I hope I was helpful.
In the custom cell:
import UIKit
protocol CustomCellDelegate: class {
func btnPressed(of cell: CustomCell?)
}
class CustomCell: UITableViewCell {
weak var delegate: CustomCellDelegate?
#IBAction func btnTapped(_ sender: UIButton) {
delegate?.btnPressed(of: self)
}
}
And in the view controller:
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell: CustomCell = tableView.dequeueReusableCell(for: indexPath)
cell.delegate = self
return cell
}
extension ViewController: CustomCellDelegate {
func btnPressed(of cell: CustomCell?) {
if let cell = cell, let indexPath = tableView.indexPath(for: cell) {
// Your stuff here
}
}
}

Table view cell elements not able to click and get data

I have one table view and inside that i placed one main view. And inside that main view i placed one button.And when ever use click on my cell button. I need to get the cell title label.This is what i need. But i tried following below code. Not sure what i am missing out. It not at all calling my cell.add target line.
Code in cell for row at index:
cell.cellBtn.tag = indexPath.row
cell.cellBtn.addTarget(self, action:#selector(self.buttonPressed(_:)), for:.touchUpInside)
#objc func buttonPressed(_ sender: AnyObject) {
print("cell tap")
let button = sender as? UIButton
let cell = button?.superview?.superview as? UITableViewCell
let indexPath = tableView.indexPath(for: cell!)
let currentCell = tableView.cellForRow(at: indexPath!)! as! KMTrainingTableViewCell
print(indexPath?.row)
print(currentCell.cellTitleLabel.text)
}
I even added a breakpoint, still it not at calling my cell.addTarget line
Tried with closure too. In cell for row at index:
cell.tapCallback = {
print(indexPath.row)
}
In my table view cell:
var tapCallback: (() -> Void)?
#IBAction func CellBtndidTap(_ sender: Any) {
print("Right button is tapped")
tapCallback?()
}
Here that print statement is getting print in console.
import UIKit
class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
var list = [String]()
#IBOutlet weak var tableView: UITableView!
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return list.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! MyTableViewCell
cell.saveButton.tag = indexPath.row
//cell.saveButton.accessibilityIdentifier = "some unique identifier"
cell.tapCallback = { tag in
print(tag)
}
return cell
}
}
class MyTableViewCell: UITableViewCell {
// MARK: - IBOutlets
#IBOutlet weak var saveButton: UIButton!
// MARK: - IBActions
#IBAction func saveTapped(_ sender: UIButton) {
tapCallback?(sender.tag)
}
// MARK: - Actions
var tapCallback: ((Int) -> Void)?
}
Actually this is not a good programming practice to add the button (which contains in table view cell) target action in view controller. We should follow the protocol oriented approach for it. Please try to under stand the concept.
/*This is my cell Delegate*/
protocol InfoCellDelegate {
func showItem(item:String)
}
/*This is my cell class*/
class InfoCell: UITableViewCell {
//make weak reference to avoid the Retain Cycle
fileprivate weak var delegate: InfoCellDelegate?
//Outlet for views
#IBOutlet var showButton: UIButton?
override func awakeFromNib() {
super.awakeFromNib()
}
override func setSelected(_ selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
}
//This is the public binding function which will bind the data & delegate to cell
func bind(with: DataModel?, delegate: InfoCellDelegate?, indexPath: IndexPath) {
//Now the bind the cell with data here
//.....
//Assign the delegate
self.delegate = delegate
}
//Button action
#IBAction func rowSelected(sender: UIButton) {
self.delegate?.showItem(item: "This is coming from cell")
}
}
/*Now in your ViewController you need to just confirm the InfoCellDelegate & call the bind function*/
class ListViewController: UIViewController {
//Views initialisation & other initial process
}
//Table view Delegate & Data source
extension ListViewController: UITableViewDataSource, UITableViewDelegate {
/**
Configure the table views
*/
func configureTable() {
//for item table
self.listTable.register(UINib.init(nibName: "\(InfoCell.classForCoder())", bundle: nil), forCellReuseIdentifier: "\(InfoCell.classForCoder())")
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 5
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "InfoCell") as! InfoCell
cell.bind(with: DataModel, delegate: self, indexPath: indexPath)
return cell
}
}
extension ListViewController: InfoCellDelegate {
func showItem(item) {
print(item)
}
}

Call function in secondFile.swift/viewController from firstFile.swift/viewController?

I'm trying to call a function() made in my first ViewController from another function() made in the second ViewController.
It's a function to update the title of a button in the firstViewController.
I have searched but I can't find a way.
First ViewController // ViewController.swift
import UIKit
class ViewController: UIViewController, UITextFieldDelegate {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
weightLabel.delegate = self
}
#IBAction func excerciseChooserButton(_ sender: UIButton) {
}
var weight = 0 {
didSet {
weightLabel.text = "\(weight)"
}
}
// User input WEIGHT
#IBOutlet weak var weightLabel: UITextField!
func textField(_ weightLabel: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
let isNumber = CharacterSet.decimalDigits.isSuperset(of: CharacterSet(charactersIn: string))
let withDecimal = (
string == NumberFormatter().decimalSeparator &&
weightLabel.text?.contains(string) == false
)
return isNumber || withDecimal
}
#IBAction func plusWeight(_ sender: UIButton) {
weight += 5
}
#IBAction func minusWeight(_ sender: UIButton) {
weight -= 5
}
// User input REPS
#IBOutlet weak var repLabel: UILabel!
#IBAction func repSlider(_ sender: UISlider) {
let currentRepValue = Int(sender.value)
repLabel.text = "\(currentRepValue)"
let cm = Calculator(weight: weightLabel.text!, reps: repLabel.text!)
let result = cm.calcRM()
repMax.text = "1RM: \(result)kg"
}
#IBOutlet weak var repMax: UILabel!
#IBOutlet weak var excerciseLabel: UIButton!
func changeText() {
excerciseLabel.setTitle(Excercises.excChosen, for: .normal)
print(excerciseLabel)
}
#IBAction func unwindToViewController(segue:UIStoryboardSegue) {
}
}
// // // //
Second ViewController // ExcerciseChooserViewController.swift
import UIKit
struct Excercises {
static var excChosen:String? = ""
}
class ExcerciseChooserViewController: UIViewController, UITableViewDelegate, UITableViewDataSource
// Data model: These strings will be the data for the table view cells
let excercises: [String] = ["Bench Press", "Squat", "Push Press", "Deadlift"]
// cell reuse id (cells that scroll out of view can be reused)
let cellReuseIdentifier = "cell"
// don't forget to hook this up from the storyboard
#IBOutlet var tableView: UITableView!
override func viewDidLoad() {
super.viewDidLoad()
// Register the table view cell class and its reuse id
self.tableView.register(UITableViewCell.self, forCellReuseIdentifier: cellReuseIdentifier)
// (optional) include this line if you want to remove the extra empty cell divider lines
// self.tableView.tableFooterView = UIView()
// This view controller itself will provide the delegate methods and row data for the table view.
tableView.delegate = self
tableView.dataSource = self
}
// number of rows in table view
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return self.excercises.count
}
// create a cell for each table view row
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
// create a new cell if needed or reuse an old one
let cell:UITableViewCell = self.tableView.dequeueReusableCell(withIdentifier: cellReuseIdentifier) as UITableViewCell!
// set the text from the data model
cell.textLabel?.text = self.excercises[indexPath.row]
return cell
}
// method to run when table view cell is tapped
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let excerciseChosen = "\(excercises[indexPath.row])"
print("You tapped cell number \(indexPath.row).")
print(excerciseChosen)
goBackToOneButtonTapped((Any).self)
Excercises.excChosen = excerciseChosen
print(Excercises.excChosen!)
// call function to update text
ViewController.changeText()
}
#IBAction func goBackToOneButtonTapped(_ sender: Any) {
performSegue(withIdentifier: "unwindToViewController", sender: self)
}
}
Call it from unwindToViewController instead, no need to call it while the first view controller is not visible
There are many ways to do this, but I'll describe a simple one here.
Because you're going back to 'ViewController' via a segue, a good option for you is to override prepare(for:sender:). This will give you a reference to the destination view controller of that segue, which will then allow you to call functions or set properties in that view controller. You can read more about this method here.
Here are some basic steps:
In ViewController, update your changeText() method to accept a string parameter: changeText(_ text: String?).
Add a property to ExcerciseChooserViewController to hold the text you want to use: private var chosenExercise: String?
In your tableView:DidSelectRowAtIndexPath: method, set your new chosenExercise property to the string you want to pass to ViewController.
In prepare(for:sender:) of ExcerciseChooserViewController, grab a reference to destination view controller, downcast it to your subclass ViewController, and call your new method passing in the exerciseText string.
For example:
class ViewController: UIViewController, UITextFieldDelegate {
#IBOutlet weak var excerciseLabel: UIButton!
func changeText(_ text: String?) {
guard let text = text else { return }
excerciseLabel.setTitle(text, for: .normal)
print(excerciseLabel)
}
}
And in ExcerciseChooserViewController:
class ExcerciseChooserViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
private var chosenExercise: String?
// method to run when table view cell is tapped
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let excerciseChosen = "\(excercises[indexPath.row])"
print("You tapped cell number \(indexPath.row).")
print(excerciseChosen)
goBackToOneButtonTapped((Any).self)
Excercises.excChosen = excerciseChosen
print(Excercises.excChosen!)
chosenExercise = excerciseChosen
}
#IBAction func goBackToOneButtonTapped(_ sender: Any) {
performSegue(withIdentifier: "unwindToViewController", sender: self)
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if let destinationVC = segue.destination as? ViewController {
destinationVC.changeText(chosenExercise)
}
}
}

Can't Click on button in tableView cell after reloadData

I have in cell implement 3 buttons and button action. After call tableView.reloadData() can't click to any button in cell.
In controller in func cellForRow call cell?.onbuttonTappedAdd and other. Afer add element I call tabelViewReloadData.
//TableViewCell
var onButtonTappedAdd : (() -> Void)? = nil
var onButtonTappedRemove : (() -> Void)? = nil
override func awakeFromNib() {
super.awakeFromNib()
// Initialization code
}
func configureCell(orderitem: OrderItem){
self.orderItem = orderitem!
}
func setButtonGradient(){
}
#IBAction func changeValue(_ sender: Any) {
}
#IBAction func btnAddValue(_ sender: Any) {
if let onButtonTappedAdd = self.onButtonTappedAdd
{
onButtonTappedAdd()
}
}
#IBAction func btnRemoveValue(_ sender: Any) {
if let onButtonTappedRemove = self.onButtonTappedRemove {
onButtonTappedRemove()
}
}
//ViewControll cellForRow
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
var cell = tableViewOrderItem.dequeueReusableCell(withIdentifier: "OrderItemCheckoutCell", for: indexPath)
as? OrderItemCheckoutCell
var orderItem = GlobalArray.orderArrayList[indexPath.section].ListOrderItem[indexPath.row]
cell?.configureCell(orderitem: orderItem)
cell?.onButtonTappedAdd = {
GlobalArray.orderArrayList[indexPath.section].ListOrderItem[indexPath.row].quantity = GlobalArray.orderArrayList[indexPath.section].ListOrderItem[indexPath.row].quantity! + (GlobalArray.orderArrayList[indexPath.section].ListOrderItem[indexPath.row].Product?.bigValue)!
self.tableViewOrderItem.reloadData()
self.setTotalInfo()
}
cell?.onButtonTappedRemove = {
GlobalArray.orderArrayList[indexPath.section].ListOrderItem[indexPath.row].quantity = GlobalArray.orderArrayList[indexPath.section].ListOrderItem[indexPath.row].quantity! - (GlobalArray.orderArrayList[indexPath.section].ListOrderItem[indexPath.row].Product?.bigValue)!
self.tableViewOrderItem.reloadData()
}
return cell!
}
Reload TableView Outside of the "cellforRow" method

Tableview.reloadData doesn't work

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()
}

Resources