【问题标题】:Adding picture in UIPickerView在 UIPickerView 中添加图片
【发布时间】:2010-04-06 17:58:10
【问题描述】:

UIImageView *one = [[UIImageView alloc]initWithImage:[[UIImage alloc] initWithContentsOfFile:[[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"ball1.png"]]] ;

UIImageView *two = [[UIImageView alloc]initWithImage:[[UIImage alloc] initWithContentsOfFile:[[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"ball2.png"]]] ;
UIImageView *three = [[UIImageView alloc]initWithImage:[[UIImage alloc] initWithContentsOfFile:[[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"ball3.png"]]] ;

arrayBalls = [[NSArray alloc]initWithObjects:one,two,three,nil];

我使用上面的代码在 UIPickerView 中显示图像。但是执行此代码时应用程序崩溃了。

请大家帮忙。

【问题讨论】:

    标签: iphone objective-c cocoa-touch xcode


    【解决方案1】:

    你遇到了什么错误,看看堆栈跟踪,等等......

    如果没有这些信息,就很难诊断出您的问题,但是看起来您的代码充满了漏洞。我的猜测是您没有遵循正确的内存管理技术并且没有适当地发布内容。还有一种更简单的方法来做你想做的事情:

    UIImageView *one = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"ball1.png"]]
    ...
    NSArray* ballsArray = [NSArray arrayWithObjects:one,two,three,nil];
    [one release];
    [two release];
    [three release];
    

    确保将图像添加到项目中(右键单击 > 添加 > 添加现有文件)

    查看有关内存管理的 CS139P 讲座。

    W.R.T.你的评论: 当您没有正确保留对象然后尝试向其发送消息时,就会发生您遇到的错误类型。

    你的代码可能看起来像这样

    @interface SomeObject : NSObject{
       NSString* aString;
    }
    
    @implementation SomeObject
    
    -(void)someMethod{
        aString = [NSString stringWithString:@"A String"];
        ...
    }
    -(void)someOtherMethod{
        [aString isEqualToString:@"A String"];
        ...
    }
    

    如果你不明白为什么会失败,你真的需要回去看看内存管理是如何与 iphone 一起工作的。

    请从 iTunes U 下载并收听 CS193P 内存管理讲座,对我有帮助,对你也有帮助。

    【讨论】:

    • 我收到以下错误请帮助 *** 由于未捕获的异常 'NSInvalidArgumentException' 导致应用程序终止,原因:'*** -[UIImageView isEqualToString:]: unrecognized selector sent to instance 0x4a21df0'跨度>
    【解决方案2】:

    我在pickerView方面遇到了和你一样的问题,我已经解决了。

    首先,您尝试将图片添加到您的选择器视图中,并且您必须覆盖选择器视图的委托方法。

    我猜你已经使用以下委托方法从选取器视图中获取图片:

    (NSString *)pickerView: (UIPickerView *)pickerView titleForRow: (NSInteger)row forComponent (NSInteger)component
    

    请注意,上面的函数返回的是 NSString 对象,但是你的选择器视图必须返回 UIImageView。因此,Xcode 会通知一个模棱两可的错误:-[UIImageView isEqualToString:]: unrecognized selector sent to instance 0x4a21df0' (UIImageView is not equal to NSString)

    解决方案是你必须使用picker view的另一个委托方法:

    (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view
    

    问题解决了! 如果您的情况与我不同,请更详细地说明您的问题。 ^^

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-01-10
      相关资源
      最近更新 更多