簡體   English   中英

選擇項目時,UIActionSheet崩潰

[英]UIActionSheet crash when selecting item

我有一個以編程方式添加到我的UIViewControllerUIView 該視圖包含一些按鈕,其中一個按鈕可打開UIActionSheet

當我點擊任一UIActionSheet選項時,該應用程序立即崩潰,然后再調用委托。

我假設這是一個內存問題,但我想我已經解決了所有問題。

  • 自定義UIView是父視圖控制器上的一個強大屬性。
  • 從父視圖控制器的選項卡欄顯示操作表。
  • 操作表委托是UIView ,並且委托方法已正確實現。

錯誤是EXC_BAD_ACCESS。 ([ContactOptionView actionSheet:clickedButtonAtIndex:]:發送到已釋放實例的消息)

任何幫助,不勝感激。

使用此代碼創建操作表,其中CDField對象是核心數據NSManagedObject:

UIActionSheet *sheet = [[UIActionSheet alloc] init];
sheet.title = title;
for (CDField *field in fields) {
    [sheet addButtonWithTitle:field.value];
}
sheet.cancelButtonIndex = [sheet addButtonWithTitle:@"Cancel"];
sheet.delegate = self;
[sheet showFromTabBar:self.parentViewController.tabBarController.tabBar];

將ActionSheet聲明為強類屬性而不是本地聲明可能會解決此問題,因為會延長其生存時間。

@interface MyTableViewController ()

@property (strong, nonatomic) UIActionSheet *actionSheet;

@end    
@implementation MyViewController
@synthesize actionSheet = _actionSheet;
- (UIActionSheet *)actionSheet {
    if (!_actionSheet) {
        _actionSheet = [[UIActionSheet alloc] initWithTitle:@"title 
                                                   delegate:self
                                          cancelButtonTitle:@"Cancel"
                                     destructiveButtonTitle:nil 
                                          otherButtonTitles:@"do something", nil), nil];
    }
    return _actionSheet;
}

彼得

我在@Larme的幫助下解決了我的問題。 我要添加到視圖控制器中的UIView是通過長按手勢創建的,當長按手勢結束時,將重新分配視圖。 在顯示操作表時,用戶必須將手指從屏幕上移開以點擊所選的選項,並且此時要重新分配操作表的委托(視圖)。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM