繁体   English   中英

转换为ARC后,我收到“发送到已释放实例的消息”

[英]After converting to ARC I am getting “message sent to deallocated instance”

我正在创建这样的View实例...

- (IBAction)findPatientTapped:(id)sender { 

    RequestDialogViewController *findPatientViewController = [[RequestDialogViewController alloc] initWithNibName:@"RequestDialogViewController"    bundle:nil];

    findPatientViewController.delegate = self;
    findPatientViewController.autoCorrectOff = YES;
    findPatientViewController.defaultResponse = @"";
    findPatientViewController.keyboardType = @"ASCII";
    findPatientViewController.returnKeyType = @"Go";
    findPatientViewController.tag = findRequestTag;
    findPatientViewController.editSet = YES;
    findPatientViewController.titleText = @"Find Patient";
    findPatientViewController.messageText =@"Enter all or a portion of the patient's name...";
    findPatientViewController.messageText2 = @"Last Name...";
    findPatientViewController.messageText3 = @"First Name...";
    findPatientViewController.showResponse2 = YES;   
    findPatientViewController.showNavBarSaveButton = NO;
    findPatientViewController.infoTextFile = @"";
    findPatientViewController.view.frame = CGRectMake(280, 230, 480, 300);

    [self.view addSubview:findPatientViewController.view];
}

该视图包含2个UITextField字段,供用户输入。 在运行转换为ARC工具之前,所有这些工作都很好。 转换后,尝试在两个字段中的任意一个中输入值时,视图崩溃。

-[RequestDialogViewController responsesToSelector:]:发送到已释放实例的消息

这是运行转换为ARC工具后的UIViewController的.h文件...

#import <UIKit/UIKit.h>
@protocol RequestDialogViewControllerDelegate;

@interface RequestDialogViewController : UIViewController <UITextFieldDelegate>{
    id<RequestDialogViewControllerDelegate> __weak delegate;
    IBOutlet UINavigationBar *navBar;
    IBOutlet UITextField *response;
    IBOutlet UITextField *response2;
    IBOutlet UILabel *message;
    IBOutlet UILabel *message2;
    IBOutlet UILabel *message3;
    IBOutlet UIButton *saveButton;
    NSTimer *selectAllTimer;

    NSString *defaultResponse;
    NSString *titleText, *infoTextFile;
    NSString *messageText, *messageText2, *messageText3;
    NSString *placeHolderText;
    NSString *keyboardType, *returnKeyType;
    BOOL editSet, showSaveButton,showNavBarSaveButton, showResponse2, autoCorrectOff;
    int tag;
}

@property (weak) id<RequestDialogViewControllerDelegate> delegate;
@property (nonatomic, strong)IBOutlet UITextField *response;
@property (nonatomic, strong) IBOutlet UITextField *response2;
@property (nonatomic, strong)IBOutlet UILabel *message;
@property (nonatomic, strong)IBOutlet UILabel *message2;
@property (nonatomic, strong) IBOutlet UILabel *message3;
@property (nonatomic, strong)IBOutlet UIButton *saveButton;
@property (nonatomic, strong)NSString *defaultResponse;
@property (nonatomic, strong)NSString *titleText, *infoTextFile;
@property (nonatomic, strong)NSString *messageText, *messageText2, *messageText3;
@property (nonatomic, strong)NSString *placeHolderText;
@property (nonatomic, strong)NSString *keyboardType, *returnKeyType;
@property (nonatomic, strong)IBOutlet UINavigationBar *navBar;
@property (readwrite)BOOL editSet, showSaveButton, showNavBarSaveButton, showResponse2, autoCorrectOff;
@property (readwrite)int tag;
@property (nonatomic, strong) NSTimer *selectAllTimer;

- (IBAction)saveButtonPressed:(id)sender;
- (void)selectAll;
- (IBAction)editingDidEnd:(id)sender;

@end


@protocol RequestDialogViewControllerDelegate <NSObject>
@optional
- (void)requestDialogViewDidDismiss:(RequestDialogViewController *)controller withResponse:(NSString*)reqResponse response2:(NSString*)reqResponse2;
@end

这是创建RequestDialog视图实例的类.h文件中的持久性引用...

#import "RequestDialogViewController.h"

@interface OrthoViewController : UIViewController <RequestDialogViewControllerDelegate>{
}
- (void)requestDialogViewDidDismiss:(RequestDialogViewController *)controller withResponse:(NSString*)reqResponse response2:(NSString*)reqResponse2;
@end

要使它在ARC下工作,我需要做什么? 我认为这可能与协议的形成有关。

谢谢,

约翰

****感谢Dan,我通过使findPatientViewController成为调用类的属性来解决了这个问题。

RequestDialogViewController *findPatientViewController;
@implementation OrthoViewController

- (IBAction)findPatientTapped:(id)sender {    
    findPatientViewController = [[RequestDialogViewController alloc] initWithNibName:@"RequestDialogViewController" bundle:nil];
    //set all the properties and add the subview
 }
@end

您的findPatientViewController没有保留任何内容,因此在创建它的方法末尾将其释放。 然后,当视图中的某项尝试在其上调用委托方法时,您将崩溃。

如果findPatientTapped是视图控制器中的方法,则应将findPatientViewController添加为子视图控制器。 如果它在视图中,那么您需要最少将findPatientViewController存储在一个属性中,以便在您仍在使用它时不会将其释放。

在ARC之前,您的代码无法真正正常工作,只是发生了内存泄漏。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM