Xib 与 Storyboard,如何更新
我正在新的 iOS 5.0 上研究 Storyboard。使用和实施似乎非常简单,但我的问题是...... 如何将旧的 Xib 更新为 Storyboard?
例如。我在没有故事板的情况下开发了一些类,其中一些类附带了 xib 文件,可以帮助我快速设置自定义布局。 显然,当我使用这种类时,我需要使用 initWithNibName:bundle: 实例化它,现在它已经可以使用了,我可以根据需要多次使用它,因为布局是在 xib 内编码的。
现在故事板...故事板不允许从 xib 加载视图控制器,并且我没有找到在主故事板中加载故事板文件的方法。似乎每次在新项目中使用特定视图控制器时,我都需要重新配置它的布局。 看来现在我被迫在每个使用此控制器的新应用程序中重新配置控制器的布局,而不是使用内部包含布局的 xib 文件。
也许有什么我不明白的地方。 任何人都可以帮助我了解使用故事板的最佳方式?
先感谢您。
加布里埃尔.
编辑回复 sw3n
也许我明白了,谢谢 sw3n。下面的代码可以工作,但这完全正确吗?
// All this code is implemented inside the MyViewController class.
// Attached to an UIButton;
- (void)loadNewController:(id)sender {
[self performSegueWithIdentifier:@"newControllerIdentifier" sender:sender];
}
- (void)performSegueWithIdentifier:(NSString *)identifier sender:(id)sender {
// As suggested by sw3n
// Load the storyboard from file.
UIStoryboard *storyboardTest = [UIStoryboard storyboardWithName:@"StoryBoardLoad_Test" bundle:nil];
// Instantiate the viewController that live in the storyboard file
UIViewController *destinationViewController = [storyboardTest instantiateViewControllerWithIdentifier:@"newControllerIdentifier"];
// Instantiate a segue to start the chain
UIStoryboardSegue *segue = [[UIStoryboardSegue alloc] initWithIdentifier:identifier source:self destination:destinationViewController];
[self prepareForSegue:segue sender:sender];
}
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([segue.identifier isEqualToString:@"newControllerIdentifier"]) {
[segue.destinationViewController setModalTransitionStyle:UIModalTransitionStyleFlipHorizontal];
// Are there new options to present the controller?
// If I was in a NavigationController a could obviously push it.
[self presentModalViewController:segue.destinationViewController animated:YES];
}
}
I was studying Storyboard on the new iOS 5.0. It seems really simple to use and to implement but my question is ...
How can I update the old Xib to Storyboard?
For instance. I have some classes I developed when there was no storyboard and some of this classes come with xib file that help me to setup a custom layout quickly.
Obviously when I use this kind of class I need to instantiate it using initWithNibName:bundle: and it now is ready to use and I can use it as many times I need because layout is coded inside xib.
Now Storyboard ... Storyboard doesn't allow to load view controller from xib and I did not found a way to load storyboard file inside the main storyboard. It seems like I need to reconfigure layout for a particular view controller every time I use it in a new project.
It seems that now I'm forced to reconfigure the layout of my controller in every new application that use this controller instead of using xib file that have the layout inside.
Maybe there is something I did not understand.
Anyone can help me to understand the best way to use storyboard?
Thank you in advance.
Gabriele.
EDIT in reply to sw3n
Maybe I understood thanks to sw3n. This code below works but is this completely correct?
// All this code is implemented inside the MyViewController class.
// Attached to an UIButton;
- (void)loadNewController:(id)sender {
[self performSegueWithIdentifier:@"newControllerIdentifier" sender:sender];
}
- (void)performSegueWithIdentifier:(NSString *)identifier sender:(id)sender {
// As suggested by sw3n
// Load the storyboard from file.
UIStoryboard *storyboardTest = [UIStoryboard storyboardWithName:@"StoryBoardLoad_Test" bundle:nil];
// Instantiate the viewController that live in the storyboard file
UIViewController *destinationViewController = [storyboardTest instantiateViewControllerWithIdentifier:@"newControllerIdentifier"];
// Instantiate a segue to start the chain
UIStoryboardSegue *segue = [[UIStoryboardSegue alloc] initWithIdentifier:identifier source:self destination:destinationViewController];
[self prepareForSegue:segue sender:sender];
}
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([segue.identifier isEqualToString:@"newControllerIdentifier"]) {
[segue.destinationViewController setModalTransitionStyle:UIModalTransitionStyleFlipHorizontal];
// Are there new options to present the controller?
// If I was in a NavigationController a could obviously push it.
[self presentModalViewController:segue.destinationViewController animated:YES];
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这是您要找的吗?
Is this what you're looking for?
听起来回答有点晚了,但似乎还没有结论。你可以使用上面提到的Nightfirecat的storyboardWithName()。从 UIStoryboard 创建新的 Viewcontroller,并将新的 ViewController 挂接到视图层次结构中所需的位置。您也可以使用多个故事板文件以这种方式分段您的 UX 设计。我在我的网站上写了一些关于如何将其挂接到根控制器的内容,但它基本上应该可以在任何地方工作。
Sound a bit late to answer, but seem to have no conclusion to the answer. You can use storyboardWithName() that Nightfirecat mentioned above. Create the new Viewcontroller from UIStoryboard, and hook the new ViewController to where you want in the view hierarchy. You can use multiple storyboard files to segment your UX design this way too. I wrote something about how to hook it as rootcontroller on my site, but it should work anywhere basically.
界面生成器:
在代码中:
Interface Builder:
In code: