如何使用故事板设置委托 [英] How to set the delegate with a storyboard

查看:102
本文介绍了如何使用故事板设置委托的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经讨论了一段时间了,希望你能帮助我。

I've been debating with this for a while now, hope you can help me.

我一直在使用故事板创建应用程序,我有一个点我弹出一个模态框来添加一个新记录,popup工作正常,问题是解雇它。

I've been creating an app using storyboards mostly, I have a point where I popup a modal box to add a new record, popup works fine, the problem is dismissing it.

我已经按照Apple的说明如何正确关闭模态使用委托的框,并且工作正常,除了我需要在我的模态框中添加导航控制器,因为添加过程需要两个步骤(这里全屏):

I've followed Apple's instructions on how to properly close modal boxes using delegates, and that works fine, except I need to add a navigation controller to my modal box, because the add process requires two steps (here fullscreen):

问题在于设置委托,所以这是我的两个问题:

The problem lies in setting the delegate, so here are my two questions:

1-在我的根视图中,类(我的选项卡)是Add类(模态)的委托,除此之外一切都设置正确:

1- In my root view class (My Tab) is a delegate of the Add class (the modal), everything is set up right except this:

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    if ([[segue identifier] isEqualToString:@"showAdd"]) {
        [[segue destinationViewController] setDelegate:self];

    }
}

问题出在于[segue] destinationViewController]返回navigationcontroller而不是AddDrinkViewController类(参见故事板)。我该如何解决这个问题?如果我完全删除导航控制器,代码可以正常设置适当的委托。

The problem lies in that [segue destinationViewController] is returning the navigationcontroller and not the AddDrinkViewController class (see the storyboard). How do I get around this? If I remove the navigation controller altogether, the code works fine setting the appropriate delegate.

2-有没有办法通过拖动故事板中的插座来设置委托?

2- Is there any way to set the delegate by dragging the outlets in the storyboard?

谢谢!

推荐答案

你是对的,<$在这种情况下,c $ c> destinationViewController 将是 UINavigationController 。我写了一个类别来处理这种常见情况:

You're right, the destinationViewController will be a UINavigationController in this case. I wrote a category to handle this common situation:

// category .h file
@interface UIStoryboardSegue (NavControllerExtensions)
// Gets destinationViewCotroller. But if that controller 
// is a NavigationController, returns the nav controller's 
// top level view controller instead.
@property (readonly) id topLevelDestinationViewController;
@end

// category .m file
@implementation UIStoryboardSegue (NavControllerExtensions)
- (id)topLevelDestinationViewController
{
  id dest = self.destinationViewController;
  if ([dest isKindOfClass:[UINavigationController class]]) {
    UINavigationController* nav = dest;
    dest = nav.topViewController;
  }
  return dest;
}
@end

所以现在你可以在任何一个你的 prepareForSegue 方法,不用担心是否存在 NavigationController

So now you can just do this in any of your prepareForSegue methods, and not need to worry about whether there even exists a NavigationController:

[[segue topLevelDestinationViewController] setDelegate:self]
// another example:
MyViewController *vc = segue.topLevelDestinationViewController;
vc.delegate = self; // etc.

要回答你的第二个问题,我找不到设置代表的方法在IB内。

To answer your second question, I couldn't find a way to set the delegate within IB.

这篇关于如何使用故事板设置委托的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆