如何通过 xcode 4.2 故事板正确使用模态视图控制器

发布于 2024-12-18 07:03:21 字数 593 浏览 1 评论 0原文

我想知道如何正确使用故事板以模态方式放置视图控制器。就我个人而言,我更喜欢与 xibs 合作,但故事板似乎越来越受欢迎,并且将成为未来的发展方向。

我通常以模态方式放置视图控制器的方式是这样的:假设我们有 ViewControllerA (简称 A)和 ViewControllerB (简称 B)。 然后,我通常会在 Bh 中放置一个协议,指定当 B 想要被解雇时的委托方法,并添加 id。 delegate 字段作为 assign 属性。假设我正忙于 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

花落人断肠 2024-12-25 07:03:21

看看这个tutorial

根据它,您应该将委托设置为如下:

- (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;
    }
}

其中@“AddPlayer”是您的“模式”segue 的名称

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

不气馁 2024-12-25 07:03:21

您可以直接使用在 prepareForSegue 中传递的 UIStoryboardSegue 对象,而不是使用导航控制器。它有一个名为 destinationViewController 的属性,它是正在实例化的视图控制器。我发现这样干净多了。
这是一个例子。

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    if ([segue.identifier isEqualToString:@"AddPlayer"])
    {
        PlayerDetailsViewController 
          *playerDetailsViewController = 
            (PlayerDetailsViewController *) segue.destinationViewController;

        playerDetailsViewController.delegate = self;
    }
}

IMO 我认为故事板很棒,因为它们的功能就像应用程序的蓝图。而且我从来不喜欢笔尖。 =D

Instead of using the navigation controller you could directly use the UIStoryboardSegue object passed in prepareForSegue. It has a property called destinationViewController which is the view controller that is being instantiated. 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. Also I've never liked nibs. =D

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文