gpt4 book ai didi

ios - 关闭模态视图时无法形成对 UIScrollView 子类 (EXC_BAD_INSTRUCTION) 的弱引用

转载 作者:行者123 更新时间:2023-11-28 18:29:46 25 4
gpt4 key购买 nike

大家好,我调试这个问题已经有一段时间了,但到目前为止运气不好。我在这里很迷路,不知道导致这次崩溃的原因以及如何解决它。如果有人能为此提供帮助,我将不胜感激,非常感谢!

我准备了一个示例项目来演示 GitHub here 上的问题.

场景如下:

  1. 有两个 View Controller ,即 Root View 和模态视图,每个 View Controller 都有一个自定义 ScrollView (类即SubScorllView)作为 subview ,模态视图有一个按钮关闭模态视图。

  2. ScrollView 是 UIScrollView 的子类,每个子类都有对应的委托(delegate)协议(protocol),它们的类层次结构如下:

UIScrollView
∟ SuperScrollView
.....∟ SubScrollView

应用程序以非常简单的方式启动和运行,在 AppDelegate 的 didFinishLaunchingWithOptions 中:

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

self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
self.window.backgroundColor = [UIColor blackColor];

RootViewController * rootVC = [[RootViewController alloc] init];
self.navVC = [[UINavigationController alloc] initWithRootViewController:rootVC];
self.navVC.navigationBarHidden = TRUE;

self.window.rootViewController = self.navVC;
[self.window makeKeyAndVisible];

ModalViewController *modalVC = [[ModalViewController alloc] init];
[self.navVC presentViewController:modalVC animated:YES completion:nil];

return YES;
}

并且 View 是从 xib 文件中加载的, ScrollView 的委托(delegate)也在其中设置,并且有一些关于 ScrollView 子类的委托(delegate)和设置委托(delegate)的方法的覆盖。

当我通过单击模态视图中的“关闭”按钮关闭模态视图时出现问题,当单击该按钮时,会发生以下情况:

- (IBAction)didPressedCloseButton:(id)sender {

self.subScrollView.delegate = nil;
[self dismissViewControllerAnimated:YES completion:nil];

}

应用程序在 SuperScrollView 中的以下部分崩溃:

- (void)setDelegate:(id<SuperScrollViewDelegate>)delegate {

_superScrollViewDelegate = delegate;

// trigger UIScrollView to re-examine delegate for selectors it responds
super.delegate = nil;
super.delegate = self; // app crashes at this line
}

在控制台中出现以下错误消息:

objc[6745]: Cannot form weak reference to instance (0x7fa803839000) of class SubScrollView. It is possible that this object was over-released, or is in the process of deallocation.

我不明白为什么应用程序会崩溃并给出上述错误消息,或者我应该如何解决它。我尝试使用错误消息进行搜索,但似乎该消息主要与 TextView 等其他类相关,而其他一些人通过在解除分配之前将 ScrollView 的委托(delegate)设置为 nil 来解决它,但在我的情况下不起作用。

==========

更新:刚刚测试过这种情况是否发生在带有模拟器的 iOS 8 上,它根本不会像在 iOS 9 上那样崩溃。

最佳答案

当 SuperScrollView 被释放时,setDelegate 被隐式调用。在 iOS 9 中,你不能将委托(delegate)设置为 self,因为 self 正在被释放(不知道为什么这在 iOS 8 中有效)。要解决此问题,您可以先检查传入的委托(delegate)参数是否不为 nil,然后再将 super.delegate 设置为 self:

- (void)setDelegate:(id<SuperScrollViewDelegate>)delegate {

_superScrollViewDelegate = delegate;

// trigger UIScrollView to re-examine delegate for selectors it responds
super.delegate = nil;

if(delegate)
{
super.delegate = self;
}
}

如果出于某种原因您需要支持自响应 UIScrollView 委托(delegate)方法,即使 _superScrollViewDelegate 为 nil,您也可以创建一个参数

@interface SuperScrollView ()

@property (nonatomic, weak) SuperScrollView * weakSelf;

@end

在文件的顶部,并在setup中设置

- (void)setup {

super.delegate = self;
self.weakSelf = self;
}

然后,在 setDelegate 中,只需检查 weakSelf 是否不为 nil。如果 weakSelf 为 nil,则 self 处于释放过程中,您不应将其设置为 super.delegate:

- (void)setDelegate:(id<SuperScrollViewDelegate>)delegate {

_superScrollViewDelegate = delegate;

// trigger UIScrollView to re-examine delegate for selectors it responds
super.delegate = nil;

if(self.weakSelf)
{
super.delegate = self;
}
}

关于ios - 关闭模态视图时无法形成对 UIScrollView 子类 (EXC_BAD_INSTRUCTION) 的弱引用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36002247/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com