gpt4 book ai didi

ios - 按下后退按钮时调用 scrollViewDidScroll 并使应用程序崩溃

转载 作者:可可西里 更新时间:2023-11-01 05:03:14 29 4
gpt4 key购买 nike

我的应用程序出现奇怪的结果。当我按下后退按钮时,它总是调用 scrollViewDidScroll 并且它使我的应用程序崩溃。

如果我没有滚动到底部(只需加载 View 然后按下后退按钮),就不会发生错误。但是当我滚动到底部然后按下后退按钮时它会强制退出。如果我滚动到底部然后再次滚动回到 tableview 的中间也没有错误。

这是我的代码片段。

    - (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];
}
}

在 .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

我已经通过实现 viewWillDisappear 防止在按下后退按钮时不调用 scrollviewdidscroll 方法,但这没有帮助。该应用程序仍然崩溃并且没有错误消息。

有人知道这个问题吗?

最佳答案

将 UIScrollView 或继承类(UITableView、UICollectionView 等)的委托(delegate)设置为 nil

objective-C :

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

swift :

deinit {
scrollView.delegate = nil
}

关于ios - 按下后退按钮时调用 scrollViewDidScroll 并使应用程序崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24113751/

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