【问题标题】:Trying to pass array of strings to destination view controller试图将字符串数组传递给目标视图控制器
【发布时间】:2013-06-12 16:44:14
【问题描述】:

在我的应用程序中,我已经解析了 Collectionview 单元格中的图像,当按下到具有较大图像的另一个屏幕时,这些单元格会继续。这工作正常。但是,当我尝试将包含字符串数组的标签添加到同一个目标视图控制器时,我得到:

由于未捕获的异常“NSInvalidArgumentException”而终止应用,原因:“-[__NSCFString objectAtIndex:]: unrecognized selector sent to instance 0xaaa7520”

每个数组,图像和字符串都有相同数量的索引。

在第一个视图控制器中

-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([segue.identifier isEqualToString:@"BottomSegue"]) {
    //DetailShowImageViewController *detailedVC = segue.destinationViewController;
    //detailedVC.detailImageView  =sender;


    NSArray *indexPaths = [self.bottomCollectionview indexPathsForSelectedItems];
    NSLog(@"%@", indexPaths);
    BottomCollectionDetail *destinationVC =segue.destinationViewController;
    NSIndexPath *indexPath = [indexPaths objectAtIndex:0];
    NSLog(@"%@", indexPath);

    destinationVC.imageFromArray  = [bottomArray objectAtIndex:indexPath.row];
    destinationVC.string = [arrayOfTitles objectAtIndex:indexPath.row];
    [self.bottomCollectionview deselectItemAtIndexPath:indexPath animated:YES];


}

destinationVC.h

@property (weak, nonatomic) IBOutlet UILabel *labelOne;
@property (weak, nonatomic) IBOutlet UIImageView *imageViewBCD;
@property(weak, nonatomic) NSString *string;
@property(weak, nonatomic)UIImage *imageFromArray;

destinationVC.m

- (void)viewDidLoad
{
    [super viewDidLoad];
// Do any additional setup after loading the view.
    self.imageViewBCD.image = _imageFromArray;
    self.labelOne.text = _string; //This is the line of code that is causing exception
}

我的问题是试图让标签显示字符串,即抛出异常。无论如何我可以解决这个问题吗?

【问题讨论】:

  • arrayOfTitles的内容是什么?
  • 您确定问题出在您的标签上吗?
  • arrayOfTitles 包含已解析的字符串。字符串显示在原始视图控制器的 NSLog 中。
  • 试试这个self.labelOne.text = [NSString stringWithFormat:@"%@",_string]
  • 你的 arrayOfTitles 是一个 NSString 而不是一个 NSArray

标签: ios arrays string segue


【解决方案1】:

NSInvalidArgumentException', reason: '-[__NSCFString objectAtIndex:]: unrecognized selector sent to instance 0xaaa7520' 很容易解释,它与发布无关,问题是您的arrayOfTilesNSString 而不是NSArray。检查您创建/分配arrayOfTiles 的所有位置。

【讨论】:

    【解决方案2】:

    检查您的arrayOfTitles,在某些地方它是released。检查那部分代码。但是您可以尝试将这两个array 设置为strong 属性。

    【讨论】:

    • 使 arrayOfTitles 成为一个强属性,但仍然绘制异常。
    • 你有没有在添加对象的地方检查过数组,试试这个:当所有对象都添加到数组中时,用日志检查一次。
    • 你也可以尝试使用 ViewWillAppear 而不是 viewDidLoad
    • 对象就位。问题似乎出在 prepareForSegue 方法中
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-12-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多