[英]How to properly use modal view controller with the xcode 4.2 storyboard
I was wondering how to properly use the storyboard to put up a view controller modally. 我想知道如何正确使用故事板以模态方式设置视图控制器。 Personally I prefer working with xibs, but it seems that the storyboard is gaining popularity and will be the way to go in the future.
就个人而言,我更喜欢使用xibs,但似乎故事板越来越受欢迎,并且将成为未来的发展方向。
The way I would normally put up a view controller modally would be like this: let's say we have ViewControllerA (A for short) and ViewControllerB (B for short). 我通常以模态方式设置视图控制器的方式如下:假设我们有ViewControllerA(简称A)和ViewControllerB(简称B)。 I would then normally put a protocol in Bh specifying the delegate method when B wants to be dismissed and add the
id<theProtocol> delegate
field as an assign
property. 然后,我通常会在Bh中指定委托方法,当B想要被解雇时添加一个协议,并将
id<theProtocol> delegate
字段添加为assign
属性。 Assuming i'm busy in A and I want to present B modally, I would write: 假设我在A忙,我想以模态方式呈现B,我会写:
B* b = [[B alloc] initWithNibName:@"B" bundle:nil];
b.delegate = self;
[self presentModalViewController:B animated:YES];
Using the storyboard, I know it's possible to put up a different view controller in a modal way by ctrl-dragging from a button to a viewcontroller and selecting modal as transition type. 使用故事板,我知道可以通过按住Ctrl键从按钮拖动到视图控制器并选择模态作为转换类型,以模态方式设置不同的视图控制器。 I'm just wondering though;
我只是想知道; where do I set the delegate of the new view controller?
我在哪里设置新视图控制器的委托? What's the correct practice of passing things to your modal view controller?
将事物传递给模态视图控制器的正确做法是什么? I don't really know what the whole deal with Segues is...
我真的不知道Segues的整个交易是什么......
Take a look at this tutorial 看一下本 教程
According to it, you should set the delegate as follows: 根据它,您应该如下设置委托:
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([segue.identifier isEqualToString:@"AddPlayer"])
{
UINavigationController *navigationController =
segue.destinationViewController;
PlayerDetailsViewController
*playerDetailsViewController =
[[navigationController viewControllers]
objectAtIndex:0];
playerDetailsViewController.delegate = self;
}
}
Where @"AddPlayer" is the name of your 'modal' segue 其中@“AddPlayer”是“模态”segue的名称
Instead of using the navigation controller you could directly use the UIStoryboardSegue
object passed in prepareForSegue
. 您可以直接使用
prepareForSegue
传递的UIStoryboardSegue
对象,而不是使用导航控制器。 It has a property called destinationViewController
which is the view controller that is being instantiated. 它有一个名为
destinationViewController
的属性,它是正在实例化的视图控制器。 I find that a lot cleaner. 我觉得很清洁。 This is an example.
这是一个例子。
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([segue.identifier isEqualToString:@"AddPlayer"])
{
PlayerDetailsViewController
*playerDetailsViewController =
(PlayerDetailsViewController *) segue.destinationViewController;
playerDetailsViewController.delegate = self;
}
}
IMO I think that storyboards are great because they function like a blueprint of your application. IMO我认为故事板非常棒,因为它们的功能就像应用程序的蓝图。 Also I've never liked nibs.
我也从不喜欢笔尖。 =D
= d
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.