UISwitch is not working

时光毁灭记忆、已成空白 提交于 2019-12-04 21:46:00

I think the error is the following:

@interface SettingsViewController : UIViewController <UINavigationControllerDelegate>
{
    UISwitch *gravaSwitch;
    ...
}

@property (retain, nonatomic) IBOutlet UISwitch *gravaSwitch;
...
@end

IBOutlet placeholder has to be inserted into @property. Change your code and try to connect your outlet again.

Edit:

Try to create your UISwitch programatically. Change your @property as the following:

@property (retain, nonatomic) UISwitch *gravaSwitch;

Leave @synthesize as is. Then in your viewDidLoad method add your UISwitch (by default on property is 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];

Within the same controller, but outside the viewDidLoad method, add the following method:

- (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]
}

You could call self.gravaSwitch = nil; also in viewDidUnload method.

As an alternative you can set gravaSwicth to assign policy as follow. In this case you haven't to call self.gravaSwitch = nil; both in dealloc and/or viewDidUnload.

@property (assign, nonatomic) UISwitch *gravaSwitch;

Hope it helps.

Edit 2:

This code works for me. This is the implementation (.m file) for MyViewController.

@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");
    }
}

where gravaSwicth is declared within MyViewController.h as follows:

@interface MyViewController : UIViewController
{
     UISwitch *gravaSwitch;
     ...
}

@property (retain, nonatomic) UISwitch *gravaSwitch;
...
@end

rembember to call self.gravaSwicth = nil in dealloc in MyViewController.m!!

-(IBAction)alarmSwitchToggled:(id)sender
{


    if ([switchAlarm isOn]) {
        NSLog(@"switch is ON");
    }
    else
    {
                   NSLog(@"switch is OFF");


    }
    }

Give proper connections in nib file and dont forget to make it as valuechanged instead of touchupinside

FWIW I had this problem when my UISwitch was in a UITableViewCell, and I put it at the top of the list of subviews in Interface Builder (and therefore beneath the other views in the cell when running). It worked in a develop build but not a production build for who knows what reason. In prod, I would click on the UISwitch and the whole row would blink ask if selected.

So I moved it to the bottom of the list (and therefore above the other views in the cell when running) and then I could click on the UISwitch.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!