为什么我的代表没有收到这个选择器?

时间:2012-06-30 02:03:56

标签: xcode

我有一个我不明白的错误。

我有一个课程来帮助打印作业:

//.h

@interface PrintDelegate : NSObject <UIPrintInteractionControllerDelegate, UIAlertViewDelegate>

@property (weak, nonatomic) FFDetailViewController* controller;
@property (strong, nonatomic) NSMutableData*    pdf;
@property (assign) int          pageCount;
@property (strong, nonatomic) NSArray*          fields;
@property (weak, nonatomic)   UIPrintInteractionController* printController;

- (id) initWithPageCount:(int)pc forFields:(NSArray*)flds Controller:(FFDetailViewController*)ctlr;
- (int) printFromButton: (UIBarButtonItem*) btn;
- (void) makePDF;
- (void) shift:(PixelShiftDirection)dir pixelCount:(int)amt;
- (void) adjustFields;
- (void) onPrintComplete;

@end

打印完成后,我会显示一条警告,询问用户是否要调整打印输出(并重新打印)。

//.m
- (void) onPrintComplete
{
    UIAlertView* alert = [[UIAlertView alloc] initWithTitle:@"Printing Complete" message:@"Would you like to adjust the field positions?" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"Adjust", nil];

    [alert show];
}

- (void) alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    NSString* clickedButton = [alertView buttonTitleAtIndex:buttonIndex];

    if ([clickedButton isEqualToString:@"Adjust"]) 
    {
        [self adjustFields];
    }
}

当我点击提醒中的任一按钮时,我收到类似这样的错误:

-[__NSArrayM alertView:clickedButtonAtIndex:]: unrecognized selector sent to instance

接收错误选择器的对象总是很奇怪(我也见过 NSCFArrayM和__NSMallocBlock )。选择器是UIAlertViewDelegate协议的方法。我不明白为什么选择器被发送到一些不正确的对象而不是我的PrintDelegae对象。

由于

3 个答案:

答案 0 :(得分:0)

您发布的代码对我来说没问题(尽管UIAlertView最终可能会泄漏)。根据您对错误的描述,可能是您有一些堆损坏,可能是由多次释放指向同一对象的指针引起的。

答案 1 :(得分:0)

简短的回答是,您很可能无法正确保留PrintDelegate。版本越长,您应该检查PrintDelegate实例的生命周期。它需要足够长的时间来处理clickedButton回调。您可以尝试在PrintDelegate的{​​{1}}方法中设置断点,看看它何时以及如何被调用。

答案 2 :(得分:0)

__NSArrayM是一个可变数组。您要么将错误的对象作为委托传递,要么在调用委托方法之前释放,取消分配,重新分配和重新初始化。既然你也得到NSCFArrayM,另一个可变数组,__NSMallocBlock,我推断它是一个已分配但尚未初始化的内存块,我建议第二个。在创建警报视图和解除警报视图之间检查内存管理。您可能想要尝试XCode分析和分析工具,它们非常好。