2013-07-27 43 views
1

徹底難倒。 我打開一個視圖控制器的動作表,當我點擊視圖控制器中的一個按鈕啓動一個SLComposeViewController時,我得到錯誤。按鈕上的EXC Bad Access點擊

這是我的初始化動作片與視圖控制器是:

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]; 

這是ExportVC.h文件:

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


@interface ExportVC : UIViewController{ 
} 

- (IBAction)tweet:(id)sender; 


@end 

而這裏的ExportVC.m文件,其中IBAction推出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]; 
} 
} 

連接沒問題。我在ExportVC的viewdidload中沒有任何東西。每當我點擊連接到「tweet」動作的按鈕,我都會收到EXC Bad Access錯誤。 任何幫助,將不勝感激。

回答

2

這是我以前也有過的問題。它與ARC釋放內存的方式有關。

當您創建ExportVC對象並將其添加到actionSheet時,只要該方法終止,儘管視圖被添加到actionSheet,但沒有對viewController的引用。正因爲如此,在的viewController被摧毀,由於具有0保留計數,當你火tweet:(id)sender法,的viewController與方法已不存在,因此,你會得到一個EXC_BAD_ACCESS

我已經能夠方式解決這個問題的方法是創建一個對viewController的引用,我在創建alertSheet並將其設置爲viewController的類中進行創建,以便它不會被釋放。

+0

你認爲你可以給出一個這樣的代碼示例嗎? – user1520507

+0

在創建actionSheet的viewController的.h文件中,您可以添加以下內容:「@property(nonatomic,strong)ExportVC * exportView',然後在創建actionSheet時執行:self.exportView = innerView'。 – Jsdodgers

+0

這工作完美。我很感激它 – user1520507