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

我试图实现UIActionSheet将有一些共享button,以分享到微博,脸谱,电子邮件和打印。

这是我的共享视图controller.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 

这是我的共享视图controller.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; } } 

我主要在主视图controller.m中启动我的共享视图控制器,如下所示:

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

我可以打开行动表,但是当我点击任何button,它崩溃的应用程序。 任何想法,为什么?

这很可能是内存pipe理问题。 它看起来像你使用ARC,所以在shareButtonTapped方法返回后,ARC会自动释放svc ,因为它不再被引用到任何地方。 行动表的代表财产weak ,所以也不保留。

我不明白为什么ShareViewController是一个视图控制器,因为你似乎从来没有加载它的视图,但你可能有你的理由…在任何情况下,你应该使该控制器一个strong属性或实例您的其他(主)视图控制器的variables,以便在动作表完成之前不会自动释放。

是。 转到您的.xib文件。

你应该看到你所有的button。

点击一个button,然后转到“连接检查器”

删除参考sockets。

将参考sockets拖动到button,并将button设置为所需的“ID”。

让我知道这是否有效。

只是一个愚蠢的答案 – 像 – (void)sendToFacebook; 正在实施,对不对?