-1

ねえ私はそれをしたいこのボタンとボタンにはテキストフィールドがありますボタンを押すとアクションシートピッカーが表示され、選択した文字列の4から5のリストがボタンにあるテキストフィールドに表示されます. 私を助けてください

ここに画像の説明を入力

4

1 に答える 1

1

ボタンのターゲットを追加することから始めます。Objective-C では、次のようになります。

[myButton addTarget:self
             action:@selector(buttonPressed:)
   forControlEvents:UIControlEventTouchUpInside];

次に、メソッドを作成しますbuttonPressed。その例は次のとおりです。

- (void)buttonPressed:(id)sender {
    if ([sender isEqual:self.myButton]) {
        //This is where you can create the UIAlertController
    }
}

次に、以下を作成しますUIAlertController

UIAlertController *myAlertController = [UIAlertController alertControllerWithTitle:@"Title"
                                                                     message:@"Message"
                                                              preferredStyle:UIAlertControllerStyleActionSheet];

次に、アクション シートに表示する各ボタンのアクションを作成します。アクションブロックは空でもかまいませんが、ボタンのタイトルとアクションが必要です。

UIAlertAction *action1 = [UIAlertAction actionWithTitle:@"Action 1"
                                                  style:UIAlertActionStyleDefault
                                                handler:^(UIAlertAction *action) {
                                                    //Whatever you want to have happen when the button is pressed
                                                }];
[myAlertController addAction:action1];

//repeat for all subsequent actions...

UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:NSLocalizedString(@"Cancel", nil)
                                                       style:UIAlertActionStyleCancel
                                                     handler:^(UIAlertAction *action) {
                                                         // It's good practice to give the user an option to do nothing, but not necessary
                                                     }];
[myAlertController addAction:cancelAction];

最後に、次のものを提示しますUIAlertController

[self presentViewController:myAlertController
                   animated:YES
                 completion:^{

                 }];

ノート:

iPad 用にビルドし、UIAlertController にアクション シート スタイルを使用している場合は、表示元の UIAlertController のソースを設定する必要があります。これは次のように行うことができます。

if ([sender isKindOfClass:[UIView class]]) {
    if ([myAlertController.popoverPresentationController respondsToSelector:@selector(setSourceView:)]) { // Check for availability of this method
        myAlertController.popoverPresentationController.sourceView = self.myButton;
    } else {
        myAlertController.popoverPresentationController.sourceRect = self.myButton.frame;

    }
}
于 2016-02-29T13:04:27.533 に答える