开关按钮有问题

时间:2009-10-03 16:01:58

标签: iphone uikit

默认情况下,“切换”按钮处于“关闭”状态。当用户选择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

1 个答案:

答案 0 :(得分: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];
    }
}

在视图控制器中创建一个名为 switch 的IBOutlet UISwitch,并将其挂接到IB中。

更新:

以下是我测试的一些实际代码:

#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

希望有所帮助。