- (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];
}
NSUserDefault
最新推荐文章于 2020-04-28 17:19:01 发布