菜鸟教程小白 发表于 2022-12-12 21:46:56

ios - 支持自定义和默认展开 segues


                                            <p><p>我已经使用 Storyboard实现了一个很好的界面,我决定我想要一个更漂亮的动画来呈现另一个 ViewController 。为了保持一致,这个动画需要一个 segue 和一个 unwind segue。所以我创建了我的 segue 来支持两者:</p>

<pre><code>@interface FKCircularSegue : UIStoryboardSegue

/// If it&#39;s an unwind segue
@property (nonatomic,assign) BOOL unwind;

@property (nonatomic,assign) CGRect frameCircle;

@end
</code></pre>

<p>在进行转场和展开时效果很好。为了放松,我实现了这个方法:</p>

<pre><code>- (UIStoryboardSegue *)segueForUnwindingToViewController:(UIViewController *)toViewController fromViewController:(UIViewController *)fromViewController identifier:(NSString *)identifier
{
    if ()
    {
      // Circular segue!
      FKCircularSegue *segue = [ initWithIdentifier:identifier source:fromViewController destination:toViewController];

      segue.frameCircle= _frameCameraButtonTapped;
      segue.unwind      = YES;

      return segue;
    }

    return ;
}
</code></pre>

<p>但是!我注意到所有其他正常的展开segues,从推送和模式(这是一种中央 ViewController )回来都停止工作,很明显,<code></code>没有这样做。</p>

<p>奇怪的是,调试器显示它实际上返回了一个 <code>UIStoryboardSegue</code>,但它没有做任何事情,点击一个通常会“弹出”你回到这个 ViewController 的按钮什么也没做。 </p>

<p>另一方面,如果我注释掉整个 <code>segueForUnwind...</code> 方法,它就会恢复正常,所以 <code>UIViewController</code> 的默认实现是正确的一个。</p>

<p>简而言之,我的问题是,我怎样才能支持一个自定义展开 segue,然后为其他所有展开 segue 使用默认选项?</p></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>嗯,我想通了。</p>

<p>结果表明 <code>super</code> 方法没问题,问题出在我创建的 <code>UINavigationController</code> 子类上,该子类将调用转发到此 ViewController 。</p>

<p>为了解决这个问题,我添加了一个任何 ViewController 都可以实现的协议(protocol),并且基本上询问它是否要处理展开转场,或者它应该由导航 Controller 自动完成。</p>

<p>非常冗长,但我认为它比编辑之前发布的解决方案要好。</p>

<h3>FKNavigationController.h</h3>

<pre><code>/**
*Implement this if you want custom unwind segues
*/
@protocol FKNavigationControllerSegueForwarding &lt;NSObject&gt;

- (BOOL)shouldPerformUnwindSegueFromNavigationControllerWithIdentifier:(NSString*)identifier fromViewController:(UIViewController*)viewController;

@end

/**
*Automatically forwards segue methods
*/
@interface FKNavigationController : UINavigationController

@end
</code></pre>

<h3>FKNavigationController.m</h3>

<pre><code>@implementation FKNavigationController

- (UIStoryboardSegue *)segueForUnwindingToViewController:(UIViewController *)toViewController fromViewController:(UIViewController *)fromViewController identifier:(NSString *)identifier
{
    if ()
    {
      if ([(id&lt;FKNavigationControllerSegueForwarding&gt;)toViewController shouldPerformUnwindSegueFromNavigationControllerWithIdentifier:identifier fromViewController:fromViewController])
      {
            return ;
      }
    }

    return ;
}

@end
</code></pre>

<h3>更新的 ViewController </h3>

<pre><code>#pragma mark - Segue forwarding

- (BOOL)shouldPerformUnwindSegueFromNavigationControllerWithIdentifier:(NSString *)identifier fromViewController:(UIViewController *)viewController
{
    if ()
    {
      return YES;
    }

    return NO;
}

- (UIStoryboardSegue *)segueForUnwindingToViewController:(UIViewController *)toViewController fromViewController:(UIViewController *)fromViewController identifier:(NSString *)identifier
{
    // Check the FKNavigationControllerSegueForwarding if you want to add more.
    if ()
    {
      // Circular segue!
      FKCircularSegue *segue = [ initWithIdentifier:identifier source:fromViewController destination:toViewController];

      segue.frameCircle= _frameCameraButtonTapped;
      segue.unwind      = YES;

      return segue;
    }

    return ;
}
</code></pre></p>
                                   
                                                <p style="font-size: 20px;">关于ios - 支持自定义和默认展开 segues,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/23204006/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/23204006/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: ios - 支持自定义和默认展开 segues