Question

I've got this method were it displays UILabels and UISwitches right next to them. I want the UISwitch to be switched on and off by pressing on a label as well. I have tagged the switches but I don't know what to do next.. Any help would much appreciated. Thanks!

while (i < numberOfAnswers) {
    UILabel *answerLabel = [[UILabel alloc] initWithFrame:CGRectMake(65, y+spaceBetweenAnswers, 240, 30)];
    answerLabel.text = [NSString stringWithFormat:@"%@ (%@)", questionBank[randomQuestionNumber][1][i][0],questionBank[randomQuestionNumber][1][i][1]];
    answerLabel.font = [UIFont systemFontOfSize:15];
    answerLabel.textColor = [UIColor darkGrayColor];
    answerLabel.numberOfLines=0;
    [answerLabel sizeToFit];
    [_answerView addSubview:answerLabel];
    answerHeight = answerLabel.frame.size.height + spaceBetweenAnswers;

    UISwitch *mySwitch = [[UISwitch alloc] initWithFrame:CGRectMake(10, y+spaceBetweenAnswers-5, 0, 30)];
    mySwitch.transform = CGAffineTransformMakeScale(0.75, 0.75);
    if ([questionBank[randomQuestionNumber][1][i][1] isEqual:@"0"]) {
         [mySwitch addTarget:self action:@selector(wrongAnswer:) forControlEvents:UIControlEventValueChanged];
    } else {
    }
    mySwitch.tag = i;
    [_answerView addSubview:mySwitch];
}
Was it helpful?

Solution

Use a UIButton instead of a UILabel and set an IBAction on it. You can style the button to look exactly like the label. Your IBAction method should look something like this:

- (void)buttonPressed:(UIButton *)sender
{
    //Get the view by tag
    UISwitch *mySwitch = (UISwitch *)[self.view viewWithTag:yourTag];
    [mySwitch.setOn:![mySwitch isOn]];
}

Edit

As mentioned, since you're building the UIButtons in code, you need to add the target to the button to get the button click. Add the action as such:

[button addTarget:self action:@selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];

OTHER TIPS

Add a tap gesture recognizer to each label. When it is tapped, you can get the label from the gesture (view). Now you just need to get the switch to update.

You could add a tag to the label which is 1000 + i, then a simple subtraction will give you the switch tag that you need to find.

You could tag the labels and use the tag as the index into an array or switches (possibly IBOutletCollection).

You can do something like this.

UISwitch* mySwitch = [[UISwitch alloc]init];
[self addSubview:mySwitch];

UIButton* switchLabelBtn = [UIButton buttonWithType:UIButtonTypeCustom];
switchLabelBtn.frame = CGRectMake(0, 0, 80, 40);
[switchLabelBtn addTarget:self action:@selector(switchLabelbtnTapped:) forControlEvents:UIControlEventTouchUpInside];
[switchLabelBtn.layer setValue:mySwitch forKey:@"Switch"];
[self addSubview:switchLabelBtn];


- (void)switchLabelbtnTapped:(UIButton*)sender
{
    UISwitch* mySwitch = [sender.layer valueForKey:@"Switch"];
    mySwitch.on = ![mySwitch isOn];
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top