Creating a UISwitch Programmatically

59,158

Solution 1

The compiler is trying to help you out. You're overriding the onoff instance variable in your viewDidLoad; thus, that's never getting set. In your -flip: method, you're referencing a nil controller. There are two ways to fix this:

(a) Get rid of the local declaration of onoff, and just use your instance variable

(b) Cast the sender argument to -flip: as a UISwitch, and access that:

- (IBAction) flip: (id) sender {
    UISwitch *onoff = (UISwitch *) sender;
    NSLog(@"%@", onoff.on ? @"On" : @"Off");
}

Solution 2

Steve, I'm sorry if I misunderstood your question. Did you actually create the UISwitch and add it to the view hierarchy? Your controller's -loadView or -viewDidLoad implementation should have code like the following:

// Use the ivar here
onoff = [[UISwitch alloc] initWithFrame: CGRectZero];
[onoff addTarget: self action: @selector(flip:) forControlEvents: UIControlEventValueChanged];
// Set the desired frame location of onoff here
[self.view addSubview: onoff];
[onoff release];
Share:
59,158
Steve
Author by

Steve

Updated on May 12, 2020

Comments

  • Steve
    Steve about 4 years

    In a seemingly never ending effort to learn more about iphone development, I have been playing around with some of the source code available through apples developer website. The particular example I am working with is Core Data Books, found here. The DetailViewController, and the AddViewController are made programatically, because there aren't any xib files for them. My question is about programatically adding things to a view without using IB. I want to put a UISwitch beneath the UITableView that has the detailed information about a particular book in the DetailView. How do I do this? This is what I have tried so far:

    In the AddViewController, I setup the UISwitch:

    @interface AddViewController : DetailViewController {
    id <AddViewControllerDelegate> delegate;
    UISwitch *onoff;
    
     }
    
    @property (nonatomic, assign) id <AddViewControllerDelegate> delegate;
    @property (nonatomic, retain) IBOutlet UISwitch *onoff;
    

    I also setup an IBAction:

    - (IBAction)flip:(id)sender;
    

    Then I synthesize it in the AddViewController.m file, but nothing happens. I just need to setup the switch and make it so that I can control what it does from my IBAction that I setup. I know this is embarrassingly simple, but I can't figure it out. So, any help would be appreciated! Thanks

    Edit 1

    So I implemented the code as I was directed to in the viewDidLoad, like so:

       - (void)viewDidLoad {
    
    [super viewDidLoad];    
      UISwitch *onoff = [[UISwitch alloc] initWithFrame: CGRectZero];
    [onoff addTarget: self action: @selector(flip:) forControlEvents:UIControlEventValueChanged];
    // Set the desired frame location of onoff here
    [self.view addSubview: onoff];
    

    And it throws two warnings saying that the local declaration of 'onoff' hides the instance variable. But even though there are those earnings, the UISwitch pops up just fine, but when I move it, or use it, it doesn't look like it is working fully. For my action that looks like this:

    - (IBAction)flip:(id)sender {
    if (onoff.on) NSLog(@"On");  
    else  NSLog(@"Off");  
     }
    

    Whenever the switch is on, the console should read on, and when its off, the console should read off. Right? Anytime I move it, it just repeats in the console, off. If its on, or if its off, it only shows off. What in the world am I doing wrong? Please help! Thanks

  • Steve
    Steve over 14 years
    Hey thanks very much for your help. It made the UISwitch show up, which is great! But, it doesn't appear to be working fully, please see my edit above for the code. Thanks
  • Costique
    Costique over 14 years
    Sorry for the stupid mistake. I have fixed it (see Ben Gottlieb's comments).