I tried to control my UIWebView with a UIPickerView, in order to display different PDF files. But somehow my code doesn’t. Either it crashes if I select a certain row or it displays the wrong PDF. Any ideas?
#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
Note that you can replace all of the code:
By:
In the first two lines (of the simplified code), you see the problem: When row is 3 or greater, you can’t call
[array2 objectAtIndex:row];which has a length of only 3.Edit
You can use the method:
To determine the correct indices of the letters and numbers: