2011-09-12 1 views
0

UIActionSheetで2つの別々のボタンをタップすると、2つの異なるアクションが発生するようにいくつかのコードを設定しました。残念ながら、ボタンを押すと何も起こりません。 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!"); 

} 
} 

おかげで、

ジェームズ

答えて

2

あなたはnilUIActionSheetデリゲートを設定しています。ここでは、selfに設定します。

+0

ありがとうございます、今すぐご利用ください! :-) – pixelbitlabs

+0

@ジェームスアンダーソンあなたは歓迎です:) – albertamg

1

あなたが自己

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

あなたはnilデリゲートとアクションシートを作成しているにデリゲートを設定する必要がありますので、あなたが実装デリゲートメソッドが呼び出されることはありません飽きないだろう。アクションシートのイニシャライザの代理人としてselfを渡すと、動作するはずです。

関連する問題