开发者

Add a subview to a window, along with a specific key to toggle visibility?

I've figured out how to create a new UI element where I touch and add it using [self.view addSubview:[uilabel reference]]. However, if I tap in the same place, it'll simply add another subview on top of the current one.

I am able to create a unique key for each part of the grid I'm making (and thus placing subviews). Is there any way I can tag this subview with a key so I can toggle visibility on it?

Here's some of the code:

CGRect rectNote = CGRectMake(notePosX, notePosY, noteFrameWidth - 2, noteFrameHeight - 3);

UILabel *rectNoteLabel = [[UILabel alloc] initWithFrame:rectNote];

//label settings

[self.view addSubview:开发者_如何转开发rectNoteLabel];

Obviously I calculate the label's position, which could possibly be used as a key for the subview?


UIview has a "tag" property, so yes, you can tag a view and find it again later.

http://developer.apple.com/iphone/library/documentation/uikit/reference/UIView_Class/UIView/UIView.html#//apple_ref/doc/uid/TP40006816-CH3-SW25

Then you can call [self.view viewWithTag: blah] to get it back.

if you're just trying to figure out if a subview exists where you're clicking, then Jonathan Grynspan already mentioned -hitTest:

http://developer.apple.com/iphone/library/documentation/uikit/reference/UIView_Class/UIView/UIView.html#//apple_ref/doc/uid/TP40006816-CH3-BBCCAICB


You could test for the existence of a subview at that location by sending -hitTest: to the parent view.

It may be easier to keep a mutable array or set in your controlling class, and test against its contents before actually adding the new view. (The expense of iterating through the array is going to be the same as the expense of iterating through the parent view's subviews, and in either case it's probably not going to be significant.)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜