2

ユーザーがactionSheetのcancelButtonまたは破壊ボタンを押したかどうかを検出しようとしています。彼/彼女の入力に応じて、画面に表示されるアラートメッセージを変更したいと思います。

そこで、を使っifて確認してみましたが、ユーザーからの入力によって値が変わらないことがわかりましたbuttonIndexbuttonIndex

UIActionSheet *informUser = [[UIActionSheet alloc] initWithTitle:@"Missing data" delegate:self cancelButtonTitle:@"Thanks for telling me!" destructiveButtonTitle:@"I have ignored it!" otherButtonTitles:nil];

    [informUser showInView:self.view];

//        NSLog(@"Cancel button = %d", informUser.cancelButtonIndex);
//        NSLog(@"Destructive button = %d", informUser.destructiveButtonIndex);

    if(informUser.cancelButtonIndex == 1)
    {
        NSString *msg = [[NSString alloc] initWithFormat:@"Pls enter the number."];

        UIAlertView *alertUser = [[UIAlertView alloc] initWithTitle:@"Missing data" message:msg delegate:self cancelButtonTitle:@"Thanks!" otherButtonTitles:nil];

        [alertUser show];
    }

また、別の方法を使用してみました。

-(void)actionSheet:(UIActionSheet *)actionSheet didDismissWithButtonIndex:(NSInteger)buttonIndex;

UIAlertしかし、同じ画面の異なるポイントに複数あると、間違った結果が得られます。

UIAlert要するに、ユーザーからのアクション/入力ごとに異なるものを使用したいと思います。したがって、のどのボタンがユーザーによって押されたかを検出するにはどうすればよいactioSheetですか?

ありがとうございました。

4

1 に答える 1

1

を実装し、UIActionSheetDelegate次のメソッドを使用します。

– actionSheet:clickedButtonAtIndex: 

インデックスは、タップしたボタンを定義します。

ボタンインデックスを使用したくない場合は、タップしたボタンのボタンタイトルを次のように取得できます。

– buttonTitleAtIndex:

次に、結果をボタンのタイトル文字列と比較できます。

Appleドキュメントで詳細を読む


編集: より多くのアクションシートを使用したい場合は、それぞれにタグ値を設定できます。たとえば、ActionSheet1にはactionsheet.tag=1などがあります。

– actionSheet:clickedButtonAtIndex:次に、指定されたアクションシートのタグで切り替えることができます。例えば:

switch (actionsheet.tag) {
    case 1: 
        // do stuff
    break;
    case 2: 
        // do stuff
    break;
    default: break;
}

編集2: これはロジックを表示するサンプルクラスです:

ヘッダーファイル:

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController <UIActionSheetDelegate>

@end

実装ファイル:

#import "ViewController.h"

@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.

    UIActionSheet *as1 = [[UIActionSheet alloc] initWithTitle:@"Title" delegate:self cancelButtonTitle:@"Cancel" destructiveButtonTitle:@"Delete" otherButtonTitles:@"Ok", nil];
    as1.tag = 0;
    as1.delegate = self;

    UIActionSheet *as2 = [[UIActionSheet alloc] initWithTitle:@"Title2" delegate:self cancelButtonTitle:@"Cancel2" destructiveButtonTitle:@"Delete2" otherButtonTitles:@"Ok2", nil];
    as2.tag = 1;
    as2.delegate = self;
}


- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {
    switch (actionSheet.tag) {
        case 0: // as1
            switch (buttonIndex) {
                case 0:
                    // button index 0
                    break;

                case 1:
                    // button index 1
                    break;

                //....

                default:
                    break;
            }
            break;

        case 1: // as2
            switch (buttonIndex) {
                case 0:
                    // button index 0
                    break;

                case 1:
                    // button index 1
                    break;

                    //....

                default:
                    break;
            }
            break;

        default:
            break;
    }
}

@end
于 2012-09-18T14:30:07.293 に答える