单个选择器包括一个If语句 - 不工作

时间:2011-10-06 12:00:55

标签: objective-c xcode

我有一个选择器,我希望合并一个if语句,以便根据返回的文本显示不同,有一些问题实现这一点,代码如下:

@implementation ButtonFive

@synthesize singlePicker;
@synthesize pickerData;

-(IBAction)buttonPressed {
NSInteger row = [singlePicker selectedRowInComponent:0];
NSString *selected = [pickerData objectAtIndex:row];




 if ([selected isEqualToString: @"MC860"])
     //if ([selected isEqualToString: @"MC860"])
 {
 NSString *title = [[NSString alloc] initWithFormat: @"Use the maintenance utility to upgrade the %@!", selected];

 }

 else {
 NSString *title = [[NSString alloc] initWithFormat: @"try this %@!", selected];

 }



//NSString *title = [[NSString alloc] initWithFormat: @"you selected %@!", selected];


UIAlertView *alert = [[UIAlertView alloc] initWithTitle:title message : @"Thank you for choosing." delegate:nil cancelButtonTitle :@"Continue" otherButtonTitles :nil];
[alert show];
[alert release];
[title release];
}


- (void)viewDidLoad {
NSArray *array = [[NSArray alloc]        initWithObjects:@"B410",@"B411",@"B440",@"B840",@"MC860", @"MC861",@"MC861",@"B410",@"B411",@"B440",@"B840",@"MC860", nil];
self.pickerData = array;
[array release];
[super viewDidLoad];
}

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

-(NSInteger)pickerView:(UIPickerView *)pickerView
numberOfRowsInComponent:(NSInteger)component
{
return [pickerData count];
}

-(NSString *)pickerView:(UIPickerView *)pickerView
        titleForRow:(NSInteger)row
       forComponent:(NSInteger)component 
{
return[pickerData objectAtIndex:row];
}

标题文字无法识别,但我想我可能会认为这一切都错了,非常感谢任何帮助。

1 个答案:

答案 0 :(得分:2)

你应该得到编译错误..原因很明显......你在if和else块中声明了标题,但你应该在if-else块之前声明它!

即。您应该使用此代码代替您的代码 -

NSString *title;


if ([selected isEqualToString:@"MC860"])//if ([selected isEqualToString: @"MC860"])
{
    title = [[NSString alloc] initWithFormat:@"Use the maintenance utility to upgrade the %@!", selected];
}
else {
    title = [[NSString alloc] initWithFormat:@"try this %@!", selected];

}