0

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];
}
Domas
  • 39
  • 2
  • 7
  • First, add a gesture recognizer for your labels: http://stackoverflow.com/questions/6324724/is-there-a-touch-method-for-uilabel. Second, iterate over the views from _anserview like this: http://stackoverflow.com/questions/4779048/looping-through-subviews-or-a-view. Last, compare switch tags and switch on / off. Let me know if this works for you. – Radu Dan Feb 28 '14 at 15:52
  • 2
    Why don't you just use a `UIButton` instead of a label? That's what they are made for! You can add an `IBAction` method for it and put the code to change the switch state inside. – Mischa Feb 28 '14 at 15:54
  • I don't know, why you need this, but here is a work around, just put UIButton below UILable, and add action on that button. – zaheer Feb 28 '14 at 15:56
  • @zaheer or just use a button – David Snabel-Caunt Feb 28 '14 at 16:05

3 Answers3

1

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];
coder
  • 10,460
  • 17
  • 72
  • 125
  • Its sounds like a good idea! My switches are all tagged. How do I switch the tagged UISwitch in IBAction? – Domas Feb 28 '14 at 16:10
  • Why would the OP use an `IBAction`? Look at the code in the question. IB is not being used. – rmaddy Feb 28 '14 at 16:10
  • @rmaddy You're absolutely correct - my oversite. I updated my code appropriately. – coder Feb 28 '14 at 16:22
0

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).

Wain
  • 118,658
  • 15
  • 128
  • 151
  • 1
    It's important to note that you need to enable user interaction for the labels for this to work. – rmaddy Feb 28 '14 at 15:59
0

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];
}
Srikanth
  • 1,861
  • 17
  • 29