Creating a UISwitch Programmatically

Steve picture Steve · Jan 8, 2010 · Viewed 58.9k times · Source

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

Answer

Ben Gottlieb picture Ben Gottlieb · Jan 8, 2010

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");
}