I have a single picker and I want to incorporate a if statement so that depending on the text returned the display will be different, having some issues implementing this, code is as follows:
@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 re开发者_开发问答lease];
[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];
}
the title text is not recognized but I think I may be going about this all wrong, any help is much appreciated.
You should be getting compilation errors.. the reason is obvious... you are declaring title within if and else block, but you should declare it before if-else block !
ie. you should use this code in place of yours-
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];
}
精彩评论