我正在尝试实现一个UIActionSheet,它将有一些分享按钮上,以分享到推特,脸书,电子邮件和印刷品。
这是我的共享视图控制器.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];
}我可以很好地打开动作单,但当我点击任何一个按钮时,它就会使应用程序崩溃。有什么想法可以解释原因吗?
发布于 2012-07-17 05:15:15
这很可能是内存管理问题。看起来你使用的是ARC,所以在shareButtonTapped方法返回后,ARC会自动释放svc,因为它不再被引用。动作表的委托属性是weak,因此它也不会被保留。
我真的不明白为什么ShareViewController是一个视图控制器,因为您似乎从来没有加载过它的视图,但您可能有自己的原因……在任何情况下,您都应该使该控制器成为另一个(主)视图控制器strong属性或实例变量,这样它就不会在操作表完成之前自动释放。
发布于 2012-07-17 04:37:28
是。转到您的.xib文件。
您应该会看到您拥有的所有按钮。
单击一个按钮并转到"Connections Inspector“
删除引用插座。
将参考插座拖到按钮上,并将按钮设置为所需的"ID“。
如果有效的话,请告诉我。
发布于 2012-07-17 04:38:26
这只是一个愚蠢的答案--像-(空) sendToFacebook;这样的方法是实现的,对吧?
https://stackoverflow.com/questions/11512056
复制相似问题