将 UIViews 拖入故事板与以编程方式创建它们之间的区别? [英] Difference between dragging UIViews into storyboard and creating them programmatically?

查看:33
本文介绍了将 UIViews 拖入故事板与以编程方式创建它们之间的区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 iOS 编程的新手,想知道将 UIView 拖入故事板并以编程方式创建它们之间有什么区别.我正在使用 iPad 进行开发.我有一个 UISplitView.在 Appdelegate.m 中,我以编程方式创建了一个 UISplitViewController:

Hi I am new to iOS programming and am wondering what is the difference between dragging a UIView into the storyboard and create them programmatically. I am using an iPad for development. I have a UISplitView. Inside the Appdelegate.m, I created a UISplitViewController programmatically:

    MyTableViewController* orders = [[MyTableViewController alloc] initWithClassName:@"Order"];
    MyDetailsViewController* details = [[MyDetailsViewController alloc] init];
    UISplitViewController* splitViewController = [[UISplitViewController alloc] init];
    splitViewController.viewControllers = [NSArray arrayWithObjects: orders, details, nil];
    self.window.rootViewController = splitViewController;
    [self.window makeKeyAndVisible];

在情节提要中,我将根视图控制器的类设置为 MyTableViewController,将详细视图控制器的类设置为 MyDetailsViewController.但是,如果我将另一个 UILabel 拖入 MyDetailsViewController 的故事板并将其作为类的属性进行连接.它没有出现.我只能在 viewDidLoad() 中以编程方式创建它.

Inside the storyboard, I have the root view controller's class set to MyTableViewController and the detail view controller's class to MyDetailsViewController. However, if I drag another UILabel into the storyboard of MyDetailsViewController and wire it as a property of the class. It does not show up. I can only create it programmatically inside viewDidLoad().

- (void)viewDidLoad
{
    [super viewDidLoad];
    //self.testLabel was dragged into the storyboard and added as a property of the class
    [self.testLabel setText:@"TESTING!"]; 
    [self.view addSubview:self.testLabel];
    // creating programmatically
    UILabel *program = [[UILabel alloc] initWithFrame:CGRectMake(10, 10, 100, 100)];
    program.text = @"im a string";
    program.hidden = NO;
    [self.view addSubview:program];
    // The second one program shows up
}

为什么会出现这种情况?

Why is this the case here?

推荐答案

  • 每当你拖拽&在故事板中,它实际上创建了一个基于 XML 的文件,其中包含有关组件的每个细节.所以没有生成(目标-C)代码.您可以右键单击故事板 &将其作为源代码打开以查看 XML 文件.甚至可以在程序编译或运行之前查看故事板中的任何设计.

    • Whenever you do a drag & drop in the story board, it actually creates a XML based file that holds every detail about the component. So there is no (objective -C) code generated. You can right click the storyboard & open it as source code to see the XML file. Any design in story board can be viewed even before the program is compiled or running.

      当你写代码时,你的视图/视图控制器只能通过编译&来查看.然后在模拟器中运行构建.

      When you write the code, your view / view controller can be viewed only by compiling & then running the build in simulator.

      请右键单击故事板 &将其作为源代码打开,您可以看到它在 XML 标签中是如何表示的.

      Kindly right click storyboard & open it as source code, you can see how it is represented in XML tags.

      希望能帮到你.

      这篇关于将 UIViews 拖入故事板与以编程方式创建它们之间的区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆