2

我有三个文本字段:名字、姓氏和年龄,还有一张照片。我该怎么做才能自动保存信息,这样我就不必单击按钮?

这是我的ViewController.h

#import <UIKit/UIKit.h>

@interface ContactViewController : UIViewController <UIImagePickerControllerDelegate, UINavigationControllerDelegate> {

    IBOutlet UIImageView *contactImageView;
    IBOutlet UITextField *firstNameTextField;
    IBOutlet UITextField *lastNameTextField;
    IBOutlet UITextField *ageTextField;
}

- (IBAction)save:(id)sender;
- (IBAction)chooseImage:(id)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)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

- (IBAction)save:(id)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)chooseImage:(id)sender {
    UIImagePickerController *picker = [[[UIImagePickerController alloc] init] autorelease];
    picker.delegate = self;
    picker.allowsEditing = YES;
    picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
    [self presentModalViewController:picker animated:YES];
}

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)image editingInfo:(NSDictionary *)editingInfo {
   contactImageView.image = image;

    [picker dismissModalViewControllerAnimated:YES];
}


- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker {

   [picker dismissModalViewControllerAnimated:YES];

}
4

2 回答 2

2

您将不得不使用<UITextFieldDelegate>协议

参考:UITextFieldDelegate 协议

你必须设置

textField.delegate = yourViewController

然后可以使用协议函数来处理文本视图的输入和字符更改。

例如你可以使用

- (BOOL)textFieldShouldReturn:(UITextField *)textField

当用户按下返回按钮时保存文本字段内容 =)

编辑:可能这种方法更适合您的需求 - 我昨天写帖子时有点累=)

- (void)textFieldDidEndEditing:(UITextField *)textField {
    //save your textfied.text string here :)
}
于 2012-04-05T16:18:55.723 回答
0

您需要在您的方法中将委托设置为UITextfield视图控制器,例如viewDidLoad

firstNameTextField.delegate = self;
lastNameTextField.delegate = self;
ageTextField.delegate = self;

你的视图控制器需要实现UITextFieldDelegateProtocol 方法:

- (void)textFieldDidEndEditing:(UITextField *)textField {
    //Do save Operations here
}

这将仅在文本完成编辑后保存。如果要在用户键入时保存,请使用

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
    NSString *stringToSave=[textField.text stringByReplacingCharactersInRange:range withString:string];
    //Identify text field by comparing textField with you text fields and do save operations
    return YES;
}
于 2012-04-06T06:21:32.647 回答