5

弹出到不支持当前方向的视图控制器时,未调用 UINavigationControllerDelegate 方法。

我有一个 UINavigationController 作为我的应用程序的根视图控制器(我的故事板中的初始视图控制器)。

假设我用这个来推送一个 ViewController A 来定位:

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

因此我们不支持任何横向模式。最重要的是,让我们用代码推送另一个 ViewController:

@interface B : UIViewController <UINavigationControllerDelegate>
@end

@implementation B
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    return YES;   // any orientation supported
}

- (void)viewDidLoad {
    [super viewDidLoad];

    AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
    UINavigationController *nc = (UINavigationController*)appDelegate.window.rootViewController;
    nc.delegate = self;
}

- (void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated {
    // not always called...
}

- (void)navigationController:(UINavigationController *)navigationController didShowViewController:(UIViewController *)viewController animated:(BOOL)animated {
    // not always called...
}
@end

现在,如果我的 iPhone 是纵向的,我通过一些触摸事件将视图控制器 B 推到 A 上,然后触摸导航栏中的“后退”按钮,一切都很好,并且调用了委托方法(我在那里做了一些事情)。

但是,如果当我查看视图 B 时,我旋转到横向,然后点击后退按钮,则不会调用这些委托方法!如果有时调用方法但不调用其他方法,那么为 UINavigationController 设置委托有什么意义?我肯定做错了什么。

我尝试将委托放在其他类中,例如 AppDelegate 或我的 MainView,没有任何变化。

有任何想法吗 ?

演示代码在这里: git://github.com/malaba/NavBarTest.git

从一个基本的主从模板,如上修改。

如何表明我的观点?这是一步一步:

  1. 在主视图中添加一些行(+ 右上角)
  2. 然后触摸一条线进入详细信息视图
  3. 请参阅显示的登录输出
  4. 点击返回按钮(标记为“Master”),再次显示日志。

现在尝试在详细信息视图中旋转 iPhone(或模拟器),然后“返回”并看到没有显示日志?!

4

2 回答 2

2

嗨,这对我来说也是一个棘手的问题,直到我看到这个想法: http ://www.hanspinckaers.com/custom-action-on-back-button-uinavigationcontroller

当 NavigationController 和 TopViewController 具有相同的方向时,IOS 使用以下代码创建以下调用序列(单点触控)

  • SomeTopViewController ViewWillDisappear
  • WillShowViewController viewController:新的 TopViewController
  • SomeTopViewController ViewDidDisappear
  • DidShowViewController viewController:新的 TopViewController

当 NavigationController 和 TopViewController 具有不同的方向时,则不会像您描述的那样调用 NavigationController 委托。因此调用顺序为:

  • SomeTopViewController ViewWillDisappear
  • SomeTopViewController ViewDidDisappear

以下代码通过覆盖 NavigationController 的 PopViewControllerAnimated 来重现 ios 调用序列:

public class MyNavigationControllerDelegate : UINavigationControllerDelegate {
  public MyNavigationControllerDelegate( ) : base() {}

  public bool WasCalled {get;set;}  // flag that we use to track whether iOS calls the handlers or we have to 

  public override void WillShowViewController( UINavigationController navigationController, UIViewController viewController, bool animated ) {
    Console.WriteLine( "WillShowViewController viewController: {0}", viewController.GetType() );
    WasCalled = true;  // signal that we have been called
    //.….. do your stuff
  }

  public override void DidShowViewController( UINavigationController navigationController, UIViewController viewController, bool animated ) {
    Console.WriteLine( "DidShowViewController viewController: {0}", viewController.GetType() );
    //.….. do your stuff
  }
}

public partial class MyNavigationController : UINavigationController {
  MyNavigationControllerDelegate navigationControllerDelegate;

  public override void ViewDidLoad() {
    base.ViewDidLoad();
    navigationControllerDelegate = new MyNavigationControllerDelegate( viewSelectionControl );
    Delegate = navigationControllerDelegate;
  }

public override UIViewController PopViewControllerAnimated( bool animated ) {
  Console.WriteLine( "PopViewControllerAnimated TopViewController.GetType: {0}", TopViewController.GetType() );
  navigationControllerDelegate.WasCalled = false;   // reset flag before we start the popsequence

  UIViewController ctrl = base.PopViewControllerAnimated( animated );

  AppDelegate.MainWindow.BeginInvokeOnMainThread( delegate {
    if( !navigationControllerDelegate.WasCalled )  {   // if iOS did not call the delegate handler then we must do it
      Delegate.WillShowViewController( this, TopViewController, animated );
      navigationControllerDelegate.WasCalled = false;  // reset flag to be used in the next DidShowViewController step of the popsequence
      }
  } );

  Thread t = new Thread( () => RunPop(animated) );
  tt.Start();

  return ctrl;
}

void RunPop(bool animated) {
  Thread.Sleep( 500 );
  AppDelegate.MainWindow.BeginInvokeOnMainThread( delegate {
    if( !navigationControllerDelegate.WasCalled ) {  // if iOS did not call the delegate handler then we must do it
      Delegate.DidShowViewController(this,TopViewController,animated);
    }
  } );
}

}

于 2012-04-25T11:58:12.433 回答
1

由 Kaspar 回答,这是我在 Obj-C 中的代码。

。H:

@interface ProperNavigationController : UINavigationController

@end

@interface ProperNavigationControllerDelegate : NSObject <UINavigationControllerDelegate>
@property (assign, nonatomic) BOOL wasCalled;
@end

米:

#import "ProperNavigationController.h"

@interface ProperNavigationController ()
@property (strong, nonatomic) id<UINavigationControllerDelegate> oldDelegate;
@property (strong, nonatomic) ProperNavigationControllerDelegate *myDelegate;
@end

@implementation ProperNavigationController
@synthesize oldDelegate = _oldDelegate;
@synthesize myDelegate = _myDelegate;

- (void)viewDidLoad {
    [super viewDidLoad];

    self.oldDelegate = self.delegate;
    self.myDelegate = [ProperNavigationControllerDelegate new];
    self.delegate = self.myDelegate;
}

- (UIViewController *)popViewControllerAnimated:(BOOL)animated {
    self.myDelegate.wasCalled = FALSE;

    UIViewController *vc = [super popViewControllerAnimated:animated];

    if (!self.myDelegate.wasCalled) {
        // if iOS did not call the delegate handler then we must do it
        [self.myDelegate navigationController:self willShowViewController:self.topViewController animated:animated];
        [self.myDelegate navigationController:self didShowViewController:self.topViewController animated:animated];
    }

    return vc;
}

@end

@implementation ProperNavigationControllerDelegate
@synthesize wasCalled = _wasCalled;     // flag that we use to track whether iOS calls the handlers or we have to 

- (id)init {
    if (self = [super init]) {
       _wasCalled = FALSE; 
    }
    return self;
}

- (void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated {
    ProperNavigationController *nc = (ProperNavigationController *)navigationController;
    [nc.oldDelegate navigationController:navigationController willShowViewController:viewController animated:animated];
    self.wasCalled = TRUE;  // signal that we have been called
}

- (void)navigationController:(UINavigationController *)navigationController didShowViewController:(UIViewController *)viewController animated:(BOOL)animated {
    ProperNavigationController *nc = (ProperNavigationController *)navigationController;
    [nc.oldDelegate navigationController:navigationController didShowViewController:viewController animated:animated];
}

@end

这行得通。

你怎么看 ?我们应该填写错误报告吗?

于 2012-05-03T09:44:06.113 回答