Reputation: 63
I am currently working on an iPhone App with XCode 5.1.1. I'm only getting started with apps but things went pretty well until I recently hit a wall I can't figure out how to overcome.
On the first Viewcontroller that is shown I have two buttons to switch between languages. However everytime this viewcontroller is shown the buttons seem to be automatically "clicked" that is the functions that the buttons trigger are executed. I will attach the code from the .h and .m files here:
ViewController.h
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController
{
NSString *settingsTitle;
NSString *gerBtn;
NSString *engBtn;
}
@property (strong, nonatomic) IBOutlet UIButton *english;
@property (strong, nonatomic) IBOutlet UIButton *german;
@property (strong, nonatomic) IBOutlet UIButton *settings;
- (IBAction)setEnglish:(UIButton *)sender;
- (IBAction)setGerman:(UIButton *)sender;
@end
ViewController.m
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewWillAppear:(BOOL)animated
{
if([[NSUserDefaults standardUserDefaults] boolForKey:@"englishEnabled"]){
[_settings setTitle:@"Settings" forState:UIControlStateNormal];
} else {
[_settings setTitle:@"Einstellungen" forState:UIControlStateNormal];
}
NSLog(@"%hhd", [[NSUserDefaults standardUserDefaults] boolForKey:@"englishEnabled"]);
[super viewWillAppear:animated];
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (IBAction)setEnglish:(UIButton *)sender {
NSLog(@"setEnglishClicked");
[self enLangChange];
}
- (IBAction)setGerman:(UIButton *)sender {
NSLog(@"setGermanClicked");
[self deLangChange];
}
- (void) enLangChange
{
[[NSUserDefaults standardUserDefaults] setBool:true forKey:@"englishEnabled"];
[[NSUserDefaults standardUserDefaults] synchronize];
}
- (void) deLangChange
{
[[NSUserDefaults standardUserDefaults] setBool:false forKey:@"englishEnabled"];
[[NSUserDefaults standardUserDefaults] synchronize];
}
@end
Now everytime I navigate to this viewcontroller also when I start the app it will log "setEnglishClicked" and "setGermanClicked" and always set the BOOL false for the key englishEnabled. I've tried everything from deleting the buttons and putting them back in, even starting the whole project new from scratch but it always acted the same. The strange thing is that the other two buttons in this view for "Start" and "Settings" are not "clicked". It would help a ton if somebody could advice me what I'm doing wrong! Best Regards! J
Upvotes: 0
Views: 72
Reputation: 659
(IBAction)
is actually defined to (void)
in UINibDeclarations.h
.
Therefor, the methods - (IBAction)setEnglish:(UIButton *)sender
and - (IBAction)setGerman:(UIButton *)sender
are setters and are called when your view controller appears.
Rename them, for example to - (IBAction)didPressEnglishButton:(UIButton*)sender
and it should work.
Upvotes: 1