Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/iphone/42.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Iphone UIPickerView以显示不同的Web视图_Iphone_Objective C_Ios5_Xcode4.2_Uipickerview - Fatal编程技术网

Iphone UIPickerView以显示不同的Web视图

Iphone UIPickerView以显示不同的Web视图,iphone,objective-c,ios5,xcode4.2,uipickerview,Iphone,Objective C,Ios5,Xcode4.2,Uipickerview,我试图用UIPickerView控制我的UIWebView,以便显示不同的PDF文件。但不知怎么的,我的代码没有。如果我选择了某一行,它就会崩溃,或者它显示了错误的PDF。有什么想法吗 #import "PickerViewTutorialViewController.h" @implementation PickerViewTutorialViewController @synthesize pickerView; - (void)dealloc { [super dealloc

我试图用UIPickerView控制我的UIWebView,以便显示不同的PDF文件。但不知怎么的,我的代码没有。如果我选择了某一行,它就会崩溃,或者它显示了错误的PDF。有什么想法吗

#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

首先,由于处理行选择的方式,代码崩溃。 您的第二个对象数组(array2)只有3个对象(索引为0、1、2),但如果您在第一个组件中选择了第四个对象,您将尝试访问array2中索引为3的对象,因此您将得到一个“越界”异常

我建议您用这种方法重写您的逻辑:

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
您应该使用
inComponent:(NSInteger)组件来优化代码


我希望这有帮助。

请注意,您可以替换所有代码:

    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"]]]]; }
    }
作者:

在(简化代码的)前两行中,您看到了问题:当行为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]]]];
}

这意味着该代码只能显示“1A”、“2B”和“3B”。此外,没有理由让您有18行
stundenplan.scalesPageToFit=YES。只有一行就足够了,即在方法的顶部。你可以说你希望有人为你写整件事。谢谢,这很有帮助,但仍然不能解决我的主要问题。我知道问题在哪里,但我不知道如何让它工作。。。
- (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]]]];
}