转换为 ARC 后,我得到 "message sent to deallocated instance"
After converting to ARC I am getting "message sent to deallocated instance"
我正在创建一个这样的视图实例...
- (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 工具之前,这一切都运行良好。转换后,尝试在 2 个字段中的任一字段中输入值时视图崩溃...
-[RequestDialogViewController respondsToSelector:]: message sent to deallocated instance
这是 运行 转换为 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
这是 class 创建 RequestDialog 视图实例的 .h 文件中的相关引用...
#import "RequestDialogViewController.h"
@interface OrthoViewController : UIViewController <RequestDialogViewControllerDelegate>{
}
- (void)requestDialogViewDidDismiss:(RequestDialogViewController *)controller withResponse:(NSString*)reqResponse response2:(NSString*)reqResponse2;
@end
我需要做什么才能在 ARC 下工作?我想这可能与协议的形成方式有关。
谢谢,
约翰
**** 感谢 Dan,我通过将 findPatientViewController 设置为调用 class...
的 属性 解决了这个问题
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 之前并没有真正正常工作,您只是发生了内存泄漏。
我正在创建一个这样的视图实例...
- (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 工具之前,这一切都运行良好。转换后,尝试在 2 个字段中的任一字段中输入值时视图崩溃...
-[RequestDialogViewController respondsToSelector:]: message sent to deallocated instance
这是 运行 转换为 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
这是 class 创建 RequestDialog 视图实例的 .h 文件中的相关引用...
#import "RequestDialogViewController.h"
@interface OrthoViewController : UIViewController <RequestDialogViewControllerDelegate>{
}
- (void)requestDialogViewDidDismiss:(RequestDialogViewController *)controller withResponse:(NSString*)reqResponse response2:(NSString*)reqResponse2;
@end
我需要做什么才能在 ARC 下工作?我想这可能与协议的形成方式有关。
谢谢,
约翰
**** 感谢 Dan,我通过将 findPatientViewController 设置为调用 class...
的 属性 解决了这个问题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 之前并没有真正正常工作,您只是发生了内存泄漏。