2011-09-12 2 views
0

두 개의 별도 버튼이 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!"); 

} 
} 

감사합니다,

제임스 당신은 nilUIActionSheet 대리자를 설정 한

답변

2

:

여기 내 코드입니다. 이 경우 self으로 설정하려고합니다.

+0

대단히 고맙습니다. :-) – pixelbitlabs

+0

@James Anderson 당신은 환영합니다 :) – 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을 위임자로 전달하면 제대로 작동합니다.

관련 문제