MapController Alloc Init & ViewController alloc init Resulting in loop

时间:2018-12-19 11:10:32

标签: ios objective-c iphone xcode

I Have a very long view controller.m which I'm trying to split into separate files to keep it organised. I know I can create separate .m .h files and separate them but in doing so I am ending up in loops (which I Understand since the MapController.m is initialising the viewcontroller again causing viewDidLoad to be triggered and repeat.

I have a Map I want to create in my MapController.m

-(void)setMapSettings{
    NSLog(@"test");
    viewController = [[ViewController alloc] init];
    SKMapView *mapView = [[SKMapView alloc] initWithFrame:CGRectMake( 0.0f, 0.0f,  CGRectGetWidth(viewController.view.frame), CGRectGetHeight(viewController.view.frame) )];

}

I want to add the above SKMapView to a UIView called mapViewContainer in my viewController.h

ViewController.m:

- (void)viewDidLoad {
    mapsViewController = [[MapController alloc] init];
    [mapsViewController setMapSettings];
}

Doing this creates a loop resulting in a crash. I know the answer is probably dead simple and I'm just being stupid, but I've spent hours finding a solution and I can't figure it out. Any idea's how I'm suppose to be able to do the above without looping, and without initialising the map views I require all in the ViewController.m?

1 个答案:

答案 0 :(得分:0)

您说您是通过从视图控制器中删除代码来拆分视图控制器的,但是据我所知,这导致创建了另一个视图控制器(此名称或“ MapController”都不正确)。 / p>

无论如何,就像您已经提到的那样,崩溃肯定是由alloc / init循环引起的:您应该重新设计代码,以使两个对象之一实例化另一个对象。

(请记住,如果这样做是为了从另一个对象访问一个对象,那是错误的:alloc / init每次创建该类的 new 实例调用,它不会为您提供指向现有对象的指针)