UIActivityViewController崩溃

时间:2013-11-16 04:54:38

标签: uiactivityviewcontroller

我不知道为什么uiactivityviewcontroller在我使用prensentViewController方法时崩溃了。

这很奇怪,任何人都有任何线索?

当我使用presenviewcontroller时,程序运行正常并非无效。

#import "ActivityViewController.h"

@interface ActivityViewController ()

@end

@implementation ActivityViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.

    [self createTextField];
    [self createButton];

}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void) createTextField
{
    self.myTextField = [[UITextField alloc] initWithFrame:CGRectMake(20.0f, 35.0f, 280.0f, 30.0f)];
    self.myTextField.translatesAutoresizingMaskIntoConstraints = NO;
    self.myTextField.borderStyle = UITextBorderStyleRoundedRect;
    self.myTextField.placeholder =@"Enter text to Share";
    self.myTextField.delegate = self;
    [self.view addSubview:self.myTextField];
}

- (void) createButton
{
    self.myButtonShare = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    self.myButtonShare.frame = CGRectMake(20.0f, 80.0f, 280.0f, 40.0f);
    self.myButtonShare.translatesAutoresizingMaskIntoConstraints = NO;


    [self.myButtonShare setTitle:@"Share" forState:UIControlStateNormal];
    [self.myButtonShare addTarget:self action:@selector(handleShare:) forControlEvents:UIControlEventTouchUpInside];

    [self.view addSubview:self.myButtonShare];



}

- (void) handleShare:(id)sender
{

    NSArray *activities = @[UIActivityTypeMail,UIActivityTypeMessage];

    self.myActivityViewController = [[UIActivityViewController alloc] initWithActivityItems:@[self.myTextField.text] applicationActivities:activities];

    [self presentViewController:self.myActivityViewController animated:YES completion:nil];

}

- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    [self.myButtonShare resignFirstResponder];
    return YES;
}

2 个答案:

答案 0 :(得分:1)

ccjensen所说的一切都是正确的,除非您实际上可以将您的共享表限制为Mail&通过排除活动的消息,例如:

activityController.excludedActivityTypes = @[ UIActivityTypeCopyToPasteboard, UIActivityTypeAddToReadingList ];

默认情况下,这只会显示电子邮件共享。

答案 1 :(得分:0)

UIActivityViewController的initWithActivityItems:applicationActivities:文档指出NSArray作为第二个参数(applicationActivities)传递的文档应该是“UIActivity数组”。您传入的数组包含两个对象UIActivityTypeMail和UIActivityTypeMessage,它们的类型为 NSString * const 。您似乎希望可以限制共享表仅显示邮件和邮件活动,这是目前无法实现的。

要停止崩溃,请将代码更改为:

<...>
self.myActivityViewController = [[UIActivityViewController alloc] initWithActivityItems:@[self.myTextField.text] applicationActivities:nil];
<...>