UISwitch不工作

首先是我的.h文件:

@interface SettingsViewController : UIViewController  { IBOutlet UISwitch *gravaSwitch; ... } @property (retain, nonatomic) UISwitch *gravaSwitch; ... @end 

我在.m文件中的viewDidload (它可以工作):

 ... // SET SWITCH BUTTON STATE keychain = [[KeychainItemWrapper alloc] initWithIdentifier:@"TrafficApp" accessGroup:nil]; if ( [[keychain objectForKey:(id)kSecAttrAccount] isEqualToString:@"" ] ) [self.gravaSwitch setOn:FALSE]; else [self.gravaSwitch setOn:TRUE]; ... 

但我的switchChanged不起作用,我不知道为什么。 在IB上,一切都正确连接,它进入此方法但gravaSwitch始终为空。

 - (IBAction)switchChanged:(id)sender { if ( self.gravaSwitch.on ) { NSLog(@"IF"); [self.gravaSwitch setOn:FALSE animated:YES]; } else { NSLog(@"ELSE"); [self.gravaSwitch setOn:TRUE animated:YES]; } } 

问候。

我认为错误如下:

 @interface SettingsViewController : UIViewController  { UISwitch *gravaSwitch; ... } @property (retain, nonatomic) IBOutlet UISwitch *gravaSwitch; ... @end 

必须将IBOutlet占位符插入@property。 更改您的代码并尝试再次连接您的sockets。

编辑:

尝试以编程方式创建UISwitch。 按以下方式更改@property

 @property (retain, nonatomic) UISwitch *gravaSwitch; 

请保留@synthesize 。 然后在你的viewDidLoad方法中添加你的UISwitch(默认情况下属性为FALSE):

 // Width and height are set to zero but they take the default dimension UISwitch *yourSwitch = [[UISwitch alloc] initWithFrame:CGRectMake(100, 100, 0, 0)]; // add target and action mySwitch addTarget:self action:@selector(yourCustomAction:) forControlEvents:UIControlEventValueChanged]; self.gravaSwitch = yourSwitch; // don't forget because gravaSwitch has already a retain policy [release yourSwitch]; // add the switch to the view [self.view addSubview:self.gravaSwitch]; 

在同一个控制器中,但在viewDidLoad方法之外,添加以下方法:

 - (void)yourCustomAction:(id)sender { if ( self.gravaSwitch.on ) { NSLog(@"IF"); [self.gravaSwitch setOn:FALSE animated:YES]; } else { NSLog(@"ELSE"); [self.gravaSwitch setOn:TRUE animated:YES]; } } - (void)dealloc { self.gravaSwitch = nil; // remember to dealloc the switch!! [super dealloc] } 

你可以打电话给self.gravaSwitch = nil; 也在viewDidUnload方法中。

作为替代方案,您可以设置gravaSwicth以分配策略如下。 在这种情况下,您不必调用self.gravaSwitch = nil;dealloc和/或viewDidUnload

 @property (assign, nonatomic) UISwitch *gravaSwitch; 

希望能帮助到你。

编辑2:

这段代码适合我。 这是MyViewController的实现(.m文件)。

 @synthesize gravaSwitch; - (void)viewDidLoad { [super viewDidLoad]; UISwitch *yourSwitch = [[UISwitch alloc] initWithFrame:CGRectMake(100, 100, 0, 0)]; [yourSwitch addTarget:self action:@selector(yourCustomAction:) forControlEvents:UIControlEventValueChanged]; self.gravaSwitch = yourSwitch; [yourSwitch release]; [self.view addSubview:self.gravaSwitch]; } - (void)yourCustomAction:(id)sender { if(self.gravaSwitch.on) { NSLog(@"on"); } else { NSLog(@"off"); } } 

其中gravaSwicth在MyViewController.h中声明如下:

 @interface MyViewController : UIViewController { UISwitch *gravaSwitch; ... } @property (retain, nonatomic) UISwitch *gravaSwitch; ... @end 

rembember在MyViewController.m中调用self.gravaSwicth = nil inallall!

 -(IBAction)alarmSwitchToggled:(id)sender { if ([switchAlarm isOn]) { NSLog(@"switch is ON"); } else { NSLog(@"switch is OFF"); } } 

在nib文件中给出适当的连接,不要忘记将其作为valuechanged而不是touchupinside

当我的UISwitch在UITableViewCell中时,我遇到了这个问题,我将它放在Interface Builder中的子视图列表的顶部(因此在运行时在单元格中的其他视图之下)。 它在开发构建中工作,但不是生产构建,谁知道是什么原因。 在prod中,我会点击UISwitch,整行会闪烁询问是否选中。

所以我将它移动到列表的底部(因此在运行时在单元格中的其他视图之上)然后我可以单击UISwitch。