gpt4 book ai didi

ios - 带有ARC的UITableView上的EXC_BAD_ACCESS错误?

转载 作者:行者123 更新时间:2023-12-01 16:48:38 25 4
gpt4 key购买 nike

我正在构建一个带有自定义栏选项卡的简单应用程序,该选项卡从另一个ViewController中的UITableView加载ViewController的内容。

但是,每次尝试在tableview上滚动时,都会收到exc_bad_access错误。我启用了NSzombies并保护了malloc以获取有关此问题的更多信息。

在控制台中,我得到:

"message sent to deallocated instance 0x19182f20" 

分析后,我得到:
#   Address             Category                    Event Type  RefCt     Timestamp Size    Responsible Library   Responsible Caller
56 0x19182f20 FirstTabBarViewController Zombie -1 00:16.613.309 0 UIKit -[UIScrollView(UIScrollViewInternal) _scrollViewWillBeginDragging]

这是发生错误的ViewController的一些代码:
.h文件:
#import <UIKit/UIKit.h>
#import "DataController.h"

@interface FirstTabBarViewController : UIViewController <UITableViewDelegate, UITableViewDataSource> {
IBOutlet UITableView* tabBarTable;
}

@property (strong, nonatomic) IBOutlet UIView *mainView;
@property (strong, nonatomic) IBOutlet UITableView *tabBarTable;
@property (nonatomic, strong) DataController *messageDataController;

@end

.m文件:
#import "FirstTabBarViewController.h"
#import "DataController.h"

@interface FirstTabBarViewController ()

@end

@implementation FirstTabBarViewController
@synthesize tabBarTable=_tabBarTable;

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

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

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

- (void)awakeFromNib
{
[super awakeFromNib];
self.messageDataController=[[DataController alloc] init];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}

- (NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return [self.messageDataController countOfList];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"mainCell";
UITableViewCell *cell = [tableView
dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil){
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
};

NSString *expenseAtIndex = [self.messageDataController
objectInListAtIndex:indexPath.row];
[[cell textLabel] setText:expenseAtIndex];
return cell;
}


- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
// Return NO if you do not want the specified item to be editable.
return NO;
}

@end

使用以下自定义设置将此FirstTabBarViewController加载到MainViewController中:
#import "customTabBarSegue.h"
#import "MainViewController.h"

@implementation customTabBarSegue

-(void) perform {
MainViewController *src= (MainViewController *) [self sourceViewController];
UIViewController *dst=(UIViewController *)[self destinationViewController];

for (UIView *view in src.placeholderView.subviews){
[view removeFromSuperview];
}

src.currentViewController =dst;
[src.placeholderView addSubview:dst.view];



}
@end

Datacontroller类只是一个包含字符串的简单NSMutableArray。

我正在使用ARC,所以我不知道内存管理错误的来源。有人知道吗?

任何帮助,不胜感激;)
谢谢!!

最佳答案

好的,谢谢您的代码示例。

参见UIStoryboardSegue documentation,当您在perform中实现customTabBarSegue时,您负责最后设置两个viewController之间的正确关系
您有2种可能性:

  • 将dst设置为模态子代(然后src是dst的presentingViewController),将此代码添加到perform的末尾:[src presentViewController:dst animated:NO completion:nil];
  • 将dst设置为src的子viewController-添加此代码:[src addChildViewController:dst];perform的末尾,但是,您必须在某个位置将其删除,或使用相同的方法删除其他子项....

  • 这是一个实现
    @implementation customTabBarSegue

    -(void) perform {
    MainViewController *src= (MainViewController *) [self sourceViewController];
    UIViewController *dst=(UIViewController *)[self destinationViewController];

    for (UIView *view in src.placeholderView.subviews){
    [view removeFromSuperview];
    }

    src.currentViewController =dst;
    [src.placeholderView addSubview:dst.view];

    // this container only show 1 viewController at a time, so we can remove previous ones
    for (UIViewController *vc in src.childViewControllers) {
    [vc.view removeFromSuperview];
    [vc removeFromParentViewController];
    }
    //then add the new View controller as child
    [src addChildViewController:dst];

    }
    @end

    无论如何,您应该明确地更多地研究UIViewController包含...

    关于ios - 带有ARC的UITableView上的EXC_BAD_ACCESS错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18019052/

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