【问题标题】:Update frame and content of nib programatically以编程方式更新 nib 的框架和内容
【发布时间】:2014-06-09 08:24:51
【问题描述】:

我有一个简单的笔尖,里面有几个UILabels。

在一种情况下,我想根据应用程序的状态将UILabel 添加到底部。是否可以动态/以编程方式将内容添加到 nib 以及如何实现?

【问题讨论】:

    标签: cocoa-touch ios7 xcode5 uilabel xib


    【解决方案1】:

    您应该从代码中执行此操作,即与 .nib 关联的 ViewController。

    在View Controller的类中,在头文件中添加几个UILabel实例变量,即

    @interface MyVC : UIViewController {
       UILabel* conditionalLabel;
    } ...
    

    那么,在.m实现文件中,

    @implementation MyVC : UIViewController {
        -(void)viewDidAppear:(BOOL)animated {
             if(myConditionToShowLabel) {
                if(!conditionalLabel) {
                   conditionalLabel = [[UILabel alloc] initWithFrame: CGRectMake(x, y, width, height)];
                   [self.view addSubView:conditionalLabel];
                } else {
                   [conditionalLabel removeFromSuperView];
                   [self.view addSubView:conditionalLabel];
                }
            }
         }
    

    记得在 XCode 的 NIB 编辑器中将视图控制器类设置为 MyVC :-)

    类似的东西会起作用,而且这是完全正常的做法 :-) 仅使用 NIB 来制作动态用户界面是很困难的——有时您需要“开始编码”。

    【讨论】:

      【解决方案2】:

      您可以通过编程方式在视图中添加 UILabel。最好的方法是 viewDidAppear 方法。

      例如,

      -(void)viewDidAppear
      
      {
      
      [super viewDidAppear];
      
      UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(CGRectGetMinX(self.view.frame),     CGRectGetMaxY(self.view.frame)-30, 200, 30)];
      
      [self.view addSubView:label];
      
      } 
      

      标签将出现在视图的底部。

      【讨论】:

        猜你喜欢
        • 2015-06-30
        • 1970-01-01
        • 2011-10-07
        • 2019-04-15
        • 1970-01-01
        • 1970-01-01
        • 2012-06-09
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多