2012-02-18 64 views
-1

我試圖用UIPickerView控制我的UIWebView,以顯示不同的PDF文件。但不知何故,我的代碼不。如果我選擇某一行或它顯示錯誤的PDF,它會崩潰。有任何想法嗎?UIPickerView顯示不同的WebViews

#import "PickerViewTutorialViewController.h" 

@implementation PickerViewTutorialViewController 

@synthesize pickerView; 

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

- (void)didReceiveMemoryWarning 
{ 
    // Releases the view if it doesn't have a superview. 
    [super didReceiveMemoryWarning]; 

    // Release any cached data, images, etc that aren't in use. 
} 

#pragma mark - View lifecycle 

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    [self populateArray1]; 
    [self populateArray2]; 
} 

- (void)populateArray1 { 
    array1 = [[NSMutableArray alloc] init]; 
    [array1 addObject:@"1"]; 
    [array1 addObject:@"2"]; 
    [array1 addObject:@"3"]; 
    [array1 addObject:@"4"]; 
    [array1 addObject:@"5"]; 
    [array1 addObject:@"6"]; 
} 
- (void)populateArray2 { 
    array2 = [[NSMutableArray alloc] init]; 
    [array2 addObject:@"A"]; 
    [array2 addObject:@"B"]; 
    [array2 addObject:@"C"]; 
} 


- (void)viewDidUnload 
{ 
    [super viewDidUnload]; 
    // Release any retained subviews of the main view. 
    // e.g. self.myOutlet = nil; 
} 

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation 
{ 
    // Return YES for supported orientations 
    return (interfaceOrientation == UIInterfaceOrientationPortrait); 
} 

#pragma mark - 
#pragma mark picker view methods 
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView; 
{ 
    return 2; 
} 

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component 
{ 
    if ([[array1 objectAtIndex:row] isEqual:@"1"]) { 
     if ([[array2 objectAtIndex:row] isEqual:@"A"]) { 
      stundenplan.scalesPageToFit = YES; 
      [stundenplan loadRequest:[NSURLRequest requestWithURL:[NSURL fileURLWithPath:[[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"Stundenplan_1A.pdf"]]]]; } 
     if ([[array2 objectAtIndex:row] isEqual:@"B"]) { 
      stundenplan.scalesPageToFit = YES; 
      [stundenplan loadRequest:[NSURLRequest requestWithURL:[NSURL fileURLWithPath:[[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"Stundenplan_1B.pdf"]]]]; } 
     if ([[array2 objectAtIndex:row] isEqual:@"C"]) { 
      stundenplan.scalesPageToFit = YES; 
      [stundenplan loadRequest:[NSURLRequest requestWithURL:[NSURL fileURLWithPath:[[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"Stundenplan_1C.pdf"]]]]; } 
    } 
    if ([[array1 objectAtIndex:row] isEqual:@"2"]) { 
     if ([[array2 objectAtIndex:row] isEqual:@"A"]) { 
      stundenplan.scalesPageToFit = YES; 
      [stundenplan loadRequest:[NSURLRequest requestWithURL:[NSURL fileURLWithPath:[[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"Stundenplan_2A.pdf"]]]]; } 
     if ([[array2 objectAtIndex:row] isEqual:@"B"]) { 
      stundenplan.scalesPageToFit = YES; 
      [stundenplan loadRequest:[NSURLRequest requestWithURL:[NSURL fileURLWithPath:[[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"Stundenplan_2B.pdf"]]]]; } 
     if ([[array2 objectAtIndex:row] isEqual:@"C"]) { 
      stundenplan.scalesPageToFit = YES; 
      [stundenplan loadRequest:[NSURLRequest requestWithURL:[NSURL fileURLWithPath:[[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"Stundenplan_2C.pdf"]]]]; } 
    } 
    if ([[array1 objectAtIndex:row] isEqual:@"3"]) { 
     if ([[array2 objectAtIndex:row] isEqual:@"A"]) { 
      stundenplan.scalesPageToFit = YES; 
      [stundenplan loadRequest:[NSURLRequest requestWithURL:[NSURL fileURLWithPath:[[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"Stundenplan_3A.pdf"]]]]; } 
     if ([[array2 objectAtIndex:row] isEqual:@"B"]) { 
      stundenplan.scalesPageToFit = YES; 
      [stundenplan loadRequest:[NSURLRequest requestWithURL:[NSURL fileURLWithPath:[[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"Stundenplan_3B.pdf"]]]]; } 
     if ([[array2 objectAtIndex:row] isEqual:@"C"]) { 
      stundenplan.scalesPageToFit = YES; 
      [stundenplan loadRequest:[NSURLRequest requestWithURL:[NSURL fileURLWithPath:[[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"Stundenplan_3C.pdf"]]]]; } 
    } 
    if ([[array1 objectAtIndex:row] isEqual:@"4"]) { 
     if ([[array2 objectAtIndex:row] isEqual:@"A"]) { 
      stundenplan.scalesPageToFit = YES; 
      [stundenplan loadRequest:[NSURLRequest requestWithURL:[NSURL fileURLWithPath:[[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"Stundenplan_4A.pdf"]]]]; } 
     if ([[array2 objectAtIndex:row] isEqual:@"B"]) { 
      stundenplan.scalesPageToFit = YES; 
      [stundenplan loadRequest:[NSURLRequest requestWithURL:[NSURL fileURLWithPath:[[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"Stundenplan_4B.pdf"]]]]; } 
     if ([[array2 objectAtIndex:row] isEqual:@"C"]) { 
      stundenplan.scalesPageToFit = YES; 
      [stundenplan loadRequest:[NSURLRequest requestWithURL:[NSURL fileURLWithPath:[[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"Stundenplan_4C.pdf"]]]]; } 
    } 
    if ([[array1 objectAtIndex:row] isEqual:@"5"]) { 
     if ([[array2 objectAtIndex:row] isEqual:@"A"]) { 
      stundenplan.scalesPageToFit = YES; 
      [stundenplan loadRequest:[NSURLRequest requestWithURL:[NSURL fileURLWithPath:[[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"Stundenplan_5A.pdf"]]]]; } 
     if ([[array2 objectAtIndex:row] isEqual:@"B"]) { 
      stundenplan.scalesPageToFit = YES; 
      [stundenplan loadRequest:[NSURLRequest requestWithURL:[NSURL fileURLWithPath:[[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"Stundenplan_5B.pdf"]]]]; } 
     if ([[array2 objectAtIndex:row] isEqual:@"C"]) { 
      stundenplan.scalesPageToFit = YES; 
      [stundenplan loadRequest:[NSURLRequest requestWithURL:[NSURL fileURLWithPath:[[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"Stundenplan_5C.pdf"]]]]; } 
    } 
    if ([[array1 objectAtIndex:row] isEqual:@"6"]) { 
     if ([[array2 objectAtIndex:row] isEqual:@"A"]) { 
      stundenplan.scalesPageToFit = YES; 
      [stundenplan loadRequest:[NSURLRequest requestWithURL:[NSURL fileURLWithPath:[[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"Stundenplan_6A.pdf"]]]]; } 
     if ([[array2 objectAtIndex:row] isEqual:@"B"]) { 
      stundenplan.scalesPageToFit = YES; 
      [stundenplan loadRequest:[NSURLRequest requestWithURL:[NSURL fileURLWithPath:[[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"Stundenplan_6B.pdf"]]]]; } 
     if ([[array2 objectAtIndex:row] isEqual:@"C"]) { 
      stundenplan.scalesPageToFit = YES; 
      [stundenplan loadRequest:[NSURLRequest requestWithURL:[NSURL fileURLWithPath:[[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"Stundenplan_6C.pdf"]]]]; } 
    } 
} 

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component; 
{ 
    if (component == 0) { 
     return [array1 count]; 
    } 
    else { 
     return [array2 count]; 
    } 
} 

- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component; 
{ 
    if (component == 0) { 
     return [array1 objectAtIndex:row]; 
    } 
    else { 
     return [array2 objectAtIndex:row]; 
    } 
} 

@end 

回答

0

注意,您可以替換的所有代碼:

if ([[array1 objectAtIndex:row] isEqual:@"1"]) { 
     if ([[array2 objectAtIndex:row] isEqual:@"A"]) { 
      stundenplan.scalesPageToFit = YES; 
      [stundenplan loadRequest:[NSURLRequest requestWithURL:[NSURL fileURLWithPath:[[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"Stundenplan_1A.pdf"]]]]; } 
     if ([[array2 objectAtIndex:row] isEqual:@"B"]) { 
      stundenplan.scalesPageToFit = YES; 
      [stundenplan loadRequest:[NSURLRequest requestWithURL:[NSURL fileURLWithPath:[[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"Stundenplan_1B.pdf"]]]]; } 
     if ([[array2 objectAtIndex:row] isEqual:@"C"]) { 
      stundenplan.scalesPageToFit = YES; 
      [stundenplan loadRequest:[NSURLRequest requestWithURL:[NSURL fileURLWithPath:[[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"Stundenplan_1C.pdf"]]]]; } 
    } 
    if ([[array1 objectAtIndex:row] isEqual:@"2"]) { 
     if ([[array2 objectAtIndex:row] isEqual:@"A"]) { 
      stundenplan.scalesPageToFit = YES; 
      [stundenplan loadRequest:[NSURLRequest requestWithURL:[NSURL fileURLWithPath:[[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"Stundenplan_2A.pdf"]]]]; } 
     if ([[array2 objectAtIndex:row] isEqual:@"B"]) { 
      stundenplan.scalesPageToFit = YES; 
      [stundenplan loadRequest:[NSURLRequest requestWithURL:[NSURL fileURLWithPath:[[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"Stundenplan_2B.pdf"]]]]; } 
     if ([[array2 objectAtIndex:row] isEqual:@"C"]) { 
      stundenplan.scalesPageToFit = YES; 
      [stundenplan loadRequest:[NSURLRequest requestWithURL:[NSURL fileURLWithPath:[[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"Stundenplan_2C.pdf"]]]]; } 
    } 
    if ([[array1 objectAtIndex:row] isEqual:@"3"]) { 
     if ([[array2 objectAtIndex:row] isEqual:@"A"]) { 
      stundenplan.scalesPageToFit = YES; 
      [stundenplan loadRequest:[NSURLRequest requestWithURL:[NSURL fileURLWithPath:[[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"Stundenplan_3A.pdf"]]]]; } 
     if ([[array2 objectAtIndex:row] isEqual:@"B"]) { 
      stundenplan.scalesPageToFit = YES; 
      [stundenplan loadRequest:[NSURLRequest requestWithURL:[NSURL fileURLWithPath:[[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"Stundenplan_3B.pdf"]]]]; } 
     if ([[array2 objectAtIndex:row] isEqual:@"C"]) { 
      stundenplan.scalesPageToFit = YES; 
      [stundenplan loadRequest:[NSURLRequest requestWithURL:[NSURL fileURLWithPath:[[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"Stundenplan_3C.pdf"]]]]; } 
    } 
    if ([[array1 objectAtIndex:row] isEqual:@"4"]) { 
     if ([[array2 objectAtIndex:row] isEqual:@"A"]) { 
      stundenplan.scalesPageToFit = YES; 
      [stundenplan loadRequest:[NSURLRequest requestWithURL:[NSURL fileURLWithPath:[[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"Stundenplan_4A.pdf"]]]]; } 
     if ([[array2 objectAtIndex:row] isEqual:@"B"]) { 
      stundenplan.scalesPageToFit = YES; 
      [stundenplan loadRequest:[NSURLRequest requestWithURL:[NSURL fileURLWithPath:[[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"Stundenplan_4B.pdf"]]]]; } 
     if ([[array2 objectAtIndex:row] isEqual:@"C"]) { 
      stundenplan.scalesPageToFit = YES; 
      [stundenplan loadRequest:[NSURLRequest requestWithURL:[NSURL fileURLWithPath:[[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"Stundenplan_4C.pdf"]]]]; } 
    } 
    if ([[array1 objectAtIndex:row] isEqual:@"5"]) { 
     if ([[array2 objectAtIndex:row] isEqual:@"A"]) { 
      stundenplan.scalesPageToFit = YES; 
      [stundenplan loadRequest:[NSURLRequest requestWithURL:[NSURL fileURLWithPath:[[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"Stundenplan_5A.pdf"]]]]; } 
     if ([[array2 objectAtIndex:row] isEqual:@"B"]) { 
      stundenplan.scalesPageToFit = YES; 
      [stundenplan loadRequest:[NSURLRequest requestWithURL:[NSURL fileURLWithPath:[[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"Stundenplan_5B.pdf"]]]]; } 
     if ([[array2 objectAtIndex:row] isEqual:@"C"]) { 
      stundenplan.scalesPageToFit = YES; 
      [stundenplan loadRequest:[NSURLRequest requestWithURL:[NSURL fileURLWithPath:[[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"Stundenplan_5C.pdf"]]]]; } 
    } 
    if ([[array1 objectAtIndex:row] isEqual:@"6"]) { 
     if ([[array2 objectAtIndex:row] isEqual:@"A"]) { 
      stundenplan.scalesPageToFit = YES; 
      [stundenplan loadRequest:[NSURLRequest requestWithURL:[NSURL fileURLWithPath:[[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"Stundenplan_6A.pdf"]]]]; } 
     if ([[array2 objectAtIndex:row] isEqual:@"B"]) { 
      stundenplan.scalesPageToFit = YES; 
      [stundenplan loadRequest:[NSURLRequest requestWithURL:[NSURL fileURLWithPath:[[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"Stundenplan_6B.pdf"]]]]; } 
     if ([[array2 objectAtIndex:row] isEqual:@"C"]) { 
      stundenplan.scalesPageToFit = YES; 
      [stundenplan loadRequest:[NSURLRequest requestWithURL:[NSURL fileURLWithPath:[[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"Stundenplan_6C.pdf"]]]]; } 
    } 

通過:

NSString *numberString = [array1 objectAtIndex:row]; 
NSString *letterString = [array2 objectAtIndex:row]; 
NSString *pathComponentString = [NSString stringWithFormat:@"Stundenplan_%@%@.pdf", numberString, letterString]; 
stundenplan.scalesPageToFit = YES; 
[stundenplan loadRequest:[NSURLRequest requestWithURL:[NSURL fileURLWithPath:[[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:pathComponentString]]]]; 

在頭兩行(簡化代碼的),你看到的問題:當行3或者更大的,你不能叫[array2 objectAtIndex:row];它只有3

編輯的長度

您可以使用方法:

- (NSInteger)selectedRowInComponent:(NSInteger)component; 

要確定的字母和數字的正確指標:

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component 
{ 
    NSString *numberString = [array1 objectAtIndex:[pickerView selectedRowInComponent:0]]; 
    NSString *letterString = [array2 objectAtIndex:[pickerView selectedRowInComponent:1]]; 
    NSString *pathComponentString = [NSString stringWithFormat:@"Stundenplan_%@%@.pdf", numberString, letterString]; 
    stundenplan.scalesPageToFit = YES; 
    [stundenplan loadRequest:[NSURLRequest requestWithURL:[NSURL fileURLWithPath:[[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:pathComponentString]]]]; 
} 
+0

謝謝你的幫助,但仍然不能解決我的主要問題。我知道問題出在哪裏,但我不知道如何使它工作... – AmiiQo 2012-02-18 14:21:06

+0

檢查我編輯的答案,它是否適合你? – sch 2012-02-18 14:42:15

+0

非常感謝,完美的作品! – AmiiQo 2012-02-18 14:49:36

0

首先,由於您如何處理行選擇,您的代碼崩潰。 你的第二個對象數組(array2)只有3個對象(索引0,1,2),但是如果你選擇第一個組件中的第四個對象,你將嘗試訪問array2中索引3的對象,所以你會出現「越界」異常。

我勸你重寫這個方法你的邏輯:

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component 

您應該使用它的inComponent:(NSInteger)component部分來優化你的代碼

我希望這有助於。

+0

這意味着該代碼只能顯示「1A」,「2B」和「3B」。另外,沒有理由讓你有18行'stundenplan.scalesPageToFit = YES;'。只用一行就足夠了,即在方法的頂部。 – matsr 2012-02-18 14:18:04

+0

你可能剛剛說過你想讓某人爲你寫完整件事。 – 2012-02-18 18:06:40