SEGUE无法正常工作



我试图让我的"主视图"允许用户选择照片并将其显示在我的"第二视图"上。到目前为止,按下按钮时,它允许用户选择照片并将其设置为imageView,但是当我尝试使其转到其他视图时,我会遇到问题。这是我选择用户选择按钮的代码,而底线是我试图使用视图的方法。

- (IBAction)usePhotos:(id)sender {
    UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];
    imagePicker.delegate = self;
    imagePicker.sourceType =
    UIImagePickerControllerSourceTypePhotoLibrary;
    imagePicker.mediaTypes = @[(NSString *) kUTTypeImage];
    imagePicker.allowsEditing = YES;
    [self presentViewController:imagePicker animated:YES completion:nil];
    _newMedia = NO;
    [self performSegueWithIdentifier:@"switch" sender:self];
}

我也尝试了不同的方式,它们也没有工作,所以我很困惑。预先感谢您!

据我了解,您需要首先拿起图像。一旦有一个,您就想在第二视图上显示它。如果正确的话,您的代码将无法正常工作,因为您会介绍图像选择器,并且您希望同时执行segue。尝试移动行:

[self performSegueWithIdentifier:@"switch" sender:self];

to

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
    UIImage *image = info[UIImagePickerControllerEditedImage];
    // If you want to dismiss this view animated you should perform segue with delay (0.5 should be enough)
    [picker dismissViewControllerAnimated:NO completion:NULL];
    // You pass an image as a parameter so you can access it in prepareForSegue: method and you can pass it to destination view
    [self performSegueWithIdentifier:@"switch" sender:image];
}

最新更新