本文介绍了UIActivity activityViewController 在 iPad 上以模态方式呈现,而不是在弹出窗口中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 iOS 6 中使用客户 UIActivity 子类时,可以指定自定义视图控制器,当您从初始 UIActionViewController 的视图中选择您的操作时将显示该控制器.为此,您可以从 UIActivity 子类的 activityViewController 方法返回对自定义视图控制器的引用.

When using a customer UIActivity subclass in iOS 6, it's possible to specify a custom view controller that will be displayed when your action is chosen from the initial UIActionViewController's view. You do this by returning a reference to a custom view controller from your UIActivity subclass's activityViewController method.

根据UIActivity 类参考:

activityViewController

此方法的默认实现返回 nil.使用视图控制器提供额外 UI 的子类可以重写此方法以返回该视图控制器.如果此方法返回一个有效对象,系统会为您呈现返回的视图控制器,而不是调用 performActivity 方法.在 iPad 上,您的视图控制器显示在弹出框内.在 iPhone 和 iPod touch 上,您的视图控制器以模态方式呈现.

The default implementation of this method returns nil. Subclasses that provide additional UI using a view controller can override this method to return that view controller. If this method returns a valid object, the system presents the returned view controller for you, instead of calling the performActivity method. On iPad, your view controller is presented inside of a popover. On iPhone and iPod touch, your view controller is presented modally.

您的自定义视图控制器应该为您的自定义 UI 提供一个视图,并且应该处理这些视图中的任何用户交互.完成活动后,不要自行关闭视图控制器.相反,调用 activityDidFinish: 方法并让系统为您关闭它.

Your custom view controller should provide a view with your custom UI and should handle any user interactions inside those views. Upon completing the activity, do not dismiss the view controller yourself. Instead, call the activityDidFinish: method and let the system dismiss it for you.

请注意第一段末尾的那一点:在 iPad 上,您的视图控制器显示在弹出框内.在 iPhone 和 iPod touch 上,您的视图控制器以模态方式呈现.

Note that bit at the end of the first paragraph: On iPad, your view controller is presented inside of a popover. On iPhone and iPod touch, your view controller is presented modally.

然而,在 iPad 上,由 activityViewController 返回的视图控制器总是以模态显示,无论我如何呈现 UIActivityViewController(模态或通过弹出框).当通过弹出框呈现时,它会导致它崩溃,因为它认为它没有被关闭.

However, on iPad the view controller returned by activityViewController always displays modally, no matter how I present the UIActivityViewController (either modally or via a popover). When presenting via a popover, it causes it to crash since it doesn't think it's been dismissed.

我做错了什么?这是 iOS 6 中的错误吗?

What am I doing wrong? Is this a bug in iOS 6?

更新:这是一个说明问题的简单 Xcode 项目.随意克隆它,看看你是否能看出我们哪里出错了:github.com/simonwhitaker/GSActivityDemo

推荐答案

正如我们所说的 UIActivityViewController,它是向用户显示可用活动的视图.苹果声明如下...

As we are talking about the UIActivityViewController, which is the view showing the available activities to the user. Apple state the following...

您的应用负责配置、呈现和关闭此视图控制器.视图控制器的配置包括指定视图控制器应该操作的数据对象.(您还可以指定您的应用程序支持的自定义服务列表.)当呈现视图控制器时,您必须使用当前设备的适当方式来执行此操作.在 iPad 上,您必须在弹出窗口中显示视图控制器.在 iPhone 和 iPod touch 上,您必须以模态方式呈现.

我将最后一行作为您必须处理视图呈现方式的标志,因此我检查代码是否在 iPad 上运行并相应地使用 UIPopover.正如你可以在这里看到的那样...... https://github.com/bufferapp/buffer-uiactivity/blob/master/BufferUIActivity/Views/FirstViewController.m 在下面的方法中.

I took the last line as a sign that you have to handle how the view is presented, so I check whether the code is running on iPad and use a UIPopover accordingly. As you can sere here... https://github.com/bufferapp/buffer-uiactivity/blob/master/BufferUIActivity/Views/FirstViewController.m within the following method.

-(IBAction)openUIActivityView:(id)sender {

    NSString *text = @"Hello world";
    NSString *url = @"http://bufferapp.com";


    NSArray *activityItems = @[text, url];

    BufferUIActivity *bufferActivity = [[BufferUIActivity alloc] init];

    UIActivityViewController *activityView = [[UIActivityViewController alloc] initWithActivityItems:activityItems applicationActivities:@[ bufferActivity ]];


    if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone) {
        [self presentViewController:activityView animated:YES completion:^{

        }];
    } else {
        // Change Rect to position Popover
        self.popup = [[UIPopoverController alloc] initWithContentViewController:activityView];
        [self.popup presentPopoverFromRect:CGRectMake(self.view.frame.size.width/2, self.view.frame.size.width/2, 100, 100) inView:self.view permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
    }

}

这篇关于UIActivity activityViewController 在 iPad 上以模态方式呈现,而不是在弹出窗口中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-18 00:09
查看更多