2009-07-22 22 views

答えて

-2

TheesボタンがUIActionSheetのサブビューであり、そのクラスは、あなたがそれらを取得し、あなたが望むものをすべて行うことができますUIThreePartButton

です:

UIActionSheet *a = [[UIActionSheet alloc]initWithTitle:@"" delegate: nil cancelButtonTitle: @"c" destructiveButtonTitle: @"d" otherButtonTitles: @"ot", nil]; 
    [a showInView: window]; 

    for(UIView *v in [a subviews]) 
    { 
     if([[v description] hasPrefix: @"<UIThreePartButton"]) 
     { 
      v.hidden = YES; //hide 
      //((UIButton*)v).enabled = NO; // disable 

     } 
    } 
+3

私はこれらのSDKにプライベートいるとして、あなたは、このメソッドを使用しないことをお勧め。 Appleがあなたのアプリを拒否するかもしれない。最も良い選択肢は、無効にする代わりにこれらのボタンを表示しないことです。 – lostInTransit

+0

このコードにはプライベートメソッドはありません))公開のみ: 私のアプリでプライベートメソッドを使用することがあります。私はそれらをアプリケーションで2回使用し、リンゴはこれらのアプリケーションを拒否しませんでした – oxigen

+5

Appleによって拒否されただけではなく、プライベートAPIを使用することで、お客様に敬意を払うことができません。プライベートAPIは、Appleがまだそれらをロックしているのでプライベートなので、変更するとアプリケーションが壊れます。上記のような視線を通ると、多くのアプリケーションが3.0以下で動作するようになりました。そのため、Appleはリリースノートでこれを呼び出し、これをしないようにしました。 –

0

これは少し遅れて、おそらくです。しかし、私が考え出したのは、UIButtonを作成し、UIActionSheetのサブビューに を追加することです。これらのボタンが上に配置され、置き換えるデフォルトのUIActionSheetボタンを完全に覆っていることを確認します。 UIButtonがデフォルトのUIActionSheetボタンの上に置かれると、そのUIResponderがUIActionSheetボタンUIResponderよりも優先されます。だから、これらのボタンを無効にして有効にすることができますが、UIViewControllerロジックのどこにいてもかまいません。これは、SDKへのプライベートメソッド(上記の-UIThreePartButtonのようなもの)にアクセスする代わりに、appleがあなたのアプリケーションを拒否する可能性があります。私はこれがAppleのガイドラインに従うと信じています。次のようにtoStateプロパティ方法:スレッドの数に基づいて、すなわち

// Instantiate once 
if (self.actionSheet==nil) { 
    UIActionSheet *as = [[UIActionSheet alloc] 
         initWithTitle:@"" 
         delegate:self 
         cancelButtonTitle:@"Cancel" 
         destructiveButtonTitle:@"Load Data" 
         otherButtonTitles:@"Update Data",nil]; 

    //[actionSheet showInView:self.view]; 
    self.loadUIButton.frame = CGRectMake(24.0f,25.0f,275.f,46.0f); 
    [as addSubview: self.loadUIButton]; 
    self.updateUIButton.frame = CGRectMake(24.0f,78.0f,275.f,46.0f); 
    [as addSubview: self.updateUIButton]; 
    //[actionSheet addSubview: self.cancelUIButton]; 
    //[as showFromToolbar: self.navigationController.toolbar]; 
    self.actionSheet = as; 
    [as release]; 
} 
[self.actionSheet showFromToolbar: self.navigationController.toolbar]; 
17

、iはsetButtonを追加、UIActionSheet上のカテゴリへの回答を集計しました。 はそれが役に立てば幸い:

@interface UIActionSheet(ButtonState) 
- (void)setButton:(NSInteger)buttonIndex toState:(BOOL)enbaled; 
@end 

@implementation UIActionSheet(ButtonState) 
- (void)setButton:(NSInteger)buttonIndex toState:(BOOL)enabled { 
    for (UIView* view in self.subviews) 
    { 
     if ([view isKindOfClass:[UIButton class]]) 
     { 
      if (buttonIndex == 0) { 
       if ([view respondsToSelector:@selector(setEnabled:)]) 
       { 
        UIButton* button = (UIButton*)view; 
        button.enabled = enabled; 
       } 
      } 
      buttonIndex--; 
     } 
    } 
} 
@end 
+0

優れたソリューション、非常にシンプルで使いやすく、正しいエンジニアリングの方法で実行。 +1 – Darrarski

2

Reuvenさんの少し改良版[私は評判 『まだ...彼の「私は持っていけない原因』にコメントを追加するにはカント悲しいかな]。

@interface UIActionSheet (ButtonEnabled) 
- (void)setButtonAtIndex:(NSUInteger)index Enabled:(BOOL)enabled; 
@end 

@implementation UIActionSheet (ButtonEnabled) 
- (void)setButtonAtIndex:(NSUInteger)index Enabled:(BOOL)enabled 
{ 
    for (UIView* view in self.subviews) { 
     if ([view isKindOfClass:[UIButton class]]) { 
      if (index-- == 0) { 
       [(UIButton*)view setEnabled:enabled]; 
       break; 
      } 
     } 
    } 
} 
@end 

前のrespondsToSelector:。我々はすでに、すべてのサポートsetEnabled] AppleのNSArraysを踏まえて、私も変更UicontrolのサブクラスであるUIButton(、のためにチェックしているので、チェックは、余分だった「AtIndex」とNSUIntegerへ

全体的なアプローチはうまくいくようですが、アクションシートが作成されたときに、ボタンのサブビューの順序がボタンインデックスの順序と正確に一致すると仮定していることに注意してください。厳密にはそうではありませんAFAIKはどこにでも文書化されています。

+0

ところで、[this](http://stackoverflow.com/questions/5262428/uiactionsheet-buttonindex-values-faulty-when-using-more-than-6-custom-buttons)によると、ボタンのインデックス*あなたの行動シートにあまりにも多くのアイテムがあるときは、結婚式に行ってはいけません。そうすれば、多くの人にはこのアプローチが無効になります。 – tiritea

1

kをiOS 8で使用している人がいます。これはAppleの推奨方法であるUIAlertControllerを使用していない人がいる場合、これは私が@ Reuvenの回答をiOS 8でも修正した方法です。

thisにそう答える)

- (void)setButton:(NSInteger)buttonIndex toState:(BOOL)enabled { 

    SEL selector = NSSelectorFromString(@"_alertController"); 

    //iOS 8 
    if ([self respondsToSelector:selector]) { 

     UIAlertController *alertController = [self valueForKey:@"_alertController"]; 

     if ([alertController isKindOfClass:[UIAlertController class]]){ 

      UIAlertAction *action = alertController.actions[buttonIndex]; 

      [action setEnabled:enabled]; 

     } 

    //iOS 7 
    }else{ 

     for (UIView* view in self.subviews){ 

      if ([view isMemberOfClass:NSClassFromString(@"UIAlertButton")]) { 

       if (buttonIndex == 0) { 

        if ([view respondsToSelector:@selector(setEnabled:)]){ 

         UIButton* button = (UIButton*)view; 

         button.enabled = enabled; 

        } 

       } 

       buttonIndex--; 

      } 

     } 

    } 

} 
1

スウィフト3.0バージョン:

let actionSheet = UIAlertController(title:nil, message:nil, preferredStyle:UIAlertControllerStyle.actionSheet) 


actionSheet.addAction(UIAlertAction(title:"Modify", style:UIAlertActionStyle.default, handler:{ action in 
    // Do your thing here 
})) 

let disabledDelete = UIAlertAction(title:"Cannot delete this item", style:UIAlertActionStyle.destructive, handler:nil) 
disabledDelete.isEnabled = false 
actionSheet.addAction(disabledDelete) 

actionSheet.addAction(UIAlertAction(title:"Cancel", style:UIAlertActionStyle.cancel, handler:nil)) 

self.present(actionSheet, animated:true, completion:nil) 
関連する問題