2012-07-16 34 views
1

我試圖實現一個UIActionSheet,它將有一些共享按鈕,以便共享到Twitter,Facebook,電子郵件和打印。單擊UIActionSheet中的按鈕導致我的應用程序崩潰

這裏是我的分享瀏覽或者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; 
} 
} 

我是個initiaalising在主視圖Controller.m或者我的份額視圖控制器如下:

- (IBAction)shareButtonTapped:(id)sender { 

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

} 

我可以打開操作表,但是當我點擊任何按鈕時,灰燼的應用程序。任何想法爲什麼?

+0

你能提供崩潰日誌嗎? – Garoal 2012-07-16 20:34:50

+0

你也應該做'self.actionsheet = [[UIActionSheet alloc] init ...];' – jacerate 2012-07-16 20:46:55

+0

jacerate,我補充說,謝謝。但主要問題仍然存在。 – 2012-07-16 20:57:34

回答

1

這很可能是內存管理問題。看起來您正在使用ARC,因此在shareButtonTapped方法返回後,ARC將自動發佈svc,因爲它不再被引用到任何地方。行動表的代表財產是weak,所以它不保留。

我真的不明白爲什麼ShareViewController是一個視圖控制器,因爲你似乎從未加載視圖,但你可能有你的理由......無論如何,你應該讓控制器成爲一個strong您的其他(主)視圖控制器的屬性或實例變量,以便它在操作表完成之前不會自動釋放。

+0

謝謝。我現在就試試這個。你是對的,ShareViewController的最初目的已經改變了,所以它不再需要成爲一個視圖控制器。我現在將它更改爲NSObject並創建主視圖控制器的實例變量 – 2012-07-16 21:54:30

1

是的。轉到您的.xib文件。

你應該看到你所有的按鈕。

點擊一個按鈕,轉到「連接檢查」

刪除引用插座。

將參考插座拖動到按鈕並將按鈕設置爲所需的「ID」。

讓我知道這是否有效。

+0

對不起,如果這是一個愚蠢的問題,但我以編程方式創建操作表。您是指按鈕,我點擊以啓動操作表或操作表中的按鈕?啓動他的操作表的按鈕位於.xib文件中,但此時可以正常工作。 – 2012-07-16 20:56:39

0

只是一個愚蠢的答案 - 像 - (void)sendToFacebook;正在實施,對嗎?

+0

他們是,是的。我只是把它們放在一邊讓它更容易閱讀。我調試它看看他們是否被調用,而他們不是。 – 2012-07-16 20:48:45

相關問題