NSUserDefault

- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view, typically from a nib.
    [_activitySwitch addTarget:self action:@selector(toggleActivity:) forControlEvents:UIControlEventValueChanged];
    _firstName.delegate = self;
    _lastName.delegate = self;
    [self loadPersistentData:self];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(savePersistentData:) name:UIApplicationDidEnterBackgroundNotification object:nil];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark UITextField delegate
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    [textField resignFirstResponder];
    return YES;
}

#pragma mark UITextView delegte
- (BOOL)textViewShouldEndEditing:(UITextView *)textView
{
    [textView resignFirstResponder];
    return YES;
}

#pragma mark notification selector
- (void)savePersistentData:(id)sender
{
    NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
    [userDefaults setObject:_firstName.text forKey:@"firstName"];
    [userDefaults setObject:_lastName.text forKey:@"lastName"];
    [userDefaults setBool:_activitySwitch.isOn forKey:@"activityOn"];
    [userDefaults synchronize];
}

- (void)loadPersistentData:(id)sender
{
    NSUserDefaults  *userDefaults = [NSUserDefaults standardUserDefaults];
    self.firstName.text = [userDefaults objectForKey:@"firstName"];
    self.lastName.text = [userDefaults objectForKey:@"lastName"];
    [self.activitySwitch setOn:[userDefaults boolForKey:@"activityOn"]];
    if (_activitySwitch.isOn){
        if (![_activityIndicatorView isAnimating]) {
            [_activityIndicatorView startAnimating];
        }
    } else {
        if ([_activityIndicatorView isAnimating]) {
            [_activityIndicatorView stopAnimating];
        }
    }
}

#pragma mark utils
- (NSString *)currentContentFilePath
{
    NSArray *documents = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    return [NSString stringWithFormat:@"%@/Documents/%@",[documents objectAtIndex:0], _fileName.text];
}

- (void)showMsg:(NSString *)title message:(NSString *)msg
{
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:title message:msg delegate:self cancelButtonTitle:@"okay" otherButtonTitles: nil];
    [alert show];
    [alert release];
}

#pragma mark IBActions
- (IBAction)saveContent:(id)sender
{
    NSString *path = [self currentContentFilePath];
    NSFileManager *fileManager = [NSFileManager defaultManager];
    if ([fileManager fileExistsAtPath:path]) {
        [self showMsg:@"warning" message:@"file exist..."];
    } else {
        [fileManager createFileAtPath:path contents:[_textView.text dataUsingEncoding:NSUTF8StringEncoding] attributes:nil];
    }
}

- (IBAction)loadContent:(id)sender
{
    NSString *path = [self currentContentFilePath];
    NSFileManager *fileManager = [NSFileManager defaultManager];
    if ([fileManager fileExistsAtPath:path]) {
        NSError *error = nil;
        NSString *content = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:&error];
        if (error != nil) {
            NSLog(@"load failed: %@", error);
        } else {
            _textView.text = content;
        }
    } else {
        [self showMsg:@"waring" message:@"file not exits or wrong file's name"];
    }
}

- (IBAction)clearContent:(id)sender
{
    _textView.text = nil;
    NSString *path = [self currentContentFilePath];
    NSFileManager *fileManager = [NSFileManager defaultManager];
    if ([fileManager fileExistsAtPath:path]) {
        NSError *error = nil;
        [fileManager removeItemAtPath:path error:&error];
        if (error != nil) {
            NSLog(@"remove failed: %@", error);
        }
    }
}

- (IBAction)toggleActivity:(id)sender
{
    if (_activitySwitch.isOn) {
        if (!_activityIndicatorView.isAnimating) {
            [_activityIndicatorView startAnimating];
        }
    } else {
        if (_activityIndicatorView.isAnimating) {
            [_activityIndicatorView stopAnimating];
        }
    }
}
- (void)dealloc {
    [_fileName release];
    [_textView release];
    [_activitySwitch release];
    [_activityIndicatorView release];
    [_lastName release];
    [_firstName release];
    [super dealloc];
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值