当dismissViewControllerAnimated被调用时,app崩溃了

时间:2014-10-01 07:49:32

标签: xcode ios8

首先,在app中呈现模态viewcontroller。 在modalview控制器中执行某些操作后,

调用dismissViewControllerAnimated,app崩溃了。 请帮助我:(

下面的

是我在控制台中的异常日志

2014-10-01 18:05:42.508 mlink[569:232256] url - mfapp://navigation/dismissModal(GET)
2014-10-01 18:05:43.289 mlink[569:232256] viewDidAppear
2014-10-01 18:05:43.331 mlink[569:232256] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'An instance 0x14534c40 of class _UIWebViewScrollView was deallocated while key value observers were still registered with it. Current observation info: <NSKeyValueObservationInfo 0x146dfde0> (
<NSKeyValueObservance 0x146e9cd0: Observer: 0x146e1c20, Key path: contentOffset, Options: <New: YES, Old: NO, Prior: NO> Context: 0x0, Property: 0x145e5a30>
)'
*** First throw call stack:
(0x23f19e3f 0x315c7c8b 0x23f19d85 0x24bc2455 0x273b350f 0x27482fd5 0x273b5a87 0x275c1135 0x23e2126d 0x23e383bd 0x315e1d5f 0x315e21a9 0x23e2c3a9 0x23ede2ef 0x23e2c621 0x23e2c433 0x2b1db0a9 0x27417359 0xe1a45 0x31b47aaf)
libc++abi.dylib: terminating with uncaught exception of type NSException

下面是我的baseWebiewConntroller源

//
//  MFBaseWebViewController.m
//  ElandMobileFramework
//
//  Created by subicura on 12. 2. 10..
//  Copyright (c) 2012년 purpleworks. All rights reserved.
//

#import "MFBaseWebViewController.h"
#import "IIViewDeckController.h"
#import "PullToRefreshView.h"
#import "MFSettingHelper.h"
#import "MFUrlHelper.h"
#import "MFLocalizationHelper.h"
#import "MFColorHelper.h"
#import "SVProgressHUD.h"
#import "MFShellCoordinator.h"
#import "NSString+Extention.h"

@class WebFrame;
@interface UIWebView(JavaScriptAlert) <UITextFieldDelegate>
- (void)webView:(UIWebView *)sender runJavaScriptAlertPanelWithMessage:(NSString *)message initiatedByFrame:(WebFrame *)frame;
- (BOOL)webView:(UIWebView *)sender runJavaScriptConfirmPanelWithMessage:(NSString *)message initiatedByFrame:(WebFrame *)frame;
- (NSString *)webView:(UIWebView *)sender runJavaScriptTextInputPanelWithPrompt:(NSString *)prompt defaultText:(NSString *)defaultText initiatedByFrame:(WebFrame *)frame;
@end

@interface MFBaseWebViewController ()
- (void)initControls;
- (void)initWebView;
- (void)initSubView;
- (NSString *)getBlankPageScript;
- (void)willEnterForeground;
- (void)willEnterBackground;
@end

@implementation MFBaseWebViewController

static UIAlertView *webviewDialog;

@synthesize parent;

@synthesize webView;
@synthesize topSubView;
@synthesize bottomSubView;

@synthesize indicator;
@synthesize dispatchRouter;
@synthesize deviceOrientation;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        lastActiveTimestamp = 0;
        isViewDidAppear = NO;
        dispatchRouter = [[MFDispatchRouter alloc] initWithViewController:self];
        deviceOrientation = [MFSettingHelper getValue:@"Orientation" defaultValue:@"portrait"];

        self.webView = [[UIWebView alloc] init];
        self.webView.tag = MF_BASE_UI_WEBVIEW_TAG;
        self.indicator = [[UIActivityIndicatorView alloc] init];
        self.indicator.tag = MF_BASE_UI_INDICATOR_VIEW_TAG;
        self.topSubView = [[UIView alloc] init];
        self.topSubView.tag = MF_BASE_UI_TOP_SUB_VIEW_TAG;
        self.bottomSubView = [[UIView alloc] init];
        self.bottomSubView.tag = MF_BASE_UI_BOTTOM_SUB_VIEW_TAG;
    }
    return self;
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];

    deviceOrientation = [MFSettingHelper getValue:@"Orientation" defaultValue:@"portrait"];
    if (![deviceOrientation isEqualToString:[[NSUserDefaults standardUserDefaults] valueForKey:@"deviceOrientation"]]) {
        MFBaseWebViewController *temp = [[MFBaseWebViewController alloc] init];
        [self presentModalViewController:temp animated:NO];
        [temp dismissModalViewControllerAnimated:NO];
    }
}

- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];

    // Release any cached data, images, etc that aren't in use.
    [[NSURLCache sharedURLCache] removeAllCachedResponses];
}

#pragma mark - View lifecycle
/*
// Implement loadView to create a view hierarchy programmatically, without using a nib.
- (void)loadView
{
}
*/

// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad
{
    [super viewDidLoad];

    if ([self respondsToSelector:@selector(edgesForExtendedLayout)])
        self.edgesForExtendedLayout = UIRectEdgeNone;

    [self initWebView];
    [self initControls];
    [self initSubView];
}

- (void)viewDidUnload
{
    [super viewDidUnload];
}

- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation
{
//    NSString *deviceOrientation = [[NSUserDefaults standardUserDefaults] valueForKey:@"deviceOrientation"];
    if ([deviceOrientation isEqualToString:@"both"] || [deviceOrientation isEqualToString:@"all"]) {
        if(UIInterfaceOrientationIsLandscape(fromInterfaceOrientation)) {
            self.viewDeckController.leftLedge = [[UIScreen mainScreen] bounds].size.width - MF_SLIDE_LEFT_MENU_WIDTH;
        } else {
            self.viewDeckController.leftLedge = [[UIScreen mainScreen] bounds].size.height - MF_SLIDE_LEFT_MENU_WIDTH;
        }
    }
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
//    NSString *deviceOrientation = [[NSUserDefaults standardUserDefaults] valueForKey:@"deviceOrientation"];
    if ([deviceOrientation isEqualToString:@"all"]) {
        if(UIInterfaceOrientationIsLandscape(interfaceOrientation)) {
            self.viewDeckController.leftLedge = [[UIScreen mainScreen] bounds].size.height - MF_SLIDE_LEFT_MENU_WIDTH;
        } else {
            self.viewDeckController.leftLedge = [[UIScreen mainScreen] bounds].size.width - MF_SLIDE_LEFT_MENU_WIDTH;
        }
        return YES;

    } else if ([deviceOrientation isEqualToString:@"portrait_all"]) {
        self.viewDeckController.leftLedge = [[UIScreen mainScreen] bounds].size.width - MF_SLIDE_LEFT_MENU_WIDTH;
        return UIInterfaceOrientationIsPortrait(interfaceOrientation);

    } else if ([deviceOrientation isEqualToString:@"landscape"]) {
        self.viewDeckController.leftLedge = [[UIScreen mainScreen] bounds].size.height - MF_SLIDE_LEFT_MENU_WIDTH;
        return UIInterfaceOrientationIsLandscape(interfaceOrientation);

    } else if ([deviceOrientation isEqualToString:@"both"]) {
        if(UIInterfaceOrientationIsLandscape(interfaceOrientation)) {
            self.viewDeckController.leftLedge = [[UIScreen mainScreen] bounds].size.height - MF_SLIDE_LEFT_MENU_WIDTH;
        } else {
            self.viewDeckController.leftLedge = [[UIScreen mainScreen] bounds].size.width - MF_SLIDE_LEFT_MENU_WIDTH;
        }
        return UIInterfaceOrientationPortrait == interfaceOrientation || UIInterfaceOrientationIsLandscape(interfaceOrientation);

    } else { // portrait
        self.viewDeckController.leftLedge = [[UIScreen mainScreen] bounds].size.width - MF_SLIDE_LEFT_MENU_WIDTH;
        return UIInterfaceOrientationPortrait == interfaceOrientation;
    }
}

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];

    // add observer app switching event
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(willEnterForeground)
                                                 name:UIApplicationWillEnterForegroundNotification 
                                               object:NULL];
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(willEnterBackground)
                                                 name:UIApplicationWillResignActiveNotification 
                                               object:NULL];

    if(isViewDidAppear == YES) { // 한번 로딩이 완료되었다면 새로 창을 보여줄때마다 함수 호출
        NSLog(@"viewDidAppear");
        time_t nowTimestamp = (time_t) [[NSDate date] timeIntervalSince1970];
        long check = nowTimestamp - lastActiveTimestamp;
        [self evalScript:[NSString stringWithFormat:@"if(viewDidAppear !== undefined) {viewDidAppear(%ld);}", check]];
    } else {
        isViewDidAppear = YES;
    }

}

- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];

    lastActiveTimestamp = (time_t) [[NSDate date] timeIntervalSince1970];

    // remove observer app switching event
    [[NSNotificationCenter defaultCenter] removeObserver:self 
                                                    name:UIApplicationWillEnterForegroundNotification 
                                                  object:NULL];
    [[NSNotificationCenter defaultCenter] removeObserver:self 
                                                    name:UIApplicationWillResignActiveNotification 
                                                  object:NULL];
}

- (void)willEnterForeground;
{
    NSLog(@"willEnterForeground");
    time_t nowTimestamp = (time_t) [[NSDate date] timeIntervalSince1970];
    long check = nowTimestamp - lastActiveTimestamp;
    [self evalScript:[NSString stringWithFormat:@"if(viewDidAppear !== undefined) {viewDidAppear(%ld);}", check]];
}

- (void)willEnterBackground;
{
    NSLog(@"willEnterBackground");
    lastActiveTimestamp = (time_t) [[NSDate date] timeIntervalSince1970];

    // 로딩바 제거
    [SVProgressHUD dismiss];
}

#pragma mark - init
- (void)initControls
{
    // loading indicator
    [self.indicator setActivityIndicatorViewStyle:UIActivityIndicatorViewStyleGray];
    self.indicator.hidesWhenStopped = YES;
    self.indicator.frame = CGRectMake(self.view.frame.size.width/2.0f-10.5f, 
                                 190, 
                                 21, 21);
    [self.view addSubview:self.indicator];

    //set view background color
    self.view.backgroundColor = [MFColorHelper htmlHexColor:[MFSettingHelper getValue:@"WebViewBackgroundColor"]];

}

- (void)initWebView
{
    // 웹뷰 설정
    [self.webView setFrame:self.view.bounds];
    [self.view addSubview:self.webView];
    [self.webView setAutoresizingMask:UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight];
    self.webView.delegate = self;
    self.webView.scalesPageToFit = YES;

    // 스크롤 뷰 속도 변경 및 그림자 없앰
    [self.webView.scrollView setDecelerationRate:UIScrollViewDecelerationRateNormal];

    UIView* lastView = [[self.webView.scrollView subviews] lastObject];
    for(UIView *wview in [self.webView.scrollView subviews]) { 
        if([wview isKindOfClass:[UIImageView class]] && wview != lastView) { 
            wview.hidden = YES; 
        } 
    }

    // 배경색 변경
    self.webView.backgroundColor = [MFColorHelper htmlHexColor:[MFSettingHelper getValue:@"WebViewBackgroundColor"]];
}

- (void)initSubView
{
    CGRect topFrame = self.view.bounds;
    topFrame.size.height = 0;
    [self.topSubView setFrame:topFrame];
    [self.topSubView setAutoresizingMask:UIViewAutoresizingFlexibleBottomMargin];
    [self.view addSubview:self.topSubView];


    CGRect bottomFrame = self.view.bounds;
    bottomFrame.origin.y = self.view.bounds.size.height;
    bottomFrame.size.height = 0;
    [self.bottomSubView setFrame:bottomFrame];
    [self.bottomSubView setAutoresizingMask:UIViewAutoresizingFlexibleTopMargin];
    [self.view addSubview:self.bottomSubView];
}

- (void)updateWebViewSize:(CGRect)frame subview:(UIView *)subview
{
    CGRect webViewFrame = self.webView.frame;
    webViewFrame.origin.x += frame.origin.x;
    webViewFrame.origin.y += frame.origin.y;
    webViewFrame.size.width += frame.size.width;
    webViewFrame.size.height += frame.size.height;
    self.webView.frame = webViewFrame;

    CGRect subViewFrame = subview.frame;
    subViewFrame.origin.y += frame.origin.y + frame.size.height;
    subViewFrame.size.height -= frame.size.height;
    [subview setFrame:subViewFrame];
}

- (NSString *)getBlankPageScript
{
    return [NSString stringWithFormat:@"document.body.innerHTML = ''; document.body.style.backgroundColor = '%@';", [MFSettingHelper getValue:@"WebViewBackgroundColor"]];
}

#pragma mark - webview event
- (void)webViewDidStartLoad:(UIWebView *)webView
{
    [self webViewLoadingStart];
}

- (void)webViewDidFinishLoad:(UIWebView *)webView
{
    [self webViewLoadingFinish];
}

- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error
{
    if(error.code != -999 && error.code != 204 && error.code != 102) { // ignore operation couldn't be complete error
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Loading Error" message:[error localizedDescription] delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
        [alert show];
        NSLog(@"%@", error);
        [self webViewLoadingFinish];
    } else {
        [self webViewLoadingFinish];
    }
}

// dom load 직후, not full page load (images, etc..)
- (void)webViewDidFinishDomLoad
{
    [self webViewLoadingFinish];
}

- (void)webViewLoadingStart
{
    [self.webView setUserInteractionEnabled:NO];
    [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:YES];    
}

- (void)webViewLoadingFinish
{
    [self.webView setUserInteractionEnabled:YES];
    [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:NO];
    [(PullToRefreshView *)[self.view viewWithTag:999] finishedLoading]; // @see MFWebViewCoordinator
    [indicator stopAnimating];
}

#pragma mark - WebViewDelegate
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)req navigationType:(UIWebViewNavigationType)navigationType
{
    NSMutableURLRequest *request = (NSMutableURLRequest *)req;

    // url check
    NSString* url = [[request URL] absoluteString];
    if(url.length <= 256) {
        NSLog(@"url - %@(%@)", url, request.HTTPMethod);
    } else {
        NSLog(@"url - %@ ...xxx...(%@)", [url substringToIndex:256], request.HTTPMethod);
    }

    if ([url hasPrefix:@"http"] == YES ) {
        NSArray *externalLinks = [MFSettingHelper getArrayValue:@"ExternalLink"];
        for(int i=0; i<[externalLinks count]; i++) {
            NSString *externalLink = [externalLinks objectAtIndex:i];
            if([url contains:externalLink]) {
                MFShellCoordinator *shellCoordinator = [[MFShellCoordinator alloc] init];
                [shellCoordinator dispatchRoute:nil viewController:self];
                [shellCoordinator callWebView:url disableMenu:NO];
                return NO;
            }
        }

        return YES;
    } else if([url hasPrefix:@"file://"] == YES) { // local file 접근시 무조건 통과
        return YES;
    } else if([url hasPrefix:@"mfapp://"] == YES) { // dom load
        MFUrlRoute *route = nil;
        if([request.HTTPMethod isEqualToString:@"GET"]) {
            route = [[MFUrlRoute alloc] initWithUrl:url];
        } else {
            route = [[MFUrlRoute alloc] initWithRequest:request];            
        }
        return [dispatchRouter dispatch:route];
    } else if ([[UIApplication sharedApplication] canOpenURL:[request URL]]) {
        [[UIApplication sharedApplication] openURL:[request URL]];
        return NO;
    }

    return YES;
}

#pragma mark - public method
- (void)loadUrl:(NSString *)url
{
    [self loadUrl:url method:@"GET"];
}

- (void)loadUrl:(NSString *)url method:(NSString *)method
{
    method = [method uppercaseString];
    NSAssert([method isEqualToString:@"GET"] || [method isEqualToString:@"POST"] || [method isEqualToString:@"PUT"] || [method isEqualToString:@"DELETE"], @"method must GET or POST or PUT or DELETE");

    [indicator startAnimating];
    // 이미 url에 값은 전부 인코딩 된걸로 간주한다.
    // NSString *escapedUrl = [url stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    [self.webView stringByEvaluatingJavaScriptFromString:[self getBlankPageScript]];

    NSURL *tmpURL = [NSURL URLWithString:url];
    if(tmpURL == nil) {
        tmpURL = [NSURL URLWithString:[url stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
    }

    NSMutableURLRequest *request = nil;

    if([method isEqualToString:@"GET"]) {
        request = [NSMutableURLRequest requestWithURL:tmpURL];  
    } else {
        // GET이 아니면 url에서 parameter를 파싱하여 따로 전송
        NSString *path = nil;
        NSRange paramRange = [url rangeOfString:@"?"];
        if(paramRange.length > 0) {
            path = [url substringToIndex:paramRange.location];            
            request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:path]];              

            // add post parameter
            //NSString *parameterString = [[[url substringFromIndex:paramRange.location + 1] stringByReplacingOccurrencesOfString:@"+" withString:@" "] stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
            NSString *parameterString = [url substringFromIndex:paramRange.location + 1];
            [request setHTTPBody:[parameterString dataUsingEncoding:NSUTF8StringEncoding]];
        } else {
            path = [NSString stringWithString:url];
            request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:path]];  
        }
    }
    [request setHTTPMethod:method];
    [self.webView loadRequest:request];

}

- (void)reloadUrl
{
    [self reloadUrl:NO];
}

- (void)reloadUrl:(BOOL)init
{
    if(init == YES) {
        [indicator startAnimating];
        [self.webView stringByEvaluatingJavaScriptFromString:[self getBlankPageScript]];
    }
    [self.webView reload];
}

- (void)evalScript:(NSString *)script
{
    [self evalScript:script async:NO];
}

- (void)evalScript:(NSString *)script async:(BOOL)async
{
    if(async == YES) {
        [self.webView stringByEvaluatingJavaScriptFromString:[NSString stringWithFormat:@"setTimeout(function() {%@}, 1);", script]];
    } else {
        [self.webView stringByEvaluatingJavaScriptFromString:script];        
    }
}

#pragma - subview manage
- (void)addTopSubview:(UIView *)subview
{    
    [self removeSubview:subview.tag];

    CGRect frame = subview.frame;
    frame.origin.y = self.topSubView.frame.size.height;
    [subview setFrame:frame];

    [self updateWebViewSize:CGRectMake(0, subview.frame.size.height, 0, -subview.frame.size.height) 
                    subview:self.topSubView];

    [self.topSubView addSubview:subview];

}

- (void)addBottomSubview:(UIView *)subview
{   
    [self removeSubview:subview.tag];

    CGRect frame = subview.frame;
    frame.origin.y = 0;
    [subview setFrame:frame];
    [subview setAutoresizingMask:subview.autoresizingMask | UIViewAutoresizingFlexibleTopMargin];

    [self updateWebViewSize:CGRectMake(0, 0, 0, -subview.frame.size.height) 
                    subview:self.bottomSubView];

    [self.bottomSubView addSubview:subview];
}

- (void)removeSubview:(NSInteger)tag
{    
    UIView *removeView = [self.view viewWithTag:tag];

    if(removeView) {
        int height = removeView.frame.size.height;
        BOOL isTopSubview = (removeView.superview == self.topSubView);
        BOOL isRemove = NO;
        if(isTopSubview) {
            for (UIView* subview in self.topSubView.subviews) {
                if(subview == removeView) {
                    isRemove = YES;
                    [self updateWebViewSize:CGRectMake(0, -height, 0, height) subview:self.topSubView];
                } else {
                    if(isRemove) {
                        CGRect frame = subview.frame;
                        frame.origin.y -= height;
                        subview.frame = frame;
                    }
                }
            }
        } else {
            for (UIView* subview in self.bottomSubView.subviews) {
                if(subview == removeView) {
                    isRemove = YES;
                    [self updateWebViewSize:CGRectMake(0, 0, 0, height) subview:self.bottomSubView];
                } else {
                    if(isRemove) {
                        CGRect frame = subview.frame;
                        frame.origin.y += height;
                        subview.frame = frame;
                    }
                }
            }
        }
        [[self.view viewWithTag:tag] removeFromSuperview];
    }
}

- (void)removeAllSubViews
{
    int height = 0;
    for (UIView* subview in self.topSubView.subviews) {
        height += subview.frame.size.height;
        [subview removeFromSuperview];
    }
    [self updateWebViewSize:CGRectMake(0, -height, 0, height) subview:self.topSubView];

    height = 0;
    for (UIView* subview in self.bottomSubView.subviews) {
        height += subview.frame.size.height;
        [subview removeFromSuperview];
    }
    [self updateWebViewSize:CGRectMake(0, 0, 0, height) subview:self.bottomSubView];
}

@end

@implementation UIWebView (JavaScriptAlert)

BOOL diagStat;

- (NSString *)webView:(UIWebView *)sender runJavaScriptTextInputPanelWithPrompt:(NSString *)prompt defaultText:(NSString *)defaultText initiatedByFrame:(WebFrame *)frame
{
    webviewDialog = [[UIAlertView alloc] initWithTitle:nil 
                                               message:prompt 
                                              delegate:self 
                                     cancelButtonTitle:[MFLocalizationHelper getAppleLocalizableLanguage:@"Cancel"]
                                     otherButtonTitles:[MFLocalizationHelper getAppleLocalizableLanguage:@"OK"], nil];
    webviewDialog.alertViewStyle = UIAlertViewStylePlainTextInput;
    UITextField *alertViewSheetTextField = [webviewDialog textFieldAtIndex:0];
    alertViewSheetTextField.delegate = self;
    [alertViewSheetTextField setPlaceholder:defaultText];

    [webviewDialog show];

    //버튼 누르기전까지 지연.
    while (webviewDialog.hidden == NO && webviewDialog.superview != nil) {
        [[NSRunLoop mainRunLoop] runUntilDate:[NSDate dateWithTimeIntervalSinceNow:0.01f]];
    }
    if (diagStat == YES) {
        return alertViewSheetTextField.text;
    } else {
        return nil;
    }
}

-(BOOL)textFieldShouldReturn:(UITextField *)textField{
    [webviewDialog dismissWithClickedButtonIndex:0 animated:YES];
    diagStat = YES;
    return YES;
}

- (void)webView:(UIWebView *)sender runJavaScriptAlertPanelWithMessage:(NSString *)message initiatedByFrame:(WebFrame *)frame
{
    while (webviewDialog.hidden == NO && webviewDialog.superview != nil) {
        [[NSRunLoop mainRunLoop] runUntilDate:[NSDate dateWithTimeIntervalSinceNow:0.01f]];
    }

    webviewDialog = [[UIAlertView alloc] initWithTitle:nil 
                                                          message:message 
                                                         delegate:nil 
                                                cancelButtonTitle:[MFLocalizationHelper getAppleLocalizableLanguage:@"OK"]
                                                otherButtonTitles:nil];
    [webviewDialog show];
}

- (BOOL)webView:(UIWebView *)sender runJavaScriptConfirmPanelWithMessage:(NSString *)message initiatedByFrame:(WebFrame *)frame
{
    webviewDialog = [[UIAlertView alloc] initWithTitle:nil message:message delegate:self cancelButtonTitle:[MFLocalizationHelper getAppleLocalizableLanguage:@"Cancel"] otherButtonTitles:[MFLocalizationHelper getAppleLocalizableLanguage:@"OK"], nil];
    [webviewDialog show];

    //버튼 누르기전까지 지연.
    while (webviewDialog.hidden == NO && webviewDialog.superview != nil) {
        [[NSRunLoop mainRunLoop] runUntilDate:[NSDate dateWithTimeIntervalSinceNow:0.01f]];
    }

    return diagStat;
}

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (buttonIndex == 0){
        diagStat = NO;
    } else if (buttonIndex == 1) {
        diagStat = YES;
    }
}

@end

下面是现有的模态方法

- (DispatchCallbackStatus)presentModal:(NSString *)url withTitle:(NSString *)title method:(NSString *)method
{
    if (url == nil) {
        return DispatchCallbackStatusParameterRequireException;
    }

    MFDetailViewController *viewController = [[MFDetailViewController alloc] initWithUrl:[MFUrlHelper getFullUrl:url
                                                                                                         withUrl:self.currentViewController.webView.request.URL]
                                                                               withTitle:title
                                                                                  method:method
                                                                                  parent:self.currentViewController];

    UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:viewController]; 
    UIBarButtonItem *navigationButton = [[UIBarButtonItem alloc] initWithTitle:NSLocalizedString(@"CLOSE_BUTTON", nil)
                                                                         style:UIBarButtonItemStyleBordered 
                                                                        target:self 
                                                                        action:@selector(dismissModal)];
    [viewController.navigationItem setLeftBarButtonItem:navigationButton];

//    [[self.currentViewController navigationController] presentModalViewController:navigationController animated:YES];
    [[self.currentViewController navigationController] presentViewController:navigationController animated:YES completion:nil];

    return DispatchCallbackStatusOk;
}

下面是dimissmodal方法

- (DispatchCallbackStatus)dismissModal
{
    if([self.currentViewController navigationController]) {
        [[self.currentViewController navigationController] dismissViewControllerAnimated:YES completion:nil];
//        [[self.currentViewController navigationController] dismissModalViewControllerAnimated:YES];
    } else {
//        [self.currentViewController dismissModalViewControllerAnimated:YES];
        [self.currentViewController dismissViewControllerAnimated:YES completion:nil];
    }
    return DispatchCallbackStatusOk;
}

1 个答案:

答案 0 :(得分:1)

我解决了问题...

问题是“istopped-PullToRefreshView”库。

我使用该库来刷新我的Web视图。 它是代表webview的scrollview。

PullToRefreshView *pull = [[PullToRefreshView alloc] initWithScrollView:currentScrollView];
        [pull setDelegate:self];
        pull.tag = MF_WEBVIEW_REFRESH_TAG;
        [currentScrollView addSubview:pull];

所以, 当webview消失时,我会删除观察者。 然后,问题解决了!!!

@try{
        [self removeObserver:self.webView.scrollView forKeyPath:@"contentOffset"];
        [self removeObserver:self.webView.scrollView forKeyPath:@"contentSize"];
        [self removeObserver:self.webView.scrollView forKeyPath:@"frame"];
    }
    @catch(id anException){

    }