Search code examples
iosuiactionsheetuiswitch

How to get answer from UISwitch which is in UIactionsheet


I am implementing Uiswitch inside my uiactionsheet, I've successfully placed it where i wanted to, but couldn't get the way to USE it. here's is my code:

switchButton = [[UISwitch alloc] init];
[mySheet addSubview:switchButton];
[mySheet showInView:self.view];

UILabel *instruction = [[UILabel alloc] init];
[instruction setBackgroundColor:[UIColor clearColor]];
[instruction setText:@"Daily"];
[mySheet addSubview:instruction];

// get the dimension of the sheet to position the switch
CGSize parentSize  = mySheet.frame.size;
CGRect rect = switchButton.frame;
rect.origin.x = parentSize.width/2.0 - rect.size.width + 90;
rect.origin.y = parentSize.height - rect.size.height + 90 ;
switchButton.frame = rect;


//get the dimension of the sheet to possition the label
rect.origin.x = parentSize.width/2.0 - rect.size.width -20;
rect.origin.y = parentSize.height - rect.size.height + 90 ;
instruction.frame = rect;

I've initialized my switch button as an iboutlet. In actionsheet's function I'm trying to do this.

-(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:        (NSInteger)buttonIndex{



    switch (buttonIndex) {
        case 0:
        {

            NSLog(@"Yes Button is working");
        }
            break;
         case 1:
        {

            if(switchButton.on){

            NSLog(@"IS on ?");
        }
          else {

         NSLog(@"Is off?");

         }
        }
        default:
            break;
    }

It is all set

**IMP I've also tried on more thing i had created "-(ibAction)funct" for it but when I did this I couldn't get the to link this with my switch **


Solution

  • the following code will help

    switchButton = [[UISwitch alloc] init];
    
    [switchButton addTarget:self action:@selector(setState:) forControlEvents:UIControlEventTouchUpInside];