2011-11-09 3 views
0

나는 마르코 Armant에 의해 this tweet을 보았다 :누군가 UIActionSheet (buttonIndex, delegate)를 UIButton (addTarget : action)처럼 만들었습니까?

이/buttonIndex을 대표을 방지하기 위해 버튼을 UIActionSheet w/target:action:userInfo:를 서브 클래스. 다른 사람이하지 않았습니까? 그것을 찾을 수 없습니다.

나는 훌륭한 생각처럼 들리 겠지만, 이렇게 한 사람의 코드를 찾을 수는 없었다. 내가 직접 해보기 전에 누구도 알고 있니?

답변

2

예, 내 github에서 OHActionSheet을 참조하십시오.

블록을 사용하여 구현되므로 소스 코드의 대상/작업 코드를 추방하지 않고도이 방법으로 사용할 수 있습니다. 모든 것이 소스 코드의 동일한 위치에 있으며, 당신은 동일한 컨트롤러에서 원하는대로 당신은 많은 OHActionSheets를 사용할 수있는

NSURL* anURL = ... // some URL (this is only as an example on using out-of-scope variables in blocks) 
[OHActionSheet showSheetInView:yourView 
         title:@"Open this URL?" 
      cancelButtonTitle:@"Cancel" 
     destructiveButtonTitle:nil 
      otherButtonTitles:[NSArray arrayWithObjects:@"Open",@"Bookmark",nil] 
        completion:^(OHActionSheet* sheet,NSInteger buttonIndex) { 
    if (buttonIndex == sheet.cancelButtonIndex) { 
    NSLog(@"You cancelled"); 
    } else { 
    NSLog(@"You choosed button %d",buttonIndex); 
    switch (buttonIndex-sheet.firstOtherButtonIndex) { 
     case 0: // Open 
     // here you can access the anURL variable even if this code is executed asynchrously, thanks to the magic of blocks! 
     [[UIApplication sharedApplication] openURL:anURL]; 
     break; 
     case 1: // Bookmark 
     default: 
     // Here you can even embed another OHAlertView for example 
     [OHAlertView showAlertWithTitle:@"Wooops" 
           message:@"This feature is not available yet, sorry!" 
          cancelButton:@"Damn" 
          otherButtons:nil 
          onButtonTapped:nil]; // no need for a completion block here 
     break; 
    } // switch 
    } 
}]; 

[편집] 자세한 내용과 예문을 추가 할 수있는 편집 샘플 코드를

+0

이 좋아 보인다,하지만 여전히 정의해야 버튼 제목의 배열, 인덱스를 기반으로 한 것들을 얻은 다음, 그것을 selec으로 바꿉니다. tors, 당신이 그 행동을 전달하고 싶다면. 나는 'UIButton'과 같은 것을 원했다. 제목, 셀렉터, 타겟을 한 번에 정의하면된다. – zekel

+0

블록 접근법에 대해 좋아하는 한 가지 점은 첫 번째 표시 할 때 알 수 있으므로 동일한 UIBarButtonItem을 통해 후속 작업 시트를 쉽게 표시 할 수 있다는 것입니다. (정상적인 UIActionSheet로 이것을 수행하는 좋은 방법을 모르겠습니다.) – zekel

+0

(내가 액션을 전달하고자하는 이유는 모두 응답하는 여러보기 컨트롤러에서 사용하는 작업 시트를 갖는 것입니다.) – zekel

관련 문제