开发者

My Simple Array Is Not Working

The app is freezing whe开发者_如何学Gon the array is being loaded.

Getting Error at NSString *weight line:

*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[NSCFString stringValue]: unrecognized selector sent to instance 0x14904'

Here is my code:

- (IBAction)weightButtonPressed
{
pickerArrayHalf = [[NSMutableArray alloc]initWithCapacity:2];
    [pickerArrayHalf addObject:@"0"];
    [pickerArrayHalf addObject:@"1/2"];
}

- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view
{
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 300, 37)];
    NSString *weight = [[pickerArrayHalf objectAtIndex:row] stringValue];
    label.text = [NSString stringWithFormat:@"%@", weight];
}


You stored NSStrings in the array using @"0" and @"1/2" and NSString does not respond to stringValue, it is a string. Just remove stringValue from the method calls.

NSString *weight = [pickerArrayHalf objectAtIndex:row];

Side Note:

You are over complicating setting the text of the label. Simply do the following.

label.text = [pickerArrayHalf objectAtIndex:row];

Also you are not retuning any view in that example which should generate a warning. The last line should be

//I would recommend calling autorelease on the initial alloc/initWithFrame
return [label autorelease];


You're adding a string to the array:

[pickerArrayHalf addObject:@"0"];

so there's no use to later on asking the string for its stringValue:

[[pickerArrayHalf objectAtIndex:row] stringValue];

Get rid of the call to stringValue (NSString does not even implement stringValue, for obvious reasons):

NSString *weight = [pickerArrayHalf objectAtIndex:row];

Further more why are you wrapping your string into a new one here?:

label.text = [NSString stringWithFormat:@"%@", weight];

just do this:

NSString *weight = [pickerArrayHalf objectAtIndex:row];
label.text = weight;

or even shorter:

label.text = [pickerArrayHalf objectAtIndex:row];

Last but not least your pickerView :… method is leaking label and not returning a UIView as expected.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜