Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 940023
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T21:53:13+00:00 2026-05-15T21:53:13+00:00

So I have made a custom UIButton and added it to the code and

  • 0

So I have made a custom UIButton and added it to the code and made the connections in interfacebuiler. I want the button to work as a on and off switch, how do I do this correctly? I’m a beginner at iphone development and this is for a school project for this class I’m taking during the summer to get a head start for next semester.

So if anyone can help me understand how to do this the right way and maybe write comments in the code. Thanks for all the help.
David H.

Here’s my code:

//
//  FlashlightViewController.h
//

#import <UIKit/UIKit.h>
#import <AVFoundation/AVFoundation.h>

@interface FlashlightViewController : UIViewController {

    AVCaptureSession *torchSession;
    IBOutlet UIButton *button;
}

-(IBAction)pressButton:(id) sender;

@property (nonatomic, retain) AVCaptureSession *torchSession;
@property (nonatomic, retain) IBOutlet UIButton *button;

@end

Here is the .m file

//
//  FlashlightViewController.m
//

#import "FlashlightViewController.h"

@implementation FlashlightViewController
@synthesize torchSession;
@synthesize button;


- (void)viewDidLoad {

    AVCaptureSession *session = [[AVCaptureSession alloc] init];

    [session beginConfiguration];

    AVCaptureDevice *device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];

    if ([device hasTorch] && [device hasFlash]){
        [device lockForConfiguration:nil];
        [device setTorchMode:AVCaptureTorchModeOn];
        [device setFlashMode:AVCaptureFlashModeOn];
        [device unlockForConfiguration];

        AVCaptureDeviceInput *flashInput = [AVCaptureDeviceInput deviceInputWithDevice:device error: nil];
        if (flashInput){
            [session addInput:flashInput];
        }

        AVCaptureVideoDataOutput *output = [[AVCaptureVideoDataOutput alloc] init];
        [session addOutput:output];
        [output release];
        [session commitConfiguration];

        [session startRunning];
    }

    [self setTorchSession:session];
    [session release];

    [super viewDidLoad];
}
- (void)viewDidUnload {
    self.button = nil;
}

- (void)dealloc {
    [TorchSession release];
    [button release];
    [super dealloc];
}
-(IBAction)pressButton : (id) sender{

}
@end
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T21:53:13+00:00Added an answer on May 15, 2026 at 9:53 pm

    In your @interface section (.h), you also need IBOutlet IBAction pressButton;. Then, in Interface Builder, in the inspector (outlet section), select File’s Owner, and connect pressButton: to the UIButton’s Touch Up Inside action.

    To toggle the torch state, add BOOL torchAlreadyOn; to the @interface section (.h). Then, move your viewDidLoad custom code to the pressButton method. Then, at the end of the pressButton method, add:

    if (torchAlreadyOn) {
        torchAlreadyOn = NO;
    }
    else {
        torchAlreadyOn = YES;
    }
    

    Then, everywhere where you set the torch state to on, enclose it in an if…else statement which checks the BOOL:

    AVCaptureSession *session = [[AVCaptureSession alloc] init];
    
    [session beginConfiguration];
    
    AVCaptureDevice *device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
    
    if ([device hasTorch] && [device hasFlash]){
        [device lockForConfiguration:nil];
    
        if (torchAlreadyOn) {
            [device setTorchMode:AVCaptureTorchModeOn];
            [device setFlashMode:AVCaptureFlashModeOn];
        }
        else {
            [device setTorchMode:AVCaptureTorchModeOff];
            [device setFlashMode:AVCaptureFlashModeOff];
        }
        [device unlockForConfiguration];
    
        AVCaptureDeviceInput *flashInput = [AVCaptureDeviceInput deviceInputWithDevice:device error: nil];
        if (flashInput){
            if (!torchAlreadyOn) {
                [session addInput:flashInput];
            }
            else {
                [session removeInput:flashInput];
            }
        }
    
        AVCaptureVideoDataOutput *output = [[AVCaptureVideoDataOutput alloc] init];
        if (!torchAlreadyOn) {
            [session addOutput:output];
        }
        else {
            [session removeOutput:output];
        }
        [output release];
        [session commitConfiguration];
    
        [session startRunning];
    }
    
    [self setTorchSession:session];
    [session release];
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have made a custom suface button according this example: <Button> <Button.Template> <ControlTemplate TargetType=Button>
I have made a custom button bar as described here . Now, I want
Ok I have made my custom post type and they work fine(code shown below).
I have made a Custom Component in XML, consisting of a button with an
I have made a custom control for windows phone 7. If I use this
I have made a custom adapter class here is the code public class CustomArrayAdapterForReceipts
I have made a custom block in Magento that contains some JS. I want
I have made a custom link renderer for will_paginate and have placed the code
I need to generate a custom button through code, this is how i am
i have made my custom keypad so i want to enter the text with

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.