두 개의 별도 버튼이 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!");
}
}
감사합니다,
제임스 당신은 nil
에 UIActionSheet
대리자를 설정 한
대단히 고맙습니다. :-) – pixelbitlabs
@James Anderson 당신은 환영합니다 :) – albertamg