Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/26.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Objective c 如何显示源自UiButton的操作表_Objective C_Ipad - Fatal编程技术网

Objective c 如何显示源自UiButton的操作表

Objective c 如何显示源自UiButton的操作表,objective-c,ipad,Objective C,Ipad,我有一个uiButton,我想显示一个源自它的操作表。 这是我的关闭按钮代码 + (void)addLeftButton:(UINavigationItem *)navItem withTarget:(id) navTarget { UIBarButtonItem *fixedSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil a

我有一个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]];
}
此操作用于关闭按钮

+ (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]];
}
(iAction)选择关闭按钮:(id)发送者 {

在showActionSheet中我有这个代码

(void) showActionSheet:(id)sender

{

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

}

//此处showFromBarButtonItem以uiButton的形式获取发件人,但它会崩溃。单击“关闭”按钮时,应用程序会崩溃。

您可以这样做:

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];

这是行动表代码

- (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");
}
/*更新*/

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];

提前感谢…崩溃时控制台中出现了什么?错误?还有,为什么不使用UIActionSheet?我得到了:“无法识别的选择器发送到实例”崩溃时出现错误消息。要显示操作表,我使用的是“showFromBarButtonItem”,但我使用的按钮不是barButtonItem。因此它引发了一个错误。我本应使用“showFromRect”替代。当模拟器中的键盘未打开时,它看起来非常完美,但当模拟器的软件键盘打开时,操作表显示为shown以不同的方式。@Nikhil:我已经用最新的方法(UIAlertController)更新了代码,并在一个演示上进行了尝试,以使其正常工作。我通过在按钮单击时取消键盘上的键值来解决了键盘问题。因此,操作表正确显示为现在键值存在。下次我将尝试UIAlertController而不是操作表。