单击UIActionSheet中的按钮会导致我的应用程序崩溃



我正在尝试实现一个UIActionSheet,它上面有一些共享按钮,可以共享到twitter、facebook、电子邮件和打印。

这是我的共享视图控制器.h

@interface ShareViewController : UITableViewController <UIActionSheetDelegate, MFMailComposeViewControllerDelegate>
@property (nonatomic, strong) NSMutableDictionary *services;
@property (strong, nonatomic) UIActionSheet *actionSheet;
@property (strong, nonatomic) id <ShareViewControllerDelegate> delegate;
@property (nonatomic, strong) UIPopoverController *popCon;
@property (nonatomic, strong) NSString *serviceTitle;
-(IBAction)loadActionSheet:(id)sender;
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex;
- (void)tweetThis;
- (void)printThis;
- (void)openMail;
- (void)mailComposeController:(MFMailComposeViewController*)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError*)error;

@end

这是我的共享视图控制器.m

-(IBAction)loadActionSheet:(id)sender{
if (self.actionSheet) {
            // do nothing
} 
else {
UIActionSheet *sharing = [[UIActionSheet alloc] 
                          initWithTitle:nil 
                          delegate:self 
                          cancelButtonTitle:nil 
                          destructiveButtonTitle:nil 
                          otherButtonTitles:@"Twitter", @"Facebook", @"Email", @"Print", nil];
[sharing showFromBarButtonItem:sender animated:YES];
}
}
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
     NSLog(@"Button index: %d",buttonIndex);
switch (buttonIndex) {
    case 0:
        [self tweetThis];
        break;
    case 1:
        [self sendToFacebook];
        break;
    case 2:
        NSLog(@"email");
        [self openMail];
        break;
    case 3:
        NSLog(@"Print");
        [self printThis];
        break;
    default:
        NSLog(@"Error");
        break;
}
}

我正在主视图控制器.m中初始化我的共享视图控制器,如下所示:

- (IBAction)shareButtonTapped:(id)sender {
ShareViewController *svc = [[ShareViewController alloc] initWithStyle:UIActionSheetStyleDefault];
[svc loadActionSheet:(id)sender];
}

我可以很好地打开动作表,但当我点击任何按钮时,它都会崩溃应用程序。有什么原因吗?

这很可能是内存管理问题。看起来您正在使用ARC,所以在shareButtonTapped方法返回后,ARC将自动释放svc,因为它不再在任何地方被引用。操作表的委托属性是weak,因此它也不被保留。

我真的不明白为什么ShareViewController是一个视图控制器,因为你似乎从来没有加载过它的视图,但你可能有自己的原因。。。在任何情况下,都应该使该控制器成为另一个(主)视图控制器的strong属性或实例变量,这样在操作表完成之前就不会自动释放它。

是。转到你的.xib文件

你应该看到你所有的按钮

点击按钮并进入"连接检查器"

删除引用出口

将参考插座拖动到按钮上,然后将按钮设置为所需的"ID"

如果可行,请告诉我。

只是一个愚蠢的答案-像-(void)sendToFacebook这样的方法;执行,对吧?

最新更新