***由于未捕获的异常'NSInvalidArgumentException'而终止应用程序,无法识别的选择器发送到实例0x7fe318e825f0'

时间:2015-10-22 11:18:20

标签: xcode

      - (void)viewDidLoad
    {
        [super viewDidLoad];
       //array with image filenames and country names
        _paths = [[NSBundle mainBundle] pathsForResourcesOfType:@"png"   inDirectory:self.itemName];
        NSLog(@"%@",self.itemName);
        _country=_paths;
        for(int i=0;i <[_paths count];i++)
        {
            NSString *append=[_paths objectAtIndex:i];
            NSString *temp= [NSArray arrayWithObject:[[ append     componentsSeparatedByString:@"/"] lastObject]];
             NSString *temp1= [NSArray arrayWithObject:[[ append componentsSeparatedByString:@"-"] lastObject]];
            [_paths replaceObjectAtIndex:i withObject:temp];
            [_country replaceObjectAtIndex:i withObject:temp1];


        }

    // Connect data
       self.myPickerView.dataSource = self;
        self.myPickerView.delegate = self;
        self.maxIndex = [_country count];
        self.displayedFlagIndex = arc4random() % self.maxIndex;
    [self.ImageItem setImage:[UIImage imageNamed: _paths[self.displayedFlagIndex]]];
    UITapGestureRecognizer *tapGR = [[UITapGestureRecognizer alloc]
                                     initWithTarget:self action:@selector(tapHandler:)];
    tapGR.numberOfTapsRequired = 1;           // set appropriate GR attributes
    self.ImageItem.userInteractionEnabled = YES;
    [self.ImageItem addGestureRecognizer:tapGR];  // add GR to view


    self.myTextield.delegate=(id)self;


    }


    // set image displayed based on item selected
   // self.itemImage.image = [images objectAtIndex:self.itemNumber];


- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


- (void) tapHandler: (UITapGestureRecognizer *) sender
{

    _myTextield.text=@"";
    NSLog(@"in tapHandler");
    self.displayedFlagIndex = arc4random() % self.maxIndex;
    [self.ImageItem setImage:[UIImage imageNamed: _paths[self.displayedFlagIndex]]];
}

我正在尝试使用表视图和视图控制器来实现导航控制器。如果需要,将发布更多代码。 我已经检查过tapRecognizer方法调用tapHandler:方法而不是任何其他方法。但它仍然会抛出发送到实例的无法识别的选择器。 -Thanks!

1 个答案:

答案 0 :(得分:0)

您的问题是,_paths[self.displayedFlagIndex]会返回NSArray(包含一个对象)对象,而您希望NSString imageNamed:方法UIImage

这就是你收到错误的原因:

  

***由于未捕获的异常'NSInvalidArgumentException'而终止应用程序,原因:' - [__ NSArrayI length]:   无法识别的选择器发送到实例0x7fd563f3a140'

问题的根源:

NSString *temp = [NSArray arrayWithObject:[[append componentsSeparatedByString:@"/"] lastObject]];
NSString *temp1 = [NSArray arrayWithObject:[[append componentsSeparatedByString:@"-"] lastObject]];

这应该明确地发出一个警告,说你可以像NSStringNSArray那样混合

你应该这样做:

NSString *temp = [[append componentsSeparatedByString:@"/"] lastObject];
NSString *temp1 = [[append componentsSeparatedByString:@"-"] lastObject];