Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Objective c 将视图控制器设置为';初始视图控制器';与使用presentModalViewController加载相比_Objective C_Ios_Uiviewcontroller - Fatal编程技术网

Objective c 将视图控制器设置为';初始视图控制器';与使用presentModalViewController加载相比

Objective c 将视图控制器设置为';初始视图控制器';与使用presentModalViewController加载相比,objective-c,ios,uiviewcontroller,Objective C,Ios,Uiviewcontroller,我的应用程序有一个跟踪用户位置的地图。此地图仅在特定情况下才会出现,并将在特定任务完成之前占据用户的注意力,这就是为什么地图不是导航或选项卡栏UI的一部分 如果将my map VC设置为故事板中的初始视图控制器,则效果良好。但是如果我试着从其他地方像这样加载地图VC MapViewController *mapVC = [[MapViewController alloc] init]; [self presentModalViewController:mapVC animated:YES];

我的应用程序有一个跟踪用户位置的地图。此地图仅在特定情况下才会出现,并将在特定任务完成之前占据用户的注意力,这就是为什么地图不是导航或选项卡栏UI的一部分

如果将my map VC设置为故事板中的初始视图控制器,则效果良好。但是如果我试着从其他地方像这样加载地图VC

MapViewController *mapVC = [[MapViewController alloc] init];
[self presentModalViewController:mapVC animated:YES];
我刚得到一个黑屏


我可以使用NSLog确认VC正在调用viewDidLoad和ViewDidDisplay,但VC的“map”属性为(null)。我不明白为什么(或如何)在使用此技术时需要手动创建map属性,但当它是初始VC时,我就可以完成此操作。

序列图像板中的MapViewController实例配置了视图层次结构,包括MKMapView,以及您为在情节提要中配置特定实例所做的任何其他操作

现在在这里展示的代码中,您正在创建MapViewController的一个全新实例。它与故事板中的实例没有关系,只是它们碰巧属于同一类。因此,您在此处使用
[[MapViewController alloc]init]
创建的一个没有视图层次结构(这就是您看到黑屏的原因),也没有您可能对故事板中的另一个MapViewController所做的任何输出或其他配置

因此,您需要加载已从情节提要中设置的MapViewController。假设您是在已从同一情节提要加载的另一个视图控制器的方法中执行此操作,则可以执行以下操作:

// within some method on another vc from a scene in the same storyboard:

// given an identifier for the map view controller we want to load:
static NSString *mapVCIdentifier = @"SomeAppropriateIdentifier";

NSLog(@"Storyboard: %@",self.storyboard); // make sure this vc(self) was loaded from a storyboard
MapViewController *mapVC = [self.storyboard instantiateViewControllerWithIdentifier:mapVCIdentifier];
[self presentModalViewController:mapVC animated:YES];
然后回到故事板,只需确保将此地图视图控制器的标识符设置为“somepropertiedentifier”


希望这能有所帮助。

这很有效。非常感谢您的详细回复。我一定会花时间好好理解它。很高兴这有帮助。祝应用程序好运。