2009-10-03 51 views
0

开关按钮默认处于OFF状态。当用户选择ON状态时,将显示模态视图。这是我必须调用这个过程,但它不起作用。我有这个成立于笔尖,但我也能做到这一点编程只想混账东西的工作...使用开关按钮

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 

回答

0

你可能想以此来检测改变事件:

// 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]; 
    } 
} 

创建一个IBOutlet UISw痒在你的视图控制器中调用开关并在IB中挂钩。

UPDATE:

下面是我测试了一些实际代码:

#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 

希望有所帮助。

+0

感谢您的帮助,但那段代码无法正常工作。我错过了什么? – SympleMyne 2009-10-03 23:34:56

+0

对不起。单词'switch'是一个保留字。您需要将其更改为其他内容。我用sw。此外,您不会想要自动释放您的KeyPadViewController。相反,只要按照你已经做的那样分配/初始化它,然后在dealloc中释放它,因为它是一个ivar。让我知道当您尝试运行应用这些更改的代码时会发生什么。 – 2009-10-04 01:34:33

+0

马特(感谢您的帮助)我遇到的问题是我有我的起始页面,然后用户可以从那里选择一个按钮来打开设置页面(模态视图)。在设置页面中,用户选择开关打开PIN码页面(另一个模态视图)。我一直在试图实施这个简单的过程而自杀。我似乎无法得到这个权利。 – SympleMyne 2009-10-04 13:55:56