ios - 使用未声明的标识符 'imagePickerController'

标签 ios objective-c xcode

我在以下行的图像选择器 didFinishPickingMediaWithInfo 委托(delegate)中收到错误使用未声明的标识符“imagePickerController”:

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {

我也将协议(protocol)添加到了界面中:

@interface MyMediaController : UIViewController <UICollectionViewDelegate, UICollectionViewDataSource, UIImagePickerControllerDelegate, UINavigationControllerDelegate>

此外,我还添加了 MobileCoreServices.framework 二进制文件以与该二进制文件链接并按如下方式导入:

#import <MobileCoreServices/MobileCoreServices.h>

这就是我实例化 imagePicker 的方式:

- (IBAction)takePhotoButtonAction:(id)sender {
    if ([UIImagePickerController isSourceTypeAvailable: UIImagePickerControllerSourceTypeCamera]) {
        UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];
        imagePicker.delegate = self;
        imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera;
        imagePicker.mediaTypes = @[(NSString *) kUTTypeImage];
        imagePicker.allowsEditing = NO;
        [self presentViewController:imagePicker animated:YES completion:nil];
        self.isNewMedia = YES;
    }
}

关于为什么我收到此错误有什么想法吗?这很奇怪,因为这之前是有效的,但似乎没有特殊原因就停止了工作。

编辑: 这是错误行之前的代码:

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    return CGSizeMake(collectionView.bounds.size.width / 2 - 15, 150);
}

- (MyMediaCollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    MyMediaCollectionViewCell *cell = (MyMediaCollectionViewCell *)[collectionView dequeueReusableCellWithReuseIdentifier:@"MyMediaCollectionViewCell" forIndexPath:indexPath];
    AccountModel *account = [AccountModel getInstance];
    NSString *fileName = [[self.mediaResults objectAtIndex:indexPath.row] objectForKey:@"filename"];
    NSString *urlPath = [NSString stringWithFormat:@"https://s3-us-west-2.amazonaws.com/developd/premium_media/%@/thumb.%@", account._id, fileName];

    [NSURLConnection sendAsynchronousRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:urlPath]] queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) {
    UIImage *cellImage = [UIImage imageWithData:data];
    cell.imageView.image = cellImage;
    [cell setTag:indexPath.row];

    return cell;
}

#pragma mark UIImagePickerControllerDelegate

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
    NSString *mediaType = info[UIImagePickerControllerMediaType];

    [self dismissViewControllerAnimated:YES completion:nil];

    // ...
}

最佳答案

如果您在 didFinishPickingMediaWithInfo 之前的方法中有不匹配的大括号或括号,您可能会遇到这些神秘的错误。如果您选择所有代码,然后按 control-i(或选择“Editor”-“Structure”-“Re-Indent”),则可以识别缺少的大括号,然后当它重新缩进您的代码时,丢失的大括号会跳出来。

在这种情况下,是因为 sendAsynchronousRequest 方法的 completionHandler block 没有在前面的方法 cellForItemAtIndexPath 中终止。

关于ios - 使用未声明的标识符 'imagePickerController',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32546656/

相关文章:

objective-c - 如何通过 nsXml 解析器进行解析

ios - 尝试构建开源 Signal iOS 应用程序并连接到我自己的服务器

objective-c - 获取字符串而不是源代码 - Xcode Cocoa

swift - 随机选择 skspritenode 节点生成 5 个 Sprite

ios - 收到新消息时如何平滑更新聊天 View

objective-c - 类和 block 的生命周期

objective-c - 如何在 cocoa 中获取音频文件的比特率

objective-c - 多个类中的变量访问

ios - 将 APNs 从证书升级到身份验证 token 是否会使现有 deviceToken 失效?

ios - 关于 String 和 c strcpy 的快速问题