0

私はこれらの2つのコードを持っています。最初のものは完全に機能します:

UIView *tmp = [[UIView alloc] initWithFrame:CGRectMake(0.0f, 60.0f, 296.0f, 44.0f)];
[self.dynamicView addSubview:tmp];
[tmp release];

2つ目はほとんど同じですが、ビューが表示されません。

CommentBox *commentBox = [[CommentBox alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 296.0f, 44.0f)]; 
[self.dynamicView addSubview:commentBox];
[commentBox release];   // Why does this remove the view?

ビューを削除すると[commentBox release]、驚くほどビューが表示されます。しかし、これら2つのコードスニペットに違いは見られません。

CommentBoxの初期化は次のようになります。

- (id)initWithFrame:(CGRect)frame {
    if ((self = [super initWithFrame:frame])) {
        // Load the nib:
        NSArray *nibObjects = [[NSBundle mainBundle] loadNibNamed:@"CommentBox" owner:self options:nil];
        self = [nibObjects objectAtIndex:0];

    }
    return self;
}
4

2 に答える 2

3

After thinking about Graham's answer I came up with the following solution:

  1. I drag a new UIView (-> lets call it sub UIView) in Interface Builder in my main UIView
  2. I give this sub UIView the correct size (because I cannot resize the main UIView, which is always 320x460)
  3. I drag all my other elements in this sub UIView (so that all elements are attached to my sub UIView)
  4. I give my sub UIView a tag number (Interface Builder -> View Attributes), e.g. "300"
  5. In the code I do now the following within my -initWithFrame:

    NSArray *nibObjects = [[NSBundle mainBundle] loadNibNamed:@"CommentBox" owner:self options:nil]; UIView *subView = [[nibObjects objectAtIndex:0] viewWithTag:300]; [self addSubview:subView];

Hope that helps.


Update:

I just had another idea of doing it. Instead of the tag numbers you can also create a IBOutlet UIView *viewHolder in the CommentBox class and set the outlet in IB. Then in the initWithFrame: I do the following:

[[NSBundle mainBundle] loadNibNamed:@"CommentBox" owner:self options:nil];
[self addSubview:self.viewHolder];
于 2010-05-19T10:12:20.033 に答える
2
于 2010-05-19T08:58:19.437 に答える