vishnu
vishnu

Reputation: 239

Pageview controller with webview crashing

I am using a webview in my contentviewcontroller, when I slide between pages my app crashes.
Below is my code:

contentviewcontroller.m

#import "ContentViewController.h"

@interface ContentViewController ()

@end

@implementation ContentViewController
@synthesize m_CtrlWebview,DocumentPath;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    m_CtrlWebview.scrollView.pagingEnabled=YES;
    m_CtrlWebview.scrollView.bounces=NO;
    m_CtrlWebview.scalesPageToFit=YES;
    m_CtrlWebview.backgroundColor=[UIColor clearColor];
    //m_CtrlWebview.scrollView.zooming=NO;
    m_CtrlWebview.scrollView.zoomScale=1.0;



    NSURL *url = [NSURL fileURLWithPath:DocumentPath isDirectory:NO];

    [m_CtrlWebview loadRequest:[NSURLRequest requestWithURL:url]];
}


-(void)dealloc
{
    [super dealloc];
}
- (void)viewDidUnload
{
    //[m_CtrlWebview stopLoading];
    [m_CtrlWebview release];
    [super viewDidUnload];
}
- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end

In rootviewcontroller:

  #pragma mark - UIPageViewControllerDataSource Methods

- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController
      viewControllerBeforeViewController:(UIViewController *)viewController
{
    NSUInteger currentIndex = [self.modelArray indexOfObject:[(ContentViewController *)viewController DocumentPath]];
    if(currentIndex == 0)
    {
        return nil;
    }

    ContentViewController *firstViewController = [self viewControllerAtIndex:currentIndex - 1];
    // ContentViewController *secondViewController = [self viewControllerAtIndex:x+1 ];


    return firstViewController;



}

- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController
       viewControllerAfterViewController:(UIViewController *)viewController
{
    NSUInteger currentIndex = [self.modelArray indexOfObject:[(ContentViewController *)viewController DocumentPath]];
    if(currentIndex == self.modelArray.count-1)
    {
        return nil;
    }
    NSString *Path=[self.modelArray objectAtIndex:currentIndex + 1];
     if  (![[NSFileManager defaultManager] fileExistsAtPath:Path])
     {
        return nil; 
     }
    else
    {
        ContentViewController *firstViewController = [self viewControllerAtIndex:currentIndex + 1];

        return firstViewController;
    }

}

- (ContentViewController *)viewControllerAtIndex:(NSUInteger)index {

    if (([self.modelArray count] == 0) || (index >= [self.modelArray count])) {
        return nil;
    }
    ContentViewController *dataViewController;
    dataViewController = [[ContentViewController alloc]initWithNibName:@"ContentViewController" bundle:nil];

    dataViewController.DocumentPath = [self.modelArray objectAtIndex:index];
    return dataViewController;

}

How can I solve this issue? Also how to disable the zooming of the web view? Any help, Thanks in advance.

Upvotes: 1

Views: 572

Answers (2)

vishnu
vishnu

Reputation: 239

return [dataViewController autorelease];

The above line in - (ContentViewController *)viewControllerAtIndex:(NSUInteger)index solved my issue.

Upvotes: 1

Akshay Shah
Akshay Shah

Reputation: 1120

From this code it is not really sure why the app crashes. To be more specific to catch the error, add an exception breakpoint in Xcode. Steps to do that for Xcode 4.5

  1. Select the "Breakpoint Navigator" in left pane.

  2. Tap "+" button at bottom left.

  3. From the popped-up menu, select "Add exception breakpoint"

  4. When app crashes after doing this setting, check your stack trace to see if any of your function is in the stack trace.

Also enable Zombie objects and log exceptions:

  1. Go to Product -> Edit Scheme
  2. Select Run YourApp.app from left pane.
  3. Select Diagnostics in Right pane.
  4. Check boxes for "Enable Zombie Objects" and "Log Exceptions". This may print some useful information on console when app crashes.

Hope this may help you resolve your problem.

Upvotes: 0

Related Questions