tags:

views:

138

answers:

2

The Switch button by default is in the OFF state. When the user selects the ON state a modal view will display. This is what I have to invoke the process but its not working. Please help. I have this set up in a nib but I can also do it programatically just want the darn thing to work...Thanks!

MyViewController.h

- (IBAction)offSwitchChange:(id)sender;

@end

MyViewController.m

- (IBAction)offSwitchChange:(id)sender
{
    if (self.myKeyPadViewController == nil)
        self.myKeyPadViewController = [[[KeyPadViewController alloc] initWithNibName:
               NSStringFromClass([KeyPadViewController class]) bundle:nil] autorelease];

    [self.navigationController presentModalViewController:self.myKeyPadViewController animated:YES];
}

@end
A: 

You might want to detect the change event like this instead:

// Do this in viewDidLoad
[switch addTarget:self action:@selector(switched) forControlEvents:UIControlEventValueChanged];

// Then implement it like this
- (void)switched;
{
    if ([switch isOn])
    {
        if (self.myKeyPadViewController == nil)
            self.myKeyPadViewController = [[[KeyPadViewController alloc] init] autorelease];

        [self.navigationController presentModalViewController:self.myKeyPadViewController animated:YES];
    }
}

Create an IBOutlet UISwitch called switch in your view controller and hook it up in IB.

UPDATE:

Here is some actual code I tested:

#import "SwitchResponderViewController.h"

@implementation SwitchResponderViewController


- (void)viewDidLoad {
    [super viewDidLoad];
    [sw addTarget:self action:@selector(switched) forControlEvents:UIControlEventValueChanged];
}

- (void)dealloc {
    [secondController release], secondController = nil;
    [super dealloc];
}

- (void)switched;
{
    if ([sw isOn])
    {
        NSLog(@"On");
        if (!secondController)
            secondController = [[SecondViewController alloc] init];

        [self presentModalViewController:secondController animated:YES];
    }
    else
    {
        NSLog(@"Off");
    }

}

@end

Hope that helps.

Matt Long
Thanks for the help, but that piece of code did not work. What am I missing?
SympleMyne
Sorry about that. The word 'switch' is a reserved word. You will need to change it to something else. I used sw. Also, you will not want to autorelease your KeyPadViewController. Instead, just alloc/init it the way you're already doing it and then release it in your dealloc since it's an ivar. Let me know what happens when you try to run the code having applied these changes.
Matt Long
Matt (Thanks for the help) the problem I'm having is that I have My start page, then the user can select a button from there to open a settings page (Modal View). From the settings page the user selects the switch to turn on the PIN code page (another Modal View). I have been killing myself trying to implement this simple process. I seem to can not get this right.
SympleMyne
Try to run this demo project and see if you can find the difference between your code and mine. Hope it helps: http://www.matthew-long.com/download/SwitchResponder.zip
Matt Long
A: 

This what I have...

import

import "KeyPadViewController.h"

@interface ModalViewController : UIViewController { IBOutlet UISwitch *switch; KeyPadViewController *myModalViewController; }

@property (nonatomic, retain) IBOutlet UISwitch *switch; @property (nonatomic, retain) KeyPadViewController *myKeyPadViewController;

  • (IBAction)dismissAction:(id)sender;

@end


import "ModalViewController.h"

import "KeyPadViewController.h"

@implementation ModalViewController

@synthesize myKeyPadViewController;

  • (void)viewDidLoad { [super viewDidLoad]; [switch addTarget:self action:@selector(switched) forControlEvents:UIControlEventValueChanged];

}

  • (void)dealloc { //if (self.myKeyPadViewController != nil) //[myKeyPadViewController release]; [super dealloc]; }

  • (void)viewDidUnload { }

  • (IBAction)dismissAction:(id)sender { [self.parentViewController dismissModalViewControllerAnimated:YES]; }

  • (void)switched; { if ([switch isOn]) { if (self.myKeyPadViewController == nil) self.myKeyPadViewController = [[[KeyPadViewController alloc] init] autorelease];

    [self.navigationController presentModalViewController:self.myKeyPadViewController animated:YES];
    

    } }

@end

SympleMyne