Hello -
I am a former iOS BNR student, finished this past July ('12) and I am now attempting to create my first Mac OSX App. I am doing pretty well, its my first app. I am running into a few issues with my app and was wondering if someone could help me identify why I am getting random crashes after certain tasks and advice on how to fix them.
Preference Window Bug
I have a preference window and I can launch it, once I close it I can not launch it again. The “Preferences” option is greyed out in the app menu and all functionality of my app stops working (NSSearchbar, buttons, etc…) If you wait long enough the app will just crash entirely. I think its something to do with how the preferences are saved but I am not sure. Here is my code for the preference controller. Some of the preferences get saved when the preference window is closed but some do not. (Checkbox and Popup Button do, NSTextField does not).
I do not know if I am looking in the right place or not and if there is a better way to share my entire project please let me know.
Thanks!!
Jon Brown
iOS BNR Alumni (July '12)
#import "GeneralPreferencesViewController.h"
@implementation GeneralPreferencesViewController
// General preference window stuff here.
-(BOOL)checkInternet
{
NSURL *url = [[NSURL alloc] initWithString:@"http://www.google.com"];
NSURLRequest *request = [[NSURLRequest alloc] initWithURL:url];
if ([NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil])
return TRUE;
return FALSE;
}
- (void) awakeFromNib
{
NSUserDefaults* defaults = [NSUserDefaults standardUserDefaults];
NSString* memberpref = [defaults objectForKey:@"member-name"];
NSString* checkpref = [defaults objectForKey:@"help-tips"];
// Read default prefrences for member name
if ([memberpref isEqual: @""]) {
[memberName setStringValue:@"Please enter a valid Email Address"];
} else {
[memberName setStringValue:memberpref];
}
// Read default prefrences for help checked
if ([checkpref isEqual: @"unchecked"]) {
[helpCheck setState:NSOffState];
} else if ([checkpref isEqual: @"checked"]) {
[helpCheck setState:NSOnState];
}
// Check internet connection
if ([self checkInternet]) {
NSLog(@"Online");
NSImage *onlineInd = [NSImage imageNamed:@"IndicatorOn.png"];
[onlineWell setImage: onlineInd];
[onlineText setStringValue:@"Online"];
} else {
NSLog(@"Offline");
NSImage *onlineInd = [NSImage imageNamed:@"IndicatorCritical.png"];
[onlineWell setImage: onlineInd];
[onlineText setStringValue:@"Offline"];
}
}
- (id)init
{
return [super initWithNibName:@"GeneralPreferencesView" bundle:nil];
}
#pragma mark -
#pragma mark MASPreferencesViewController
- (NSString *)identifier
{
return @"GeneralPreferences";
}
- (NSImage *)toolbarItemImage
{
return [NSImage imageNamed:NSImageNamePreferencesGeneral];
}
- (NSString *)toolbarItemLabel
{
return NSLocalizedString(@"General", @"Toolbar item name for the General preference pane");
}
- (IBAction)checkedOperation:(id)sender
{
NSUserDefaults* defaults = [NSUserDefaults standardUserDefaults];
// Set default prefrences for help checked
long state = [helpCheck state];
if (state == 1) {
[defaults setObject:@"checked" forKey:@"help-tips"];
} else {
[defaults setObject:@"unchecked" forKey:@"help-tips"];
}
}
- (IBAction)setMembername:(id)sender
{
NSUserDefaults* defaults = [NSUserDefaults standardUserDefaults];
NSString *memberNamed = [memberName stringValue];
// Set default prefrences for Member Name
[defaults setObject:memberNamed forKey:@"member-name"];
}
@end