Reputation: 1934
I have set up some code so that when two separate buttons are tapped on a UIActionSheet, there will be two different actions. Unfortunately nothing happens when the buttons are pressed. The UIActionSheet just unloads as if a cancel button had been pressed.
Here's my code:
- (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!");
}
}
Thanks,
James
Upvotes: 0
Views: 1707
Reputation: 57139
You’re creating the action sheet with a nil
delegate, so the delegate methods you implemented will never get called. Pass self
as the delegate in the action sheet’s initializer and it should work.
Upvotes: 1
Reputation: 4428
You have to set the delegate to self
UIActionSheet *saveFileSheet = [[[UIActionSheet alloc]
initWithTitle:@"iDHSB Download Centre"
delegate:self
cancelButtonTitle:@"Cancel"
destructiveButtonTitle:nil
otherButtonTitles:@"Download File", @"Show My Files", nil]
autorelease];
Upvotes: 1
Reputation: 28572
You have set the UIActionSheet
delegate to nil
. In this context, you want to set it to self
.
Upvotes: 2