views:

447

answers:

2

I am currently using the following code in the AppDelegate to make a UIBarButtonItem act as a switch and adjusts a BOOL variable in the RootViewController Class:

- (void)applicationDidFinishLaunching:(UIApplication *)application {    
...
toggleSwitch = [[UIBarButtonItem alloc] initWithTitle:@"English" style:UIBarButtonItemStylePlain target:self action:@selector(togglePress:)]; 
...
}

And then the selector comes later in the AppDelegate:

-(IBAction) togglePress:(id)sender {
RootViewController *viewController;

if (viewController.searchEnglish) {
 viewController.searchEnglish = NO;
 toggleSwitch.title = @"English";

}
else {
 viewController.searchEnglish = YES;
 toggleSwitch.title = @"OtherLanguage";
}

}

I basically check to see if the BOOL from the RootViewController class is set to YES and if it is then set it to NO and change the title of the button to 'OtherLanguage,' otherwise do the opposite. However when I run the program and click the button,the BOOL variable and the title do not change, the application freezes and crashes. What am I doing wrong?

A: 

You're allocating a new viewController, setting the toggle, then releasing it. Essentially, you're doing nothing. To accomplish what (I think) you're trying to do, remove the release and allocation, and set viewController to be the actual view controller displaying the toggle.

Dan Lorenc
When doing this the program seems to crash when I press the button.
Kulpreet
I have adjusted the question
Kulpreet
A: 

You are not setting viewController t o anything in the code you have displayed, your rootviewController is null...so ofcourse the app will crash

Daniel
'objC_msgSend' shows up on the top bar and the console does not display anything just the regular gdb message
Kulpreet
read my edit...
Daniel
you are not setting the viewController declared there to anything, you want to keep your viewcontroller as a member of the appDelegate Class...
Daniel
Thanks! That worked great.
Kulpreet