转换为ARC后,我收到了#34;消息被发送到解除分配的实例"

时间:2015-06-13 19:31:26

标签: ios objective-c xcode6 automatic-ref-counting

我正在创建像这样的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工具之前,这一切都运行良好。转换后,当尝试使用...

在2个字段中的任何一个字段中输入值时,视图会崩溃
  

- [RequestDialogViewController respondsToSelector:]:发送到解除分配的实例的消息

以下是运行转换为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

1 个答案:

答案 0 :(得分:1)

您的findPatientViewController没有任何保留它,因此它会在您创建它的方法的末尾取消分配。然后,当它的视图中的某些东西试图在其上调用委托方法时,你就会崩溃。

如果findPatientTapped是视图控制器中的方法,则应将findPatientViewController添加为子视图控制器。如果它在视图中,那么您需要至少将findPatientViewController存储在属性中,以便在您仍在使用它时不会取消分配。

你的代码在ARC之前没有真正正常工作,你只是内存泄漏。