如何通过 xcode 4.2 故事板正确使用模态视图控制器
我想知道如何正确使用故事板以模态方式放置视图控制器。就我个人而言,我更喜欢与 xibs 合作,但故事板似乎越来越受欢迎,并且将成为未来的发展方向。
我通常以模态方式放置视图控制器的方式是这样的:假设我们有 ViewControllerA (简称 A)和 ViewControllerB (简称 B)。 然后,我通常会在 Bh 中放置一个协议,指定当 B 想要被解雇时的委托方法,并添加 idassign
属性。假设我正忙于 A 并且我想以模态方式呈现 B,我会这样写:
B* b = [[B alloc] initWithNibName:@"B" bundle:nil];
b.delegate = self;
[self presentModalViewController:B animated:YES];
使用情节提要,我知道可以通过 ctrl 键从按钮拖动到视图控制器并选择以模态方式放置不同的视图控制器模态作为过渡类型。我只是想知道;在哪里设置新视图控制器的委托?将事物传递给模态视图控制器的正确做法是什么?我真的不知道 Segues 的整个交易是什么......
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.
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).
I would then normally put a protocol in B.h specifying the delegate method when B wants to be dismissed and add the id<theProtocol> delegate
field as an assign
property. Assuming i'm busy in A and I want to present B modally, I would write:
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. 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...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
看看这个tutorial
根据它,您应该将委托设置为如下:
其中@“AddPlayer”是您的“模式”segue 的名称
Take a look at this tutorial
According to it, you should set the delegate as follows:
Where @"AddPlayer" is the name of your 'modal' segue
您可以直接使用在
prepareForSegue
中传递的UIStoryboardSegue
对象,而不是使用导航控制器。它有一个名为destinationViewController
的属性,它是正在实例化的视图控制器。我发现这样干净多了。这是一个例子。
IMO 我认为故事板很棒,因为它们的功能就像应用程序的蓝图。而且我从来不喜欢笔尖。 =D
Instead of using the navigation controller you could directly use the
UIStoryboardSegue
object passed inprepareForSegue
. It has a property calleddestinationViewController
which is the view controller that is being instantiated. I find that a lot cleaner.This is an example.
IMO I think that storyboards are great because they function like a blueprint of your application. Also I've never liked nibs. =D