UIPopover如何用这样的按钮制作一个popover ?

  • 回答 (2)
  • 关注 (0)
  • 查看 (129)

我想知道如何用这样的按钮来创建一个弹出框。

答:

UIActionSheet * actionSheet = [[UIActionSheet alloc] initWithTitle: nil 
                                                          delegate: self
                                                 cancelButtonTitle: nil 
                                            destructiveButtonTitle: nil 
                                                 otherButtonTitles: @"Take Photo",
                                                                    @"Choose Existing Photo", nil];

[actionSheet showFromRect: button.frame inView: button.superview animated: YES];

委托对象类中的其他地方..。

-(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {
    if (buttonIndex == 0) {
         // take photo...
    } 
    else if (buttonIndex == 1) {
         // choose existing photo...
    }
}
athletesssathletesss提问于
工口Miku说唱歌手回答于

类似于其他的回应,但这是很容易实现的比较。

使您的类使用UIActionSheetDelegate。

例子:

@interface ExampleViewController : UIViewController <UIActionSheetDelegate>

然后添加到ExampleViewController.mm/m中

- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex  { //Get the name of the current pressed button 
NSString *buttonTitle = [actionSheet buttonTitleAtIndex:buttonIndex]; 
if  ([buttonTitle isEqualToString:@"Remove"]) {
    NSLog(@"Remove this actionSheet"); } 
if ([buttonTitle isEqualToString:@"Button 1"]) {
    NSLog(@"Button 1 pressed"); } 
if ([buttonTitle isEqualToString:@"Button 2"]) {
    NSLog(@"Button 2 pressed"); }
if ([buttonTitle isEqualToString:@"Button 3"]) {
    NSLog(@"Button 3 pressed"); } 
if ([buttonTitle isEqualToString:@"Cancel"]) {
    NSLog(@"Cancel clicked (anywhere away from it)"); } }

现在,在按钮按下的事件中,或者在需要弹出事件的位置/时间,请调用以下命令:

    - (IBAction)aButtonPressed:(id)sender {
     NSString *actionSheetTitle = @"Action Sheet"; // Title 
     NSString *destroyTitle = @"Destroy"; // Button titles
     NSString *button1 = @"Button 1"; 
     NSString *button2 = @"Button 2";
     NSString *button3 = @"Button 3";
     NSString *cancelTitle = @"Cancel"; 
    UIActionSheet *actionSheet = [[UIActionSheet alloc]
                                  initWithTitle:actionSheetTitle
                                   delegate:self
                                   cancelButtonTitle:cancelTitle
                                   destructiveButtonTitle:destroyTitle
                                   otherButtonTitles:button1, button2, button3, nil]; [actionSheet showInView:self.view];
}

更多有关这方面的信息:http://developer.apple.com/library/ios/#documentation/uikit/reference/UIActionSheet_Class/Reference/Reference.html

NAMIHEAPTX4869回答于

这是一个UIActionSheet.在iPhone上,它从底部进入。在iPad上,它出现在一个弹出框中。

假设你是按下按钮就这样做的:

UIActionSheet * actionSheet = [[UIActionSheet alloc] initWithTitle: nil 
                                                          delegate: self
                                                 cancelButtonTitle: nil 
                                            destructiveButtonTitle: nil 
                                                 otherButtonTitles: @"Take Photo",
                                                                    @"Choose Existing Photo", nil];

[actionSheet showFromRect: button.frame inView: button.superview animated: YES];

在iOS 8+中,您应该使用新的UIAlertController类

UIAlertController * alertController = [UIAlertController alertControllerWithTitle: nil
                                                                          message: nil
                                                                   preferredStyle: UIAlertControllerStyleActionSheet];
[alertController addAction: [UIAlertAction actionWithTitle: @"Take Photo" style: UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
    // Handle Take Photo here
}]];
[alertController addAction: [UIAlertAction actionWithTitle: @"Choose Existing Photo" style: UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
    // Handle Choose Existing Photo here
}]];

alertController.modalPresentationStyle = UIModalPresentationPopover;

UIPopoverPresentationController * popover = alertController.popoverPresentationController;
popover.permittedArrowDirections = UIPopoverArrowDirectionUp;
popover.sourceView = sender;
popover.sourceRect = sender.bounds;

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

或者在Swift上:

let alertController = UIAlertController(title: nil, message: nil, preferredStyle: .ActionSheet)
alertController.addAction(UIAlertAction(title: "Take Photo", style: .Default, handler: { alertAction in
    // Handle Take Photo here
    }))
alertController.addAction(UIAlertAction(title: "Choose Existing Photo", style: .Default, handler: { alertAction in
    // Handle Choose Existing Photo
    }))
alertController.modalPresentationStyle = .Popover

let popover = alertController.popoverPresentationController!
popover.permittedArrowDirections = .Up
popover.sourceView = sender
popover.sourceRect = sender.bounds

presentViewController(alertController, animated: true, completion: nil)

所属标签

可能回答问题的人

  • 腾讯云数据库 TencentDB

    腾讯云 · 产品团队 (已认证)

    302 粉丝2 提问51 回答
  • EatRice

    17 粉丝0 提问183 回答
  • 华讯云

    1 粉丝0 提问156 回答
  • 成都加米谷大数据

    11 粉丝0 提问16 回答

扫码关注云+社区

领取腾讯云代金券