无法解决带有图像的双选择器

发布于 2025-01-04 21:08:01 字数 1605 浏览 1 评论 0原文

我正在尝试做一个双重选择器,一部分是文本,另一部分是图像。但代码给了我一个错误:线程1:程序收到信号:“EXC_BAD_ACCESS”。我看不出其中的麻烦。这是代码,Array 内容图像和 Array2 内容文本。谢谢。

@synthesize Array, picker, Array2;

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
}

#pragma mark - View lifecycle

- (void)viewDidLoad
{
    UIImage *one = [UIImage imageNamed:@"6-12AM.png"];
    UIImageView *oneView = [[UIImageView alloc] initWithImage:one];
    NSArray *Array1 = [[NSArray alloc] initWithObjects:oneView, nil];
    NSString *fieldName = [[NSString alloc] initWithFormat:@"Array"];
    [self setValue:Array1 forKey:fieldName];
    Array2 = [[NSArray alloc] initWithObjects:@"Hello", @"trouble", nil];
    [super viewDidLoad];

}

- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView;
{

        return 2;// giving number of components in PickerView

    }

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:    (NSInteger)component;
{

    return [self.Array2 count];// counting the number of rows in each component

}

#pragma mark Picker Delegate Methods

- (UIView *)pickerView:(UIPickerView *)pickerView 
            viewForRow:(NSInteger)row
          forComponent:(NSInteger)component 
           reusingView:(UIView *)view 
{
    if (component == 1) {


        return [self.Array objectAtIndex:row];
    }
}   //In this line is where the error occurs

- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row     forComponent:(NSInteger)component; {
    if (component == 0) {
        return [self.Array2 objectAtIndex:row];
    }
}

I'm trying to do a double picker, one part of text and the other with images. But the code gives me an error: Thread 1: Program received signal: "EXC_BAD_ACCESS". I can't see the trouble. Here is the code, Array content Images and Array2 content text. Thanks.

@synthesize Array, picker, Array2;

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
}

#pragma mark - View lifecycle

- (void)viewDidLoad
{
    UIImage *one = [UIImage imageNamed:@"6-12AM.png"];
    UIImageView *oneView = [[UIImageView alloc] initWithImage:one];
    NSArray *Array1 = [[NSArray alloc] initWithObjects:oneView, nil];
    NSString *fieldName = [[NSString alloc] initWithFormat:@"Array"];
    [self setValue:Array1 forKey:fieldName];
    Array2 = [[NSArray alloc] initWithObjects:@"Hello", @"trouble", nil];
    [super viewDidLoad];

}

- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView;
{

        return 2;// giving number of components in PickerView

    }

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:    (NSInteger)component;
{

    return [self.Array2 count];// counting the number of rows in each component

}

#pragma mark Picker Delegate Methods

- (UIView *)pickerView:(UIPickerView *)pickerView 
            viewForRow:(NSInteger)row
          forComponent:(NSInteger)component 
           reusingView:(UIView *)view 
{
    if (component == 1) {


        return [self.Array objectAtIndex:row];
    }
}   //In this line is where the error occurs

- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row     forComponent:(NSInteger)component; {
    if (component == 0) {
        return [self.Array2 objectAtIndex:row];
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

樱&纷飞 2025-01-11 21:08:01

您的错误是由于您没有提供 if 语句的替代方案。非 void 函数在每种情况下都必须返回某些内容,因此您需要在两个函数中分别提供一个返回值(如 nil)的 else 语句。

Your error is due to the fact that you do not offer an alternative to the if statement. A non-void function must return something in every case, so you need to provide an else statement in each one of your two functions that returns a value (like nil).

┼── 2025-01-11 21:08:01

我没有看到您分配或初始化数组。因此访问不好。您的意思是使用Array1吗?

还有几件事:

  1. 分配了几个位置的内存从未被释放。 开头
  2. 变量名称应以小写字母
  3. ,例如 array1、array2
     [self setValue:Array1 forKey:fieldName]; //这是在做什么?
    

I dont see you allocating or initializing Array. Hence the bad access. Did you mean to use Array1?

Also several things:

  1. Memory allocated several places never deallocated. Leaks all over
  2. Variable names should start with small caps like array1, array2
  3.  [self setValue:Array1 forKey:fieldName];   //what is this doing?
    
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文