title: How to turn flashlight on/off using one button?
body: <p>I can turn my flashlight on with one button and turn it off with another. But I want to do it with only one button. However, I don't have a framework which allows me to use the bool isSelected method. So I'm quite clueless on how to merge both functions together in one button. </p>
  
  <p>Here's the code which works:</p>
  
  <pre><code>-(void)onButtonPressed 
  {
  
  AVCaptureDevice *flashLight = [AVCaptureDevice 
  defaultDeviceWithMediaType:AVMediaTypeVideo];
  if([flashLight isTorchAvailable] &amp;&amp; [flashLight
  isTorchModeSupported:AVCaptureTorchModeOn])
  {
      BOOL success = [flashLight lockForConfiguration:nil];
      if(success){
          [flashLight setTorchMode:AVCaptureTorchModeOn];
          [flashLight unlockForConfiguration];
      }
  }
  
  }
  </code></pre>
  
  <p>I use this to turn the flashlight off.</p>
  
  <pre><code>-(void)offButtonPressed {
  
  AVCaptureDevice *flashLight = [AVCaptureDevice
  defaultDeviceWithMediaType:AVMediaTypeVideo];
  if([flashLight isTorchAvailable] &amp;&amp; [flashLight
  isTorchModeSupported:AVCaptureTorchModeOn])
  {
      BOOL success = [flashLight lockForConfiguration:nil];
      if(success){
          [flashLight setTorchMode:AVCaptureTorchModeOff];
          [flashLight unlockForConfiguration];
      }
  }
  
  
  }
  </code></pre>
  
  <p>I'm not particular about the way it's done. As long as the flashlight turns on with the first tap and turns off on the second, I couldn't care less about the method. </p>
  
  <p>However, I'm using barbuttonitems made programatically, so please don't give me IBAction methods. I'd also appreciate it if the method suggested is as simple as possible, I think the way I'm using the flashlight right now is overly complex.</p>