我有三个文本字段:名字、姓氏和年龄,还有一张照片。我该怎么做才能让信息自动保存,这样我就不必点击按钮了?
这是我的 ViewController.h :
#import <UIKit/UIKit.h>
@interface ContactViewController : UIViewController <UIImagePickerControllerDelegate, UINavigationControllerDelegate> {
IBOutlet UIImageView *contactImageView;
IBOutlet UITextField *firstNameTextField;
IBOutlet UITextField *lastNameTextField;
IBOutlet UITextField *ageTextField;
}
- (IBAction)saveid)sender;
- (IBAction)chooseImageid)sender;
@end
这是我的 ViewController.m :
- (void)viewDidLoad {
[super viewDidLoad];
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSString *frontName = [defaults objectForKey"firstname"];
NSString *lastName = [defaults objectForKey"lastname"];
int age = [defaults integerForKey"age"];
NSString *ageString = [NSString stringWithFormat"%i",age];
NSData *imageData = [defaults dataForKey"image"];
UIImage *contactImage = [UIImage imageWithData:imageData];
firstNameTextField.text = frontName;
lastNameTextField.text = lastName;
ageTextField.text = ageString;
contactImageView.image = contactImage;
}
- (void)viewDidUnload {
[contactImageView release];
contactImageView = nil;
[firstNameTextField release];
firstNameTextField = nil;
[lastNameTextField release];
lastNameTextField = nil;
[ageTextField release];
ageTextField = nil;
[super viewDidUnload];
}
- (BOOL)shouldAutorotateToInterfaceOrientationUIInterfaceOrientation)interfaceOrientation {
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
- (IBAction)saveid)sender {
[firstNameTextField resignFirstResponder];
[lastNameTextField resignFirstResponder];
[ageTextField resignFirstResponder];
// Create strings and integer to store the text info
NSString *frontName = [firstNameTextField text];
NSString *lastName = [lastNameTextField text];
int age = [[ageTextField text] integerValue];
UIImage *contactImage = contactImageView.image;
NSData *imageData = UIImageJPEGRepresentation(contactImage, 100);
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setObject:frontName forKey"firstname"];
[defaults setObject:lastName forKey"lastname"];
[defaults setInteger:age forKey"age"];
[defaults setObject:imageData forKey"image"];
[defaults synchronize];
NSLog(@"Data saved");
}
- (IBAction)chooseImageid)sender {
UIImagePickerController *picker = [[[UIImagePickerController alloc] init] autorelease];
picker.delegate = self;
picker.allowsEditing = YES;
picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
[self presentModalViewController:picker animated:YES];
}
- (void)imagePickerControllerUIImagePickerController *)picker didFinishPickingImageUIImage *)image editingInfoNSDictionary *)editingInfo {
contactImageView.image = image;
[picker dismissModalViewControllerAnimated:YES];
}
- (void)imagePickerControllerDidCancelUIImagePickerController *)picker {
[picker dismissModalViewControllerAnimated:YES];
}
Best Answer-推荐答案 strong>
您必须使用 协议(protocol)
引用:UITextFieldDelegate Protocol
你必须设置
textField.delegate = yourViewController
然后可以使用协议(protocol)函数来处理 TextView 的输入和字符更改。
例如你可以使用
- (BOOL)textFieldShouldReturnUITextField *)textField
在用户按下返回按钮时保存文本字段内容 =)
编辑:可能这种方法更适合您的需求 - 我昨天写帖子时有点累=)
- (void)textFieldDidEndEditing:(UITextField *)textField {
//save your textfied.text string here
}
关于ios - 在 NSStrings 中自动保存,我们在Stack Overflow上找到一个类似的问题:
https://stackoverflow.com/questions/10032088/
|