【问题标题】:How to show the actionSheet originating from UiButton如何显示源自 UiButton 的 actionSheet
【发布时间】:2016-05-12 14:51:36
【问题描述】:

我有一个 uiButton,我想显示一个源自它的操作表。 这是我的关闭按钮代码,

+ (void)addLeftButton:(UINavigationItem *)navItem withTarget:(id) navTarget
{
    UIBarButtonItem *fixedSpace = [[UIBarButtonItem alloc]   initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
    [fixedSpace setWidth:-5];

    UIImage *buttonImage = [UIImage imageNamed:@"close-button.png"];

    UIButton *closeButton = [UIButton buttonWithType:UIButtonTypeCustom];

    [closeButton setBackgroundImage:buttonImage forState:UIControlStateNormal];

    closeButton.frame = CGRectMake(0.0, 0.0, buttonImage.size.width,   buttonImage.size.height);

    UIBarButtonItem *aBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:closeButton];

    [closeButton addTarget:navTarget action:@selector(didSelectCloseButton:) forControlEvents:UIControlEventTouchUpInside];

    [navItem setLeftBarButtonItems:[NSArray arrayWithObjects:fixedSpace,aBarButtonItem,nil]];
}

这个 IBAction 用于关闭按钮,

(IBAction)didSelectCloseButton:(id)sender {

if([self->_customer getCheckInDraft])
{
    [self showActionSheet:sender];
}

在 showActionSheet 我有这段代码,

(void) showActionSheet:(id)sender

{

         if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
                [self->_actionSheet showFromBarButtonItem:sender animated:YES];

}

//这里 showFromBarButtonItem 将发送者作为 uiButton 获取,但它崩溃了。单击关闭按钮时应用程序崩溃。

【问题讨论】:

  • 提前致谢...
  • 你在控制台崩溃时得到了什么?错误?还有你为什么不使用 UIActionSheet
  • 我在崩溃时收到:“无法识别的选择器发送到实例”错误消息。为了显示我正在使用“showFromBarButtonItem”的操作表,但我使用的按钮不是 barButtonItem。所以它抛出了一个错误。我应该使用'showFromRect' insted。

标签: objective-c ipad


【解决方案1】:

这是操作表代码。

- (IBAction)showActionSheet:(id)sender {

    /* Dismiss keyboard*/

    UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:@"What ever you want to ask?" delegate:self cancelButtonTitle:@"Cancel" destructiveButtonTitle:@"Delete it" otherButtonTitles:@"Copy", @"Duplicate", nil]; //Change the options that you want to display as per your requirement.

    if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
        // In this case the device is an iPad.
        [actionSheet showFromRect:[(UIButton *)sender frame] inView:self.view animated:YES];
    }
    else{
        // In this case the device is an iPhone/iPod Touch.
        [actionSheet showInView:self.view];
    }

}

#pragma mark - UIActionSheet method implementation

-(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex{

  NSLog(@"Index = %d - Title = %@", buttonIndex, [actionSheet buttonTitleAtIndex:buttonIndex]);
}

-(void)actionSheet:(UIActionSheet *)actionSheet didDismissWithButtonIndex:(NSInteger)buttonIndex{
   NSLog(@"Action sheet did dismiss");
 }


-(void)actionSheet:(UIActionSheet *)actionSheet willDismissWithButtonIndex:(NSInteger)buttonIndex{
   NSLog(@"Action sheet will dismiss");
}

/* 更新 */

Above 在 iOS 8.3 及更高版本中已弃用,因此您可以在 UIAlertController 下方使用。

此外,如果您的键盘在用户点击按钮时打开,则键盘会自动关闭并让 UIAlertController 显示选项。

UIAlertController *alertController;
    UIAlertAction *destroyAction;
    UIAlertAction *otherAction;

    alertController = [UIAlertController alertControllerWithTitle:nil
                                                          message:nil
                                                   preferredStyle:UIAlertControllerStyleActionSheet];
    destroyAction = [UIAlertAction actionWithTitle:@"Remove All Data"
                                             style:UIAlertActionStyleDestructive
                                           handler:^(UIAlertAction *action) {
                                               // do destructive stuff here
                                           }];
    otherAction = [UIAlertAction actionWithTitle:@"Blah"
                                           style:UIAlertActionStyleDefault
                                         handler:^(UIAlertAction *action) {
                                             // do something here
                                         }];
    // note: you can control the order buttons are shown, unlike UIActionSheet
    [alertController addAction:destroyAction];
    [alertController addAction:otherAction];
    [alertController setModalPresentationStyle:UIModalPresentationPopover];

    UIPopoverPresentationController *popPresenter = [alertController
                                                     popoverPresentationController];

    UIButton *btn = (UIButton *)sender;

    popPresenter.sourceView = btn;
    popPresenter.sourceRect = [btn bounds];
    [self presentViewController:alertController animated:YES completion:nil];

【讨论】:

  • 当键盘未在模拟器中打开时看起来很完美,但是当为模拟器打开软件键盘时,操作表以不同的方式显示。
  • @Nikhil : 我已经用最新的方法 (UIAlertController) 更新了代码并尝试了一个演示以便它工作。
  • 我已经通过关闭按钮单击时的键盘来解决键盘问题。因此,操作表正确显示,因为现在 keybaord 存在。下次我将尝试 UIAlertController 而不是 ActionSheet。
【解决方案2】:

你可以这样做:

UIAlertAction *action1;
UIAlertAction *actionCancel;

action1 = [UIAlertAction actionWithTitle:@"action1"                                              style:UIAlertActionStyleDefault handler:nil];                                         
actionCancel = [UIAlertAction actionWithTitle:@"cancel" style:UIAlertActionStyleDestructive handler:nil];

UIAlertController *alertView = [UIAlertController alertControllerWithTitle:@"alert" message:nil preferredStyle:UIAlertControllerStyleActionSheet];

alertView.modalPresentationStyle = UIModalPresentationPopover; // for iPad

[alertView addAction:action1];
[alertView addAction:actionCancel];

if (viewSourse) { // Here your button 

    alertView.popoverPresentationController.sourceView = viewSourse;
    alertView.popoverPresentationController.sourceRect =  CGRectMake(viewSourse.frame.size.width/2, viewSourse.frame.size.height/2, 0, 0);
}

 [self presentViewController:alertView animated:YES completion:nil];

【讨论】:

    猜你喜欢
    • 2016-10-06
    • 2016-12-15
    • 1970-01-01
    • 2023-03-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-07-09
    相关资源
    最近更新 更多