应用程序结构:uitabbar 控制器中的 4 个视图控制器。
情况:视图控制器之一实际上是带有添加和编辑按钮的 uitableview 控制器。添加新对象会推送另一个视图,该视图具有文本字段和选择或拍照的选项。选择图像就可以正常工作。
问题:如果不是选择图像,而是按下使用按钮使用相机,那么就会出现问题。图像被返回并且相机(uiimagepickercontroller)被关闭,这也很好。但是文本字段现在是空的(第一个奇怪的事情)!再次输入一些值并保存将返回到 uitablview。现在有 2 行具有相同的值,这是第二个奇怪的事情。
问题:uiimagepicker 控制器如何删除所有文本字段值?为什么在使用库中的图像时它不会发生?
它如何复制 ui 表视图中的数据条目?
信息:我正在使用 ARC。当 viewDidUnload/WillDisappear 时,我没有将文本字段设置为 nil。
代码:
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
[picker dismissModalViewControllerAnimated:YES];
self.image.image=[info objectForKey:UIImagePickerControllerEditedImage];
NSLog(@"did load the image %@", self.image.image);
}
-(void) imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)image editingInfo:(NSDictionary *)editingInfo
{
[picker dismissModalViewControllerAnimated:YES];
self.image.image=image;
}
-(void) imagePickerControllerDidCancel:(UIImagePickerController *)picker
{
[picker dismissModalViewControllerAnimated:YES];
}
- (IBAction)takePic:(id)sender {
[self.name resignFirstResponder];
[self.info resignFirstResponder];
[self.lastname resignFirstResponder];
UIActionSheet *sheet=nil;
if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera])
sheet=[[UIActionSheet alloc]initWithTitle:nil delegate:self cancelButtonTitle:nil destructiveButtonTitle:@"Cancel" otherButtonTitles:@"Choose Photo",@"Take Photo", nil];
else
sheet=[[UIActionSheet alloc]initWithTitle:nil delegate:self cancelButtonTitle:nil destructiveButtonTitle:@"Cancel" otherButtonTitles:@"Choose Photo", nil];
[sheet showInView:self.parentViewController.tabBarController.view];
}
-(void) actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
if (buttonIndex==0) return;
UIImagePickerController *takepic=[[UIImagePickerController alloc] init];
takepic.delegate=self;
takepic.allowsEditing=YES;
if (buttonIndex==1) {
if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeSavedPhotosAlbum]) {
takepic.sourceType=UIImagePickerControllerSourceTypeSavedPhotosAlbum;
}
}
if(buttonIndex==2) {
if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera])
{
takepic.sourceType=UIImagePickerControllerSourceTypeCamera;
NSLog(@"entered camerea mode");
}
}
[self presentModalViewController:takepic animated:YES];
}
编辑:通过在 viewDidLoad 上放置断点,我发现了一些“有趣”的东西。如果 uiimage 选择器源是照片库。按下使用按钮返回视图控制器后不会再次触发 viewDidLoad。但如果 uiimagepicker 控制器源是相机,它将触发 viewDidLoad。这似乎是问题的根源。如何处理?