我正在使用使用cocoapods安装的TWTSideMenuViewController编写应用程序。为此我有一个名为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;
}
然后我使用以下代码设置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;
所有这一切都很完美,但是当我切换到不同的内容视图时(除了默认的家庭视图,我尝试回到家中)我收到了EXC_BAD_ACCESS错误。我可以在任何其他内容之间来回切换查看控制器,因为看起来HomeViewController是唯一一个解除分配的。
我觉得这与在设置代码中访问它的事实有关,但有没有办法解决这个问题?
答案 0 :(得分:3)
您必须将每个视图控制器的对象声明为strong,并将其传递给数组,如
@property(strong,nonatomic) HomeViewController *home;
home = [[HomeViewController alloc] initWithNibName:@"HomeViewController" bundle:nil]
_controllerArray = [NSArray arrayWithObjects:home,aboutme....];
在旧代码中,类对象未使用强引用声明。强引用对象会使保留循环。这意味着您将成为此对象的所有者。
但是当你将对象声明为local时,你就没有强大的引用。这意味着它将在当前执行循环完成后自动解除分配。这就是为什么数组中的 HomeViewController 本地对象被取消分配并显示错误
答案 1 :(得分:0)
podspec已在GitHub上更新,指定ARC。
了解更多信息:https://github.com/twotoasters/TWTSideMenuViewController/pull/9