1

Thoroughly stumped. I open an action sheet with a view controller in it, and when I click a button in the view controller to launch an SLComposeViewController, I get the error.

This is how I initialize the action sheet with the view controller in it:

UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:nil
                                                         delegate:self
                                                cancelButtonTitle:nil

                                           destructiveButtonTitle:nil
                                                otherButtonTitles:@"1",@"2",nil];
[actionSheet setBounds:CGRectMake(0,0, 320, 285)];
ExportVC*innerView = [[ExportVC alloc] initWithNibName:@"ExportVC" bundle:nil];
innerView.view.frame = actionSheet.bounds;
[actionSheet addSubview:innerView.view];

[actionSheet showFromTabBar:self.tabBarController.tabBar];

This is the ExportVC.h file:

#import <UIKit/UIKit.h>
#import <Social/Social.h>


@interface ExportVC : UIViewController{
}

- (IBAction)tweet:(id)sender;


@end

And here's the ExportVC.m file, where the IBAction launches the SLComposeViewController:

#import "ExportVC.h"
#import <Social/Social.h>


@interface ExportVC ()

@end

@implementation ExportVC



-(IBAction)tweet:(id)sender{
NSLog(@"button works");
if ([SLComposeViewController isAvailableForServiceType:SLServiceTypeTwitter])
{
    SLComposeViewController* tweetSheet = [SLComposeViewController
                                           composeViewControllerForServiceType:SLServiceTypeTwitter];
    [tweetSheet setInitialText:@"Hi"];
    [self presentViewController:tweetSheet animated:YES completion:nil];
}
}

The connections are fine. I don't have anything in the viewdidload of the ExportVC. Whenever I click the button attached to the "tweet" action however, I get the EXC Bad Access error. Any help would be appreciated.

4

1 回答 1

2

这也是我之前遇到的问题。它与 ARC 释放内存的方式有关。

当您创建ExportVC对象并将其添加到 actionSheet 时,一旦方法终止,虽然视图已添加到 actionSheet,但没有对 viewController 的引用。因此,viewController 由于保留计数为 0 而被销毁,并且当您触发该tweet:(id)sender方法时,具有该方法的 viewController 不再存在,因此您会得到一个EXC_BAD_ACCESS

我能够解决此问题的方法是创建对我在创建 alertSheet 的类中创建的 viewController 的引用,并将其设置为 viewController 以便它不会被释放。

于 2013-07-27T21:14:35.253 回答