Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

scrollViewDidScroll get called when back button pressed and make app crash

i got weird result on my app. when i pressed back button its always call scrollViewDidScroll and it makes my app crash.

error not happen if i'm not scrolling to the bottom (just load the view and then pressed back button). but when i scrolled to the bottom and then press back button it force quit. also no error if i'm scrolling to the bottom then scroll again back to the middle of tableview.

here is my code snippet.

    - (void)viewDidLoad
{
    [super viewDidLoad];
    self.title = @"Securities Instructions";


    currentPage = 1;
    isReloading = NO;
    totalPages = 1;
    isScrollingEnable = YES;
...... bla bla bla....
[self.tableView reloadData];
self.tableView.contentInset = UIEdgeInsetsMake(0, 0, 90, 0); //values passed are - top, left, bottom, right
}

-(void) viewWillDisappear:(BOOL)animated {
    if ([self.navigationController.viewControllers indexOfObject:self]==NSNotFound) {
        // back button was pressed.  We know this is true because self is no longer
        // in the navigation stack.
        NSLog(@"back button pressed");
        isScrollingEnable = NO;
    }
//    [super viewWillDisappear:animated];
}

- (void)scrollViewDidScroll:(UIScrollView *)aScrollView {
    NSLog(@"scroll view called");
    if (isScrollingEnable) {
        NSLog(@"scroll view get called: scroll enalbe");
        CGPoint offset = aScrollView.contentOffset;
        CGRect bounds = aScrollView.bounds;
        CGSize size = aScrollView.contentSize;
        UIEdgeInsets inset = aScrollView.contentInset;
        float y = offset.y + bounds.size.height - inset.bottom;
        float h = size.height;
        float reload_distance = 20;
        if(y > h + reload_distance) {
            if (isReloading==NO) {
                if (totalPages>1) {
                    if (currentPage<totalPages) {
                        NSLog(@"scroll view called");
                        currentPage++;
                        isReloading = YES;
                        [self getJsonOnLoad];
                    }
                }

            }
        }
    }
}

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
    NSLog(@"index:%d",buttonIndex);
    if (buttonIndex==0) {
        if (totalPages!=9999) {
            [self.navigationController popViewControllerAnimated:YES];
        }
    }
}


- (void) getTotalPages{
    NSArray *detailArray = [self.jsonResult objectForKey:@"detail"];
    int detailCount = [detailArray count];

    //server tidak konsisten,parameter totalpages di menu ini tidak ada (menu lain ada) -_-!
    if (detailCount>=25) {
        if ([jsonHeader objectForKey:@"totalRows"]) {
            totalPages = [[jsonHeader objectForKey:@"totalRows"] floatValue]/25;
        }else{
            //buat unlimited
            totalPages = 9999;
        }
    }
}


//orientation handling
- (void)canRotate{}

- (void)viewWillAppear:(BOOL)animated
{
    [[NSNotificationCenter defaultCenter] addObserver:self  selector:@selector(orientationChanged:)  name:UIDeviceOrientationDidChangeNotification  object:nil];

}

-(void)viewDidDisappear:(BOOL)animated{
    [[NSNotificationCenter defaultCenter]removeObserver:self name:UIDeviceOrientationDidChangeNotification object:nil];
}



- (void)orientationChanged:(NSNotification *)notification{
    NSLog(@"orientation change...");
    [self adjustViewsForOrientation:[[UIDevice currentDevice] orientation]];
}

- (void) adjustViewsForOrientation:(UIInterfaceOrientation) orientation {

    NSLog(@"orientation:%d",orientation);

    if (orientation == UIInterfaceOrientationPortrait)

        //    if (orientation == UIInterfaceOrientationPortrait || orientation == UIInterfaceOrientationPortraitUpsideDown)

    {
        landscape = NO;
        //harus di tambahin ini karena parentnya uiviewcontroller bukan uitableviewcontroller jadi tidak otomatis
        self.tableView.frame = CGRectMake(self.tableView.frame.origin.x, self.tableView.frame.origin.y, 320, 480);
        [self.tableView layoutSubviews];

        [self.tableView setSeparatorStyle:UITableViewCellSeparatorStyleSingleLine];
        [self.tableView reloadData];

        //load the portrait view
        NSLog(@"portraid");
        [[UIApplication sharedApplication] setStatusBarHidden:NO];
        [self.navigationController.navigationBar setHidden:NO];


    }
    //    else if (orientation == UIInterfaceOrientationLandscapeLeft)

    if (orientation == UIInterfaceOrientationLandscapeLeft || orientation == UIInterfaceOrientationLandscapeRight)
    {
        landscape = YES;
        [self.tableView setSeparatorStyle:UITableViewCellSeparatorStyleSingleLine];

        //harus di tambahin ini karena parentnya uiviewcontroller bukan uitableviewcontroller jadi tidak otomatis
        self.tableView.frame = CGRectMake(self.tableView.frame.origin.x, self.tableView.frame.origin.y, 480, 320);
        [self.tableView layoutSubviews];



        [self.tableView reloadData];

        //load the landscape view
        NSLog(@"landscape");

        [[UIApplication sharedApplication] setStatusBarHidden:NO];
        [self.navigationController.navigationBar setHidden:NO];
    }
}

in .h

#import <UIKit/UIKit.h>

@interface SecuritiesInstructionsResultViewController : UIViewController<UITableViewDataSource, UITableViewDelegate>{

    NSDictionary *jsonHeader;

    //untuk paging
    NSInteger currentPage;
    Boolean *isReloading;
    NSInteger totalRows;
    float totalPages;

    BOOL landscape;

    BOOL isScrollingEnable;
}

@property (strong, nonatomic) IBOutlet UITableView *tableView;
//@property NSMutableArray *rowData;

@property NSMutableArray *dataAccount;

@property NSMutableArray *dataCashAmount;
@property NSMutableArray *dataCode;
@property NSMutableArray *dataDate;
@property NSMutableArray *dataExtRef;
@property NSMutableArray *dataInsType;
@property NSMutableArray *dataSecuritiesAmmount;
@property NSMutableArray *dataStatus;

@property NSString *dateFrom;
@property NSString *dateTo;

@property NSDictionary *jsonResult;

@property NSString *selectedAccount;
@property NSString *selectedSecurityCode;
@property NSString *selectedSecuritySecCodeType;
@property NSString *selectedSecurityType;
@property NSString *selectedCurrencyCode;
@property NSString *selectedStatus;
@property NSString *selectedDate;
@property NSString *selectedToDate;



@end

i've prevent to not called scrollviewdidscroll method when back button pressed by implementing viewWillDisappear, but it doesn't help. the app is still crash and no error message.

Any one have a clue for this problem?

like image 241
Herahadi Avatar asked Dec 11 '22 05:12

Herahadi


1 Answers

Set delegate of UIScrollView or inherited classes (UITableView, UICollectionView and etc) to nil

Objective-C:

- (void)dealloc {
  [scrollView setDelegate:nil];
}

Swift:

deinit {
  scrollView.delegate = nil
}   
like image 95
Sunny Shah Avatar answered Jan 25 '23 22:01

Sunny Shah