alloc和Init失败

时间:2015-04-11 22:09:44

标签: ios objective-c delegates

所以也许这是一个初学者的错误,对你们来说非常容易,但我真的不知道如何解决它,真的很感激任何建议: 现在:

1:我必须使用ViewController:EnterCommandViewController和DetectionViewController

2:我在EnterCommandViewController中编写了Delegate协议,并将DetectionViewController设置为其委托。

3:关于委托:我在EnterCommandView中有一个inputTextField,在这个视图的顶部工具栏上有一个“Save”栏按钮项。单击保存后,当前视图将被关闭并返回到DetectionView并显示刚刚在DetectionView中的UILabel中输入的NSString。

最后,我的问题是为什么在我分配并初始化一个EnterCommandViewController实例(即enterCVS)之后,实例仍为零,如我帖子末尾所示。

代码:

EnterCommandViewController.h

#import <UIKit/UIKit.h>
#import "RscMgr.h"

@protocol EnterCommandDelegate <NSObject>
@optional
-(void) commandEntered:(NSString*)command;

@end
@interface EnterCommandViewController : UIViewController    <RscMgrDelegate,EnterCommandDelegate>
{
    RscMgr* rscMgr;
    IBOutlet UITextField *inputTextField;
//    DetectionViewController* detectionViewController;
//      __unsafe_unretained id<EnterCommandDelegate> delegate;
}

-(void)sendMessage:(NSString*)message;

-(id)initWithDelegate:(id)delegateToBe;
- (IBAction)cancelPressed;

- (IBAction)savePressed;


@property (nonatomic,weak) id<EnterCommandDelegate> delegate;  //assign  replaced

@end

EnterCommandVIewController.m

#import "EnterCommandViewController.h"
#import "DetectionViewController.h"

@interface EnterCommandViewController () <UITextFieldDelegate>
{
    @private
    BOOL connected;
}

@end

@implementation EnterCommandViewController

@synthesize delegate;

- (void)viewDidLoad {

[super viewDidLoad];
rscMgr = [[RscMgr alloc] init];
[rscMgr setDelegate:self];
// Do any additional setup after loading the view, typically from a nib.
[inputTextField becomeFirstResponder];
}
-(id)initWithDelegate:(id)delegateToBe{
if(self = [super init]){
    delegate = delegateToBe;
}
return self;
}

-(void) viewWillAppear:(BOOL)animated{
[super viewWillAppear:animated];
inputTextField.delegate = self;
}

-(void) viewWillDisappear:(BOOL)animated{
[super viewWillDisappear:animated];
inputTextField.delegate = nil;
}

- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}


#pragma mark - UITextFieldDelegate Methods
-(BOOL) textFieldShouldReturn:(UITextField *)textField{
[self sendMessage:textField.text];
textField.text = nil;
return NO;
}


#pragma mark - Serial Tx/Rx Methods Implementation
-(void) sendMessage:(NSString *)message{
if(connected == YES) {
    [rscMgr writeString:message];
}
else{
    NSLog(@"CableDisconnected!");
    NSLog(@"Attempted To Send: %@",message);
}
}

- (IBAction)cancelPressed {
[self dismissViewControllerAnimated:YES completion:^{}];
}

- (IBAction)savePressed {
//is anyone listening
if([[[UIDevice currentDevice]systemVersion] compare:@"7.0"    options:NSNumericSearch] != NSOrderedAscending){
    NSLog(@"SYStem version > 7.0");
    }
if(delegate&&[delegate respondsToSelector:@selector(commandEntered:)]){
    NSLog(@"SomeMethod is listening"); 
    [delegate commandEntered:inputTextField.text];
}
[self dismissViewControllerAnimated:YES completion:nil]; //commened: ^{}
}

#pragma mark - RscMgrDelegate Methods Implementation
-(void) cableConnected:(NSString *)protocol{
inputTextField.text = @"cableConnected";
[rscMgr setBaud:9600];
[rscMgr open];
connected = YES;
}
-(void) cableDisconnected{
inputTextField.text = @"cableDisconnected";
connected = NO;
}
-(void) readBytesAvailable:(UInt32)length{}
-(void) portStatusChanged{}

@end

DetectionViewController.h

#import <UIKit/UIKit.h>
#import "EnterCommandViewController.h"
@interface DetectionViewController : UIViewController <EnterCommandDelegate>{
}
- (IBAction)showSettings:(UIBarButtonItem *)sender;

@property (nonatomic, strong) EnterCommandViewController* enterCVC;
@property (nonatomic, strong) IBOutlet UILabel *showReceivedCommand;
@end

DetectionViewController.m

#import <Foundation/Foundation.h>
#import "DetectionViewController.h"
#import "EnterCommandViewController.h"

@implementation DetectionViewController
@synthesize showReceivedCommand;
@synthesize enterCVC;

- (IBAction)showSettings:(UIBarButtonItem *)sender {
}

-(void) viewDidLoad{
[super viewDidLoad];
if(showReceivedCommand){
showReceivedCommand.text=@"Initial text";
    NSLog(@"UILAbel in ViewDidload is not nil");
}else {
    NSLog(@"UILAbel in viewDidload is nil");
}
enterCVC = [[EnterCommandViewController alloc] init];
if(enterCVC.delegate) NSLog(@"X nil");
[enterCVC setDelegate:self];

}

#pragma mark - EnterCommandDelegate function(s)

-(void)commandEntered:(NSString *)command{

dispatch_async(dispatch_get_main_queue(), ^{
    if(showReceivedCommand){
        NSLog(@"UILabel is not nil");
    }else{NSLog(@"UILabel is nil");}


    showReceivedCommand = [[UILabel alloc] init];
    NSLog(@"command received: %@",command);
    showReceivedCommand.text = command;
   [showReceivedCommand setNeedsDisplay];
            NSLog(@"text in showReceivedCommand is %@",showReceivedCommand.text);
    });
 }
@end

我在DetectionViewController.n设置了一个断点 - &gt; ViewDidLoad() - &gt; [enterCVC setDelegate:self];

我得到了:

self    DetectionViewController *   0x15c50e850 0x000000015c50e850
UIViewController    UIViewController        
showReceivedCommand UILabel *   0x15c510650 0x000000015c510650
enterCVC    EnterCommandViewController *    0x15c611360 0x000000015c611360
showReceivedCommand UILabel *   0x15c510650 0x000000015c510650
enterCVC    EnterCommandViewController *    0x15c611360 0x000000015c611360
UIViewController    UIViewController        
rscMgr  RscMgr *    nil 0x0000000000000000
inputTextField  UITextField *   nil 0x0000000000000000
connected   BOOL    NO  false
delegate    id  0x0 0x0000000000000000

1 个答案:

答案 0 :(得分:0)

enterCVC = [[EnterCommandViewController alloc] init]

尝试将其更改为....

enterCVC = [[EnterCommandViewController alloc] initWithDelegate:self];