0

I am an absolute newbie at ios programming so forgive me:

I have a subclass of UIView called PuzzlePiece that I want to represent just a single puzzle piece (obviously). I don't want to use a nib file for it, but just want to be able to draw a piece using code and display it.

I have a view controller and associated nib file that will hold the entire puzzle. The view in the nib is of type PuzzleView which is another class that I have defined as a custom subclass of UIView. My PuzzleView works fine and I can draw on it and everything. I have a property of type PuzzlePiece called piece that I figured can refer to my puzzle piece.

So all I am trying to figure out how to do is to now initialize my view, add it as a subview, and draw it on the screen. Sounds simple, but I am at a complete loss of how to make this work. I would totally believe that I am not even approaching the problem correctly, so if that is the case, please advise me!

I thought I should be able to do something like

piece = [[puzzlePiece alloc] initWithFrame: ... ];
[self addSubview:piece];

But... the init bit doesn't work... and really no init methods seem to be available to me.

So... yeah. How do I just add a custom subclass of uiview as a subview to another custom subclass of uiview in code?

4

1 に答える 1

1

あなたはpuzzlePieceを活用する必要がありますか?

PuzzlePiece *piece = [[PuzzlePiece alloc] initWithFrame:CGRect(0, 0, 100, 100)];

PuzzlePieceがUIViewから派生している場合、コードは機能するように見えます。

于 2012-04-20T21:48:44.723 に答える