ios - 我的 UINavigationControllerDelegate 在应用加载主视图后被取消了

标签 ios objective-c

我想为 UINavigation Controller 实现自定义动画。

我在 Appdelegate 中添加了一些属性:

@interface MyAppDelegate : UIResponder <UIApplicationDelegate> {
...
// for custrom navigation transition animation
@property (strong, nonatomic) UINavigationController *AppNavigationController;
...
@end

在 didFinishLaunchingWithOptions 中:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
...
self.AppNavigationController = (UINavigationController *)((REFrostedViewController *)self.window.rootViewController).contentViewController;
        MyNavigationControllerDelegate * naviDelegate = [[MyNavigationControllerDelegate alloc] init];
self.AppNavigationController.delegate = naviDelegate;
    }
...
}

调试时,到目前为止我可以看到委托(delegate):

(lldb) po self.AppNavigationController
<UINavigationController: 0x14f52bb90>

(lldb) po self.AppNavigationController.delegate
<MyNavigationControllerDelegate: 0x17424c5a0>

当我的主视图 Controller 加载时,我仍然可以看到

(lldb) po 0x14f52bb90
<UINavigationController: 0x14f52bb90>

但是当尝试访问 0x14f52bb90 它的委托(delegate)时,

(lldb) po [0x14f52bb90 delegate]
2015-02-28 18:54:14.724 MyApp[1995:489717] *** -[MyNavigationControllerDelegate respondsToSelector:]: message sent to deallocated instance 0x17424c5a0
0x000000017424c5a0

我不确定委托(delegate)被释放的原因,以及如何修复它。有什么想法吗?

MyNavigationControllerDelegate 初始化代码:

-(id)init {
    self = [super init];
    if (self) {
        self.pushAnimator = [[PushAnimator alloc] init];
        self.popAnimator = [[PopAnimator alloc] init];
        UIApplication * app = [UIApplication sharedApplication];
        MyAppDelegate *appDelegate = (MyAppDelegate *)app.delegate;
        self.navigationController = appDelegate.AppNavigationController;
        UIPanGestureRecognizer *panRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(pan:)];
        [self.navigationController.view addGestureRecognizer:panRecognizer];
    }
    return self;
}

以及进一步的崩溃跟踪:

(lldb) bt
* thread #1: tid = 0x778f5, 0x0000000184a72440 CoreFoundation`___forwarding___ + 968, queue = 'com.apple.main-thread', stop reason = EXC_BREAKPOINT (code=1, subcode=0x184a72440)
  * frame #0: 0x0000000184a72440 CoreFoundation`___forwarding___ + 968
    frame #1: 0x0000000184976b6c CoreFoundation`_CF_forwarding_prep_0 + 92
    frame #2: 0x00000001892d1d4c UIKit`-[UINavigationController _startTransition:fromViewController:toViewController:] + 908
    frame #3: 0x00000001892d16dc UIKit`-[UINavigationController _startDeferredTransitionIfNeeded:] + 640
    frame #4: 0x00000001892d13fc UIKit`-[UINavigationController __viewWillLayoutSubviews] + 56
    frame #5: 0x00000001892d137c UIKit`-[UILayoutContainerView layoutSubviews] + 200

最佳答案

我的猜测是,您的 AppNavigationController 上的 delegate 属性定义为 @property(assign) (或者没有任何修饰符,默认为 assign),因此当 -application:didFinishLoadingWithOptions 完成时,delegate 被释放,因为它没有在任何地方保留。

如果是这种情况,您可以使用 @property(strong) 修复它。

编辑

UINavigationController属性delegate被定义为assign,所以你需要在其他地方保留delegate,我建议这样做:

我的应用程序委托(delegate)

@interface MyAppDelegate : UIResponder <UIApplicationDelegate> {
...
@property(strong) MyNavigationControllerDelegate* naviDelegate;   
...
@end

didFinishlaunchingWithOptions

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
...

self.AppNavigationController = (UINavigationController *)((REFrostedViewController *)self.window.rootViewController).contentViewController;
self.naviDelegate = [[MyNavigationControllerDelegate alloc] init];
self.AppNavigationController.delegate = self.naviDelegate;

...
}

关于ios - 我的 UINavigationControllerDelegate 在应用加载主视图后被取消了,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28780995/

相关文章:

带有自定义单元格的IOS静态表只绘制一个随机单元格

iphone - UIDocumentInteractionController 自 iOS6 起不起作用

objective-c - 在我滚动之前,数据不会加载到 UITableView 中

ios - 网络连接应仅在更改时出现

iphone - 如何设置分组的 UITableView 的背景颜色?

iOS - 视频帧处理优化

ios - NavigationBar tintColor 不影响文本颜色

ios - CMDeviceMotion 标题为 -1.0

objective-c - 核心数据和REST

ios - 如何使用自动布局使 Nib View 适合标签的内容