30

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
  • 628
  • 1
  • 10
  • 18
  • Why not use IB? And if you are not using it, then why are things declared as IBOutlet and IBAction? – gerry3 Jan 08 '10 at 06:41
  • Wow. Great point. I actually would love to use IB, but as I mentioned, I am trying to get this to work on a view that isn't using IB. Is there an easy way to add a xib file to a screen that is just populating a uitableview and showing cells of information? Thanks – Steve Jan 08 '10 at 06:50

2 Answers2

31

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");
}
Venk
  • 5,949
  • 9
  • 41
  • 52
Ben Gottlieb
  • 85,404
  • 22
  • 176
  • 172
15

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];
Venk
  • 5,949
  • 9
  • 41
  • 52
Costique
  • 23,712
  • 4
  • 76
  • 79
  • 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 – Steve Jan 08 '10 at 16:08
  • Sorry for the stupid mistake. I have fixed it (see Ben Gottlieb's comments). – Costique Jan 08 '10 at 20:26