UIActionSheet не загружает действия для индексов кнопок? - PullRequest
0 голосов
/ 12 сентября 2011

Я настроил некоторый код, чтобы при нажатии двух отдельных кнопок на листе 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 ]

2 голосов
/ 12 сентября 2011

Вы установили для делегата UIActionSheet значение nil.В этом контексте вы хотите установить его на self.

1 голос
/ 12 сентября 2011

Вы создаете лист действий с делегатом nil, поэтому реализованные вами методы делегата никогда не будут вызываться.Передайте self в качестве делегата в инициализаторе листа действий, и он должен работать.

1 голос
/ 12 сентября 2011

Вы должны установить делегата на себя

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