【问题标题】:failure of alloc and Initalloc 和 Init 失败
【发布时间】:2015-06-17 11:37:33
【问题描述】:

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

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 处设置断点 --> ViewDidLoad() --> [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

【问题讨论】:

  • 什么是 RscMgr 类?另外,哪个视图控制器是初始视图控制器,你如何从第一个到第二个?
  • RscMgr.h 是我正在使用的另一个第三方库。 DetectionView 是初始视图
  • 你如何从DetectionViewController转到EnterCommandViewController?你是在故事板中制作这些吗?你在使用 segue 吗?
  • 我在DetectionView底部的工具栏中有一个条形按钮项(名为“Settings”),它将segue(显示)到EnterCommandView
  • 您不应该使用 alloc init 来创建控制器的实例。 segue 创建实例。您应该实现 prepareForSegue,您可以在其中获取对目标视图控制器的引用,并将自己设置为那里的委托。

标签: ios objective-c delegates


【解决方案1】:
enterCVC = [[EnterCommandViewController alloc] init]

尝试将其更改为....

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-06-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多