简体   繁体   English

NSArray中的一个对象被释放

[英]One object in NSArray being deallocated

I am writing an app using the TWTSideMenuViewController installed using cocoapods. 我正在使用使用cocoapods安装的TWTSideMenuViewController编写应用程序。 For this I have a ViewController titled MenuViewController which has an array of all the individual content view controllers: 为此,我有一个名为MenuViewController的ViewController,其中包含所有单独的内容视图控制器的数组:

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
        _controllerArray = [NSArray arrayWithObjects:
                            [[HomeViewController alloc] initWithNibName:@"HomeViewController" bundle:nil],
                            [[AboutMeViewController alloc] initWithNibName:@"AboutMeViewController" bundle:nil],
                            [[ScheduleViewController alloc] initWithNibName:@"ScheduleViewController" bundle:nil],
                            [[FoodViewController alloc] initWithNibName:@"FoodViewController" bundle:nil],
                            [[CalendarViewController alloc] initWithNibName:@"CalendarViewController" bundle:nil],
                            [[AthleticsViewController alloc] initWithNibName:@"AthleticsViewController" bundle:nil],
                            [[DirectoryViewController alloc] initWithNibName:@"DirectoryViewController" bundle:nil], nil];
    }
    return self;
}

I then set up the TWTSideMenuViewController with this code: 然后,使用以下代码设置TWTSideMenuViewController:

MenuViewController *menuViewController = [[MenuViewController alloc] initWithNibName:@"MenuViewController" bundle:nil];
TWTSideMenuViewController *twtSideMenuViewController = [[TWTSideMenuViewController alloc] initWithMenuViewController:menuViewController mainViewController:[[menuViewController controllerArray] objectAtIndex:0]];
twtSideMenuViewController.zoomScale = .5;
twtSideMenuViewController.edgeOffset = UIOffsetMake(-80, 0);
self.window.rootViewController = twtSideMenuViewController;

All of this works perfectly, however when I switch to a different content view (other than the default, home, and I try to go back to home I get an EXC_BAD_ACCESS error. I can switch back and forth between any other content view controllers, as it appears the HomeViewController is the only one deallocated. 所有这些都可以正常工作,但是当我切换到其他内容视图时(默认的主页除外),我尝试回到主页时会收到EXC_BAD_ACCESS错误。我可以在任何其他内容视图控制器之间来回切换,看起来HomeViewController是唯一一个已释放的对象。

I have a feeling that this has to do with the fact that it is accessed in the setup code, but is there any way to fix this issue? 我感觉这与在安装代码中访问它有关,但是有什么办法可以解决此问题?

You have to declare each view controller's object as strong and pass it to the array like 您必须将每个视图控制器的对象声明为强对象,并将其传递给数组,例如

@property(strong,nonatomic) HomeViewController *home;



home = [[HomeViewController alloc] initWithNibName:@"HomeViewController" bundle:nil]
_controllerArray = [NSArray arrayWithObjects:home,aboutme....];

In your old code, class objects are not declared with strong reference . 在您的旧代码中,未使用强引用声明类对象。 A strong referenced object would make retain loop . 一个强引用的对象将使保持循环 That means you will become the owner of this object. 这意味着您将成为该对象的所有者。

But when you declare object as local, you dont have the strong reference on it. 但是,当您将对象声明为局部对象时,就没有强大的引用。 That means it will deallocated automatically after current execution loop complete. 这意味着它将在当前执行循环完成后自动释放。 That's why your HomeViewController local object in the array getting deallocated and shows the error 这就是为什么数组中的HomeViewController本地对象被释放并显示错误的原因

The podspec has been updated on GitHub specifying ARC. podspec已在GitHub上更新,指定了ARC。

For more info: https://github.com/twotoasters/TWTSideMenuViewController/pull/9 有关更多信息: https : //github.com/twotoasters/TWTSideMenuViewController/pull/9

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM