简体   繁体   中英

How to reference a UILabel that was added through code so I can update position when iPad is rotated?

I added a UILabel subview using code. When the user rotates the iPad, I want to be able to tell that UILabel to shift its position to accomodate the new screen orientation.

I think I need to put my code in the willAnimateRotationToInterfaceOrientation function. I want to be able to get at my UILabel and I see sample code that looks like the following to retrieve a reference to a subview: UILabel *myLabel = (UILabel *) [self.subviews objectAtIndex:0];

How do I know what index my UILabel is at? The sample code seems to know that their subview is the first one (index 0).

Is there a way for me to find out what my UILabel's index is?

Is there another way to keep a reference to this UILabel that I create in code? If I made myLabel a variable of the class, will I be able to reference

Generally, in your UIViewController, you'd have an instance variable that was a pointer to your UILabel. Then you can access it anywhere in your controller's code.

可以将其放在实例变量中(我喜欢这样,因为它允许您为视图赋予漂亮的名称),或者设置其tag属性,以便您可以通过[self.view viewWithTag:myTag]再次找到它(标签只是您为视图分配的编号,以供以后查找之用。

还有另一种可能性不是这样做,而是设置一个autoresizingMask ,使其(例如)粘在左上角。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM