简体   繁体   English

弹出视图控制器时崩溃

[英]Crash while popping view controller

I have one view controller (let's call it ViewController) with timer within its code, and have another view controller (let's call it ContentViewController) with webView within it which shows content depending on which button is clicked. 我在代码中有一个带有计时器的视图控制器(我们称之为ViewController),在其中有一个带webView的视图控制器(我们称之为ContentViewController),它根据单击哪个按钮显示内容。

I have buttons on ViewController which, when clicked, pushes ContentViewController which loads a html file into its webView. 我在ViewController上有按钮,单击该按钮时会推动ContentViewController,后者会将html文件加载到其webView中。

Timers are used to close ContentViewController if it is pushed. 计时器用于关闭ContentViewController(如果已推送)。

Here's how I create timers: 这是我创建计时器的方法:

-(void)createTimer :(NSNumber*)index
{
if (_show)
{
    NSDictionary *dictionary = [NSDictionary dictionaryWithObjectsAndKeys:index, @"parameter1", nil];
    switch ([index intValue])
    {
        case 1001:
            [NSTimer scheduledTimerWithTimeInterval:10.0 target:self selector:@selector(updateUi:) userInfo:dictionary repeats:YES];
            break;
        case 1002:
            [NSTimer scheduledTimerWithTimeInterval:10.0 target:self selector:@selector(updateUi:) userInfo:dictionary repeats:YES];
            break;
        case 1003:
            [NSTimer scheduledTimerWithTimeInterval:20.0 target:self selector:@selector(updateUi:) userInfo:dictionary repeats:YES];
            break;
        case 1004:
            [NSTimer scheduledTimerWithTimeInterval:20.0 target:self selector:@selector(updateUi:) userInfo:dictionary repeats:YES];
            break;
    }
}
}

Here's the code in ViewController which closes ContentViewController when timer fired: 这是ViewController中的代码,该代码在计时器触发时关闭ContentViewController:

-(void)updateUi :(NSTimer *)timer
{
int  index = [[timer.userInfo objectForKey:@"parameter1"] intValue];

if([self.navigationController.visibleViewController isKindOfClass:[ContentViewController class]])
{
    if ([[[NSUserDefaults standardUserDefaults]valueForKey:@"CurrentString"] intValue]==index )
    {
        [self.navigationController popViewControllerAnimated:YES];
    }
}
}

And on web page which is shown on ContentViewController there's a button,when user clicks it ContentView must go back to ViewController. 在ContentViewController上显示的网页上,有一个按钮,当用户单击它时,ContentView必须返回到ViewController。 Here's how I do this: 这是我的操作方式:

- (BOOL)webView:(UIWebView*)webView shouldStartLoadWithRequest:(NSURLRequest*)request navigationType:(UIWebViewNavigationType)navigationType
{
NSURL *URL = [request URL];
if ([[URL scheme] isEqualToString:@"coffee-drink"])
{
    NSString *urlString = [[request URL] absoluteString];
    NSArray *urlParts = [urlString componentsSeparatedByString:@":"];

    if (urlParts.count > 1)                          ///------------поменял
    {
        [self.navigationController popViewControllerAnimated:YES];
    }
}

return YES;
}    

All of above works fine but sometimes it crashes with the following error: 以上所有方法均能正常工作,但有时会因以下错误而崩溃:

[ContentViewController respondsToSelector:]: message sent to deallocated instance

And also, I have other errors like these: 而且,我还有其他类似这样的错误:

nested push animation can result in corrupted navigation bar 嵌套的推送动画可能会导致导航栏损坏

Finishing up a navigation transition in an unexpected state. 在意外状态下完成导航过渡。 Navigation Bar subview tree might get corrupted. 导航栏子视图树可能已损坏。

and

delegate (webView:decidePolicyForNavigationAction:request:frame:decisionListener:) failed to return after waiting 10 seconds 委托(webView:decidePolicyForNavigationAction:request:frame:decisionListener :)在等待10秒后无法返回

The last one appears very rarely. 最后一个很少出现。 I don't know what am I doing wrong.Can someone help me)?If needed any more information I will provide it!Thanks in advance! 我不知道我在做什么错,有人可以帮我吗?如果需要更多信息,我将提供!谢谢!

Issue 问题

You are popping your view without leaving ARC to deallocate all objects and delegates, by keeping them alive. 您在弹出视图时没有离开ARC,而是通过使它们保持活动状态来取消分配所有对象和委托。

My Solution 我的解决方案

For the NSTimer 对于NSTimer

At @implementation create NSTimer *timer and use it when you want to initialize it. 在@implementation处创建NSTimer *timer并在要初始化它时使用它。 To dealloc it correctly when you pop back, at viewWillDisappear set [timer invalidate] and timer = nil . 要在弹出时正确取消分配,请在viewWillDisappear处设置[timer invalidate]timer = nil

For delegates 代表们

Set you specific delegates to nil. 将您的特定代表设置为nil。 For example self.delegate = nil 例如self.delegate = nil

@Yuandra Ismiraldi is right. @Yuandra Ismiraldi是正确的。 Your NSTimer will call your updateUI method every 20/10 seconds repeatedly untill it receives [timer invalidate]; 您的NSTimer将每20/10秒重复调用一次updateUI方法,直到收到[timer invalidate]; instruction. 指令。 You are getting this error messages as you navigate through your view controller tree. 在视图控制器树中导航时,会收到此错误消息。 Sometimes your NavigationViewController will need more memory and will release some of un-used objects. 有时, NavigationViewController需要更多的内存,并释放一些未使用的对象。 In your case some of the previously shown view controllers that still runs the updateUI method every 20/10 seconds. 在您的情况下,某些先前显示的视图控制器仍然每20/10秒运行一次updateUI方法。 Once this view controller has been released and your selector is called you will receive this crash. 释放此视图控制器并调用选择器后,您将收到此崩溃。

I hope this explains why sometimes you get this crash, and sometimes not. 我希望这可以解释为什么有时会崩溃,而有时却不会。

Your timer code 您的计时器代码

[NSTimer scheduledTimerWithTimeInterval:10.0 target:self selector:@selector(updateUi:) userInfo:dictionary repeats:YES] [NSTimercheduledTimerWithTimeInterval:10.0目标:自我选择器:@selector(updateUi :) userInfo:字典重复:是]

the repeat parameter is set to YES, this make your timer always repeat, thus repeating the sending of the message updateUI after the timer has run for the first time and the view has popped, thus resulting in your error. 将repeat参数设置为YES,这会使您的计时器始终重复,从而在计时器首次运行并且视图弹出后重复发送消息updateUI。 To make sure your timer only run once, set the repeat to NO 为确保您的计时器仅运行一次,请将重复设置为“否”

[NSTimer scheduledTimerWithTimeInterval:10.0 target:self selector:@selector(updateUi:) userInfo:dictionary repeats:NO]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM