Reputation:
So I have a UISwitch
:
UISwitch* statusBarSwitch = [[UISwitch alloc]initWithFrame:CGRectMake(254, 65, 0, 0)];
[statusBarSwitch addTarget:self action:@selector(statusBarSwitchChanged) forControlEvents:UIControlEventValueChanged];
[[self view]addSubview:statusBarSwitch];
[statusBarSwitch setOn:YES animated:NO];
And this is the method for the UISwitch:
-(void)statusBarSwitchChanged {
if (statusBarSwitch.on) {
[[UIApplication sharedApplication] setStatusBarHidden:YES withAnimation:UIStatusBarAnimationSlide];
}else{
[[UIApplication sharedApplication] setStatusBarHidden:NO withAnimation:UIStatusBarAnimationSlide];
}
}
And now, when I run it I get the error 'use of undeclared identifier' on statusBarSwitchChanged
.
Someone please help me, I dont know what I did wrong.
Upvotes: 0
Views: 1317
Reputation: 318774
Your error is happening when you try to compile, not when you try to run.
Why do you expect your code to work as-is? There is no such variable as statusBarSwitch
in your statusBarSwitchChanged
method.
There is a such a local variable in the first bit of code you posted but that variable isn't visible outside of the curly braces it is declared in.
You have two choices to fix this.
The first is to change the selector and method as follows:
UISwitch* statusBarSwitch = [[UISwitch alloc]initWithFrame:CGRectMake(254, 65, 0, 0)];
// Note the added colon to the selector name
[statusBarSwitch addTarget:self action:@selector(statusBarSwitchChanged:) forControlEvents:UIControlEventValueChanged];
[[self view]addSubview:statusBarSwitch];
[statusBarSwitch setOn:YES animated:NO];
- (void)statusBarSwitchChanged:(UISwitch *)sender {
if (sender.on) {
[[UIApplication sharedApplication] setStatusBarHidden:YES withAnimation:UIStatusBarAnimationSlide];
} else {
[[UIApplication sharedApplication] setStatusBarHidden:NO withAnimation:UIStatusBarAnimationSlide];
}
}
The above change ensures that the switch that was used is passed to the action method you setup.
The second option is to create an instance variable to keep a reference to the UISwitch
. Then you can access the switch from any instance method in your class.
Upvotes: 2