从UIViewController文件移动到单独的类文件时,UIActionSheet代码崩溃

时间:2012-07-01 19:34:02

标签: iphone ios

我搜查并搜查了董事会,但我无法弄清楚这一点。它必须在我面前简单明了。

我正在尝试清理我的代码并使其更具可重用性。我正在使用一些UIActionSheet代码,它可以从UIViewController中运行并创建自己的目标文件。工作正常,直到我添加UIActionSheetDelegate方法。

按下按钮时,它不会触发actionSheetCancel方法,而是在没有堆栈跟踪的情况下崩溃。每一次。

我的代码如下。任何帮助,将不胜感激。我的猜测是因为我没有使用xcode故事板工具将事物连接在一起,但我认为这是合法的。

egcTestSheet.h:

#import <UIKit/UIKit.h>

@interface egcTestSheet : NSObject <UIActionSheetDelegate> {

}

- (void) showSheet:(UITabBar *) tabBar
    displayTitle:(NSString *) name;

@end

egcTestSheet.m

#import "egcTestSheet.h"

@implementation egcTestSheet

-(void) showSheet:(UITabBar *)tabBar displayTitle:(NSString *)name{

    UIActionSheet *menu = [[UIActionSheet alloc] initWithTitle:name
                                                      delegate:self
                                             cancelButtonTitle:@"Done"
                 destructiveButtonTitle:@"Cancel"otherButtonTitles:nil];

   [menu showFromTabBar:tabBar];
   [menu setBounds:CGRectMake(0,0,320, 700)];

}

// actionsheet delegate protocol item
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex: (NSInteger)buttonIndex{

    NSLog(@"button index = %d", buttonIndex);
}


- (void)actionSheetCancel:(UIActionSheet *)actionSheet{

    NSLog(@"in action canceled method");
}

@end

从UIViewController对象调用代码:

egcTestSheet *sheet = [[egcTestSheet alloc] init];

[sheet showSheet:self.tabBarController.tabBar displayTitle:@"new test"];

1 个答案:

答案 0 :(得分:6)

你的行动表可能正在被解雇(你使用ARC吗?)。这意味着当它试图呼叫它的代表通知所述代表其解雇/选择时,它试图打电话给自己。此时自我是一个悬垂的指针,因为它已被释放。

在显示/调用此操作表的视图控制器中,设置属性以保留对操作表的引用。在解雇操作表时将属性设置为nil。