您好,我是 iOS 编程的新手,想知道将 UIView
拖到 Storyboard 中并以编程方式创建它们之间有什么区别。
我正在使用 iPad 进行开发。我有一个 UISplitView
。在 Appdelegate.m
中,我以编程方式创建了一个 UISplitViewController
:
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];
在 Storyboard中,我将 Root View Controller 的类设置为 MyTableViewController
,将详细 View Controller 的类设置为 MyDetailsViewController
。但是,如果我将另一个 UILabel
拖到 MyDetailsViewController
的 Storyboard 中并将其连接为类的属性。它没有出现。我只能在 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
}
为什么会这样?
最佳答案
每当您在 Storyboard中进行拖放时,它实际上会创建一个基于 XML 的文件,其中包含有关组件的所有详细信息。所以没有生成( objective-C )代码。您可以右键单击 Storyboard并将其作为源代码打开以查看 XML 文件。即使在程序编译或运行之前,也可以查看 Storyboard中的任何设计。
当您编写代码时,您的 View / View Controller 只能通过编译然后在模拟器中运行构建来查看。
请右键单击 Storyboard并将其作为源代码打开,您可以看到它是如何在 XML 标记中表示的。
希望对您有所帮助。
关于ios - 将 UIView 拖入 Storyboard 和以编程方式创建 UIView 之间的区别?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21421811/