UIActionSheet没有加载按钮索引的动作?

时间:2011-09-12 17:10:51

标签: iphone objective-c ios xcode uiactionsheet

我已经设置了一些代码,以便在UIActionSheet上点击两个单独的按钮时,会有两个不同的操作。不幸的是,按下按钮时没有任何反应。 UIActionSheet只是卸载,就像按下了取消按钮一样。

这是我的代码:

- (IBAction)saveFile:(id)sender {

UIActionSheet *saveFileSheet = [[[UIActionSheet alloc] 
                       initWithTitle:@"iDHSB Download Centre" 
                       delegate:nil 
                       cancelButtonTitle:@"Cancel"  
                       destructiveButtonTitle:nil 
                       otherButtonTitles:@"Download File", @"Show My Files", nil] 
                      autorelease];

 [saveFileSheet showInView:webView];

}

-(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{

NSLog(@"Action Sheet Button Pressed");

if(buttonIndex == 1) {
        NSLog(@"Show My Files");

        [self.window presentModalViewController:savedFiles animated:YES];

}

if(buttonIndex == 2){

        NSLog(@"Saving File");

        // Get the URL of the loaded ressource
        NSURL *theResourcesURL = [[webView request] URL];
        // Get the filename of the loaded ressource form the UIWebView's request URL
        NSString *filename = [theResourcesURL lastPathComponent];
        NSLog(@"Filename: %@", filename);
        // Get the path to the App's Documents directory
        NSString *docPath = [self documentsDirectoryPath];
        // Combine the filename and the path to the documents dir into the full path
        NSString *pathToDownloadTo = [NSString stringWithFormat:@"%@/%@", docPath, filename];


        // Load the file from the remote server
        NSData *tmp = [NSData dataWithContentsOfURL:theResourcesURL];
        // Save the loaded data if loaded successfully
        if (tmp != nil) {
            NSError *error = nil;
            // Write the contents of our tmp object into a file
            [tmp writeToFile:pathToDownloadTo options:NSDataWritingAtomic error:&error];
            if (error != nil) {
                UIAlertView *filenameErrorAlert = [[UIAlertView alloc] initWithTitle:@"Error Saving" message:[NSString stringWithFormat:@"The file %@ could not be saved. Please try again.", filename] delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
                [filenameErrorAlert show];
                [filenameErrorAlert release];

                NSLog(@"Failed to save the file: %@", [error description]);
            } else {
                // Display an UIAlertView that shows the users we saved the file :)
                UIAlertView *filenameAlert = [[UIAlertView alloc] initWithTitle:@"File saved" message:[NSString stringWithFormat:@"The file %@ has been saved.", filename] delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
                [filenameAlert show];
                [filenameAlert release];
            }
        } else {

            NSLog(@"Error, file could not be saved");

        }
}

else 

{

    NSLog(@"Error, could not find button index!");

}
}

谢谢,

詹姆斯

3 个答案:

答案 0 :(得分:2)

您已将UIActionSheet委托设为nil。在此上下文中,您希望将其设置为self

答案 1 :(得分:1)

您必须将委托设置为自我

UIActionSheet *saveFileSheet = [[[UIActionSheet alloc] 
                       initWithTitle:@"iDHSB Download Centre" 
                       delegate:self 
                       cancelButtonTitle:@"Cancel"  
                       destructiveButtonTitle:nil 
                       otherButtonTitles:@"Download File", @"Show My Files", nil] 
                      autorelease];

答案 2 :(得分:1)

您正在使用nil委托创建操作表,因此您实施的委托方法永远不会被调用。将self作为代理人在操作表的初始化程序中传递,它应该可以正常工作。