2011-11-09 52 views

回答

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

它看起來不錯,但你仍然需要定義按鈕標題數組,獲取基於索引的數據,然後將其轉換爲選擇如果你想通過這個動作的話。我希望有更像'UIButton'的東西,我只需要一次定義標題,選擇器和目標。 – zekel

+0

我喜歡有關塊方法的一件事是,您可以通過相同的UIBarButtonItem輕鬆顯示後續的操作表,因爲您在顯示第一個時會知道它。 (我不知道用普通UIActionSheet做這件事的好方法。) – zekel

+0

(我想傳遞動作的原因是在多個視圖控制器中使用一個動作表,它們都會響應) – zekel