Rob85
Rob85

Reputation: 1729

UISwitch to perform very simple operation

This may seem very silly, maybe i am not understanding it correctly.

I have a UISwitch in my scene.

 when a login button is clicked

     if the switch is on - do some thing
     if the switch is off - do something else

when i first add the switch and run the app i can switch it on and off but if i then add an outlet to my viewcontroller.h the switch is then greyed out. (most likely because it wants me to write my own animation and action???)

From my understanding for this i shouldnt need to create an action for it i just want to simply no if the switch is on or off.

Is this possible or do i need an action for it?

Upvotes: 1

Views: 56

Answers (1)

Maksud Ali
Maksud Ali

Reputation: 121

No need to write any animation related code just follow below step and you can achieve what you want.

  1. create a IBOutlet in your .h file. Connect it to UISwitch in storyboard
@interface FirstTabViewController : UIViewController {

    __weak IBOutlet UISwitch *switchLogin;
}

-(IBAction)btnLoginClicked:(id)sender;
  1. write below code in your .m file
   -(IBAction)btnLoginClicked:(id)sender {

      if (switchLogin.on) {
          //switch is on do whatever you want
          NSLog(@"switch is on do whatever you want");
      }
      else {
          //
          NSLog(@"switch is off do whatever you want");
      }
   }

Upvotes: 1

Related Questions