1

UIActionSheetを表示する関数を作成し、ユーザーがボタンを押すまで待ってから、押されたボタンを元に戻したい

@interface UIActionSheetHandler () <UIActionSheetDelegate>

@end
@implementation UIActionSheetHandler


-(NSInteger) buttonIndexWithMessage:(NSString *) title andArrayOfOptions:(NSArray *) options
{

    UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:title
                                                             delegate:self
                                                    cancelButtonTitle:nil
                                               destructiveButtonTitle:nil
                                                    otherButtonTitles:nil];

    for (NSString * strOption in options) {
        [actionSheet addButtonWithTitle:strOption];
    }

    [actionSheet showInView:[BGMDApplicationsPointers window]];

    //Wait till delegate is called.
    return 0;//I want to return buttonIndex here.
}

- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
    //What should I put it here
}
@end

//デリゲートが呼び出されて待機するまで待機するにはどうすればよいですか?ああ、メインスレッドを待たせたくないのですが、それは解決できると思います。

4

2 に答える 2

2

デリゲートの概念を誤解したと思います。から押されたボタンを返すことはできませんbuttonIndexWithMessage:andArrayOfOptions:

実際のところ、UIActionSheetはその時点では表示されません。

ボタンが押されると、UIActionSheetはactionSheet:clickedButtonAtIndex:デリゲートのメソッドを呼び出します。

ですから、入れたところに//What should I put it here there押されたボタンのインデックスが渡されます。そこで、それに応じて押されたボタンに反応することができます。例えば:

- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
    NSLog (@"Button pressed with index: %d", buttonIndex); 
}
于 2013-03-12T10:21:03.803 に答える
1

これは動作します

self.operation=[NSOperationQueue new]; //These four line is necessary to suspend the whole thread.
self.operation.suspended=true; //Okay make t
[self.operation addOperationWithBlock:^{}];
[self.operation waitUntilAllOperationsAreFinished];//Don't get out of the function till user act.

その後、代表者で

- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
    self.buttonIndex=buttonIndex;
    self.operation.suspended=false;
}

注:私はまだよりエレガントな解決策を探しています。

于 2013-03-12T14:21:48.540 に答える