按钮IOS上的操作表选择器

按钮IOS上的操作表选择器,ios,actionsheetpicker,Ios,Actionsheetpicker,HeyI想这样做,我的按钮,在按钮有文本字段,我想这样做,当我按下按钮时,动作表选择器会出现,并给出4到5个字符串列表,无论我选择什么,它都会显示在按钮中的文本字段上。请帮帮我 首先为按钮添加一个目标。在Objective-C中,是这样的: [myButton addTarget:self action:@selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside]; if ([se

HeyI想这样做,我的按钮,在按钮有文本字段,我想这样做,当我按下按钮时,动作表选择器会出现,并给出4到5个字符串列表,无论我选择什么,它都会显示在按钮中的文本字段上。请帮帮我


首先为按钮添加一个目标。在Objective-C中,是这样的:

[myButton addTarget:self
             action:@selector(buttonPressed:)
   forControlEvents:UIControlEventTouchUpInside];
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;

    }
}
然后创建方法
按钮按下
。这方面的一个例子是:

- (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];
[self presentViewController:myAlertController
                   animated:YES
                 completion:^{

                 }];
然后,为要在操作表上显示的每个按钮创建操作。您需要为按钮提供标题和操作,尽管操作块可以为空

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

UIAlertController *myAlertController = [UIAlertController alertControllerWithTitle:@"Title"
                                                                     message:@"Message"
                                                              preferredStyle:UIAlertControllerStyleActionSheet];
[self presentViewController:myAlertController
                   animated:YES
                 completion:^{

                 }];
注:

如果您正在为iPad构建并使用UIAlertController的操作表样式,那么您需要为UIAlertController设置一个要从中显示的源。可以这样做:

[myButton addTarget:self
             action:@selector(buttonPressed:)
   forControlEvents:UIControlEventTouchUpInside];
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;

    }
}

单击“操作表”按钮时,设置文本字段。text=@“按钮标题”还值得注意的是,如果要在iPad上运行,则必须声明
popoverPresentation
。通常为条形按钮项,或定义
sourceRect
sourceView
。它将以其他方式在iPad上崩溃,这是一个极好的观点。我会在我的回答中加上这一条。