0

我有一个以前从未遇到过的奇怪问题,我的 viewController 中有我想在 UIView 中显示的数据。

这是一个涉及 SplitView 控制器的 iPad 应用程序,当我单击表视图(masterView)中的一个元素时,它会在我的 detailViewController 中执行一个功能(通过协议)。

执行一个启动 UIView 并向其发送数据的函数:

我的控制器:

- (void)SelectionChanged:(DocumentInfo*)document_info withDocu:(Document *)document{ 

    DocumentView *viewDoc=[[DocumentView alloc]initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height)];
    viewDoc.doc=document;
    viewDoc.doc_info=document_info;
    [viewDoc setBackgroundColor:[UIColor whiteColor]];

    [self.view addSubview:viewDoc]; 
}

文档视图.h

#import <UIKit/UIKit.h>
#import "Document.h"
#import "DocumentInfo.h"

@class Document;
@class DocumentInfo;

@interface DocumentView : UIView

@property(strong,nonatomic) Document *doc;
@property(strong,nonatomic) DocumentInfo *doc_info;

@end

文档视图.m

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {        
        UILabel *titreDoc=[[UILabel alloc] initWithFrame:CGRectMake(20, 32, 339, 21)];
        titreDoc.textColor = [self makeColorRGB_RED:66 GREEN:101 BLUE:149];
        titreDoc.font = [UIFont fontWithName:@"System" size:(24.0)];
        [self addSubview:titreDoc];
        NSLog(@"%@ - %@",doc,doc_info);
        titreDoc.text=@"Nouveau Document";
    }
    return self;
}

我的视图显示得很好(我的意思是标签出现了),但无法获取本来会传递给它的数据......(NSLog print (null) (null))

有人知道原因吗?

4

2 回答 2

0

NSLog 打印 null 的原因是调用 initWithFrame 方法时 doc 和 doc_info 为 nil。在 selectionChanged: 方法中调用 initWithFrame 方法后设置 doc 和 doc_info 属性。在 selectionChanged 方法的第 3 行之后添加 NSLog 函数,如下所示:

- (void)SelectionChanged:(DocumentInfo*)document_info withDocu:(Document *)document{ 

  DocumentView *viewDoc=[[DocumentView alloc]initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height)];
  viewDoc.doc=document;
  viewDoc.doc_info=document_info;
  NSLog(@"%@ - %@",doc,doc_info);
 [viewDoc setBackgroundColor:[UIColor whiteColor]];

[self.view addSubview:viewDoc]; 

}

于 2012-06-12T10:11:33.940 回答
0

这个问题似乎很简单。您初始化您的视图(这意味着您运行- (id)initWithFrame:(CGRect)frame),然后您正在设置数据,因此您在方法中看到null值是正常的,init因为尚未设置 ivars。您可以做的是修改您的 init 方法,以便在考虑这些 ivars 的情况下构建您的视图。可能是这样的:

- (id)initWithFrame:(CGRect)frame doc:(Document *)doc docInfo:(DocumentInfo *)docInfo;

附言。如果您选择自定义init方法,请不要忘记-initWithFrame:在任何自定义之前调用指定的初始化程序 ( )。

于 2012-06-12T10:10:18.057 回答