点击UIActionSheet一个按钮使我的应用程序崩溃 [英] Clicking a button in UIActionSheet causes my app to crash

查看:103
本文介绍了点击UIActionSheet一个按钮使我的应用程序崩溃的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想实现一个UIActionSheet这将有一些关于它的共享按钮共享的Twitter,Facebook,电子邮件和打印。

I'm trying to implement a UIActionSheet which will have some sharing buttons on it for sharing to twitter, facebook, email and print.

下面是我的分享浏览或者Controller.h

Here is my share view 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或者

Here is my share view 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;
}
}

我是个initiaalising在主视图Controller.m或者我的份额视图控制器如下:

I ma initiaalising my share view controller in main view controller.m as follows:

- (IBAction)shareButtonTapped:(id)sender {

ShareViewController *svc = [[ShareViewController alloc] initWithStyle:UIActionSheetStyleDefault];
[svc loadActionSheet:(id)sender];

}

我可以打开的动作片很好,但是当我点击任何按钮,它崩溃的应用程序。任何想法,为什么?

I can open the action sheet fine, but when I click on any of the buttons, it crashes the app. Any ideas as to why?

推荐答案

这是最有可能是内存管理的问题。它看起来像你使用ARC,因此 shareButtonTapped 方法返回后,ARC会自动释放 SVC ,因为它是不再在任何地方引用。动作片的代表属性是,所以它不是由保存方式。

This is most likely a memory-management issue. It looks like you're using ARC, so after the shareButtonTapped method returns, ARC will automatically release svc, as it's no longer referenced anywhere. The delegate property of the action sheet is weak, so it's not retained by that either.

我真的不明白为什么 ShareViewController 是摆在首位视图控制器,你似乎永远不会被加载其观点,但你可能有你的理由。 。在任何情况下,你应该让该控制器的其它(主)视图控制器的强烈属性或实例变量,因此,它不是自动的动作片结束前公布

I don't really see why ShareViewController is a view controller in the first place, as you never seem to be loading its view, but you may have your reasons... In any case, you should make that controller a strong property or instance variable of your other (main) view controller, so that it's not automatically released before the action sheet finishes.

这篇关于点击UIActionSheet一个按钮使我的应用程序崩溃的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆