【问题标题】:Xcode / iOS / iPhone How to set event to UILabelXcode / iOS / iPhone 如何将事件设置为 UILabel
【发布时间】:2014-09-05 18:34:32
【问题描述】:

请给我一个建议。
我以编程方式(动态)创建 UILabel。
有没有机会给他们添加事件?

我想要的步骤:

  1. 我创建了 UILabel;
  2. 我将事件设置为它; (NSNotification?)
  3. 当我执行某些操作(例如旋转)时,我希望更改或删除标签。一个扩展示例:我创建标签,当我旋转设备时,我希望它们的那一部分(带有附加事件)在动画中消失。

我创建了很多标签,所以我不能只设置它们全局变量。而且我不能将它们设置为无限制的标签。所以UILabel *label = (UILabel*)[self.view viewWithTag:labelCount 不是解决方案。通过“viewWithTag”获取元素还有一个麻烦 - 当为该元素设置动画并且该元素已经在动画中发生碰撞时 - 它们会播放一个...

我创建这样的标签:

CGRect *labelFrame = CGRectMake(left, top, width, height); UILabel *label = [[UILabel alloc] initWithFrame:labelFrame]; label.text = @"Hi, I'm one of these army of labels"; [self.view addSubview:label];

PS:对不起英文。

【问题讨论】:

    标签: ios uilabel


    【解决方案1】:

    我假设你有一个 UIViewController 有一堆标签。我建议使用 IBOutletConnection 来存储对所有标签的引用(假设情节提要)。

    //You will need to connect all of these labels through Interface Builder.
    @property (nonatomic, strong) IBOutletCollection(UILabel) NSArray *labels;
    

    在以下轮换方法之一 (Detect rotation changes in iOS) 中重新排列。

    //Called whenever orientation changes
    - (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation
    {
        for (UILabel *label in self.labels) {
            //Make each label disappear here.
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-12-19
      • 1970-01-01
      • 2011-11-14
      • 1970-01-01
      • 2012-06-01
      • 2018-01-20
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多