代码之家  ›  专栏  ›  技术社区  ›  roryok

连接到数据源/委托时,uipikerview崩溃应用程序

  •  2
  • roryok  · 技术社区  · 16 年前

    从《头一代iPhone开发》这本书中,撕下我的头发,尝试运行最简单的教程。

    具有绑定到对象数组的UIPickerView。仅此而已。它不做任何事情,我现在想它做的就是显示数组中的内容。我已经按照书中逐字逐句的步骤重新启动了至少5次。在最近的尝试中,我发现了如何调试和添加一些断点。

    就应用程序而言

    - (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{
            return 1;
    }
    

    如果我将该行更改为返回0,应用程序将加载一个空的uipickerview。如果是一个,应用程序会在运行时爆炸。

    我已经验证了数组是否存在,并且有数据——如果pickerview设置为0 components,我可以将数组中的任何行返回到标签。只有当我通过ib将pickerview连接到数据源/委托时,它才会爆炸。

    真的,在我的束缚结束的时候,已经试着让它上下工作了几个星期,大约准备好扔出去的MacBook窗口和追捕史蒂夫乔布斯与十字弓。我觉得这是书中的第一个例子特别愚蠢。任何帮助都将不胜感激。

    更新:

    下面是代码

    - (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{
        return 1;
    }
    
    
    - (NSInteger)pickerView:(UIPickerView *) pickerViewnumberOfRowsInComponent :
      (NSInteger)component {
        return [activities count];
    }
    
    
    - (NSString *)pickerView:(UIPickerView *) titleForRow:(NSInteger)row forComponent:(NSInteger)component{
        return [activities objectAtIndex:row];
    }
    

    我刚尝试在“pickerviewnumberowsincononent”中插入一个空格,尽管 书中的空间。放入一个空格(如pickerview numberofrowsincomponent)可以使uipickerview工作,但它充满了问号而不是数组内容。这本书似乎有错别字。

    4 回复  |  直到 15 年前
        1
  •  2
  •   Richard    15 年前

    只是为了根据你的原始代码为每个人澄清…

    委托方法缺少空格:

    - (NSInteger)pickerView:(UIPickerView *) pickerViewnumberOfRowsInComponent: ...
    

    应该是:

    - (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent: ...
    

    您的另一个委托方法遗漏了参数名pikerview:

    - (NSString *)pickerView:(UIPickerView *) titleForRow: ...
    

    应该是:

    - (NSString *)pickerView:(UIPickerView *)pickerView titleForRow: ...
    
        2
  •  0
  •   Olie    16 年前

    您要添加此:

    - (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
    {
        return [activities objectAtIndex: row];
    }
    
        3
  •  0
  •   user152182    16 年前

    你需要在 pickerView numberOfRowsInComponent 在里面

    (nsinteger)pickerview:(uipickerview*)pickerviewnumberowsinconnent:(nsinteger)组件

    不幸的是,这是本书中的一个错别字,但这(连同其他的错误)被记录在第一个iphone开发勘误表的首页上。( http://oreilly.com/catalog/errata.csp?isbn=9780596803544 )很抱歉头疼,希望你喜欢这本书。——丹

        4
  •  -1
  •   user152182    16 年前

    刚刚看到你的更新-很高兴你发现了空间问题。至于问号,这(显然)不应该发生。您可以将代码发布到设置活动数组的位置吗?——丹