0

I am working on an app which requires me to be able to access a variable, tagToIndex that is in a function within the stub of a a delegate.

The class MyCell is being used as a custom class for table view cells. In the table view, each cell contains a segmented control. Each individual segmented control has a tag attached to it that corresponds to the cell row that the segmented control is in (tag definition not shown in code).

The segmented controls output an index for the option that is selected (i.e. option 0 corresponds to index of 0, and option 1 corresponds to index of 1) via a delegate (shown below as "stub")

The job of the tagToIndex dictionary is to take the selected indices of the segmented control and pair them with the tag that corresponds to that segmented control (i.e. if the segmented control in row 5 had an index value of 1, the value would be [5:1].

My question is: How can I read/access the dictionary tagToIndex in the class MyCell, as well as in other classes? Currently it is "stuck" within the willPressItemAt function. I have tried using another delegate, but it seems as if I would run into the same problem again, only tagToIndex would be within a different function

class MyCell: UITableViewCell, YSSegmentedControlDelegate {

var delegate: TagToIndexDelegate?

**//Creating Variable tagToIndex**
var tagToIndex = [0:0,1:0,2:0,3:0,4:0,5:0]



                            **// Some Logic...**


**//Segmented Control**   
let actionButton = YSSegmentedControl(
    frame: CGRect.zero,
    titles: [
        "Yes",
        "No"
    ])

                       **// Some Layout Stuff**


 **//The Stub**
func segmentedControl(_ segmentedControl: YSSegmentedControl, willPressItemAt index: Int) {

    **//Modifying Variable tagToIndex **
    tagToIndex[actionButton.tag] = index

    delegate?.finishPassing(dictionary: tagToIndex)
    print(tagToIndex)

}

func segmentedControl(_ segmentedControl: YSSegmentedControl, didPressItemAt index: Int) {

}

}

I need to use the dictionary for an if else statement within another class. The class's name is AnswerViewController, and it is just a regular UIViewController

Here is a picture of my app so far, just in case you are having trouble imagining it: https://i.stack.imgur.com/ezMUJ.png

Thanks, Nick

  • Don't use tags. They are awful and hacky. Pass the cell via a delegate method in the tableview controlller and then the tableview controller can easily determine which indexpath corresponds to the cell. See https://stackoverflow.com/questions/28659845/swift-how-to-get-the-indexpath-row-when-a-button-in-a-cell-is-tapped/38941510#38941510 – Paulw11 Jun 21 '17 at 14:11
  • To answer your question, is this a setting view controller that is in a tab bar controller? So your purpose of doing so is that when user toggle the control button, you want to set up something in another tab? – Fangming Jun 23 '17 at 12:42
  • Yes that is correct – Nicholas Tiwari Jun 24 '17 at 01:18

0 Answers0