【问题标题】:UIPopover How do I make a popover with buttons like this?UIPopover 如何使用这样的按钮制作弹出框?
【发布时间】:2012-05-31 10:28:54
【问题描述】:

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

回答:

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...
    }
}

【问题讨论】:

    标签: ios ipad uipopovercontroller uiactionsheet uialertcontroller


    【解决方案1】:

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

    在 iOS8+ 中,您应该使用新的 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)
    

    【讨论】:

    • 我是否只是将其添加到 Popover 视图中?
    • 不,只需使用 UIActionSheet showFrom... 方法之一。有关示例,请参阅我的更新答案
    • 好的,还有 1 个问题,如何为按钮设置事件处理程序?
    • 可以将新的 Facebook 分享按钮放在 UIAlertController 中,还是我需要编写一个客户弹出框?我尝试了第二种方法,因为 Storyboard 似乎在链接到特定 UITableViewCell 的弹出框上阻塞并且无法编译。
    【解决方案2】:

    与其他响应类似,但相比之下这很容易实现。

    让您的班级使用 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

    【讨论】:

    • 别忘了添加 [actionSheet showFromRect:[(UIButton *)sender frame] inView:self.view animated:YES];将弹出框附加到发件人按钮。
    猜你喜欢
    • 2020-05-27
    • 2011-08-20
    • 2013-04-20
    • 2021-08-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多