无法从 AppDelegate 导航到 UIViewController
Unable to navigate to UIViewController from AppDelegate
我正在尝试从 AppDelegate didViewLoadFinished 函数导航,但我无法成功,因为我对 ios 完全陌生。我正在为 ViewControllers 使用故事板。
我还检查了其他类似的解决方案:Swift - pushViewController from appDelegate, rootViewController.navigationController is nil
但这并没有解决我的问题。
这是我在故事板上的 ViewController:
这是我尝试加载新 ViewController "MultipleMarkersViewController" 的方式:
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main.storyboard" bundle:nil];
MultipleMarkersViewController *wc=[storyboard instantiateViewControllerWithIdentifier:@"MultipleMarkersView"];
[self.window.rootViewController.navigationController pushViewController:wc animated:YES];
预期结果是加载新的ViewController。
但实际结果是 "The App crashes" 并具有以下日志输出:
0 CoreFoundation 0x000000010894f6fb __exceptionPreprocess + 331
1 libobjc.A.dylib 0x0000000107ef3ac5 objc_exception_throw + 48
2 UIKitCore 0x00000001119f9624 +[UIStoryboard storyboardWithName:bundle:] + 675
3 Runner 0x00000001060b09bd __57-[AppDelegate application:didFinishLaunchingWithOptions:]_block_invoke + 909
4 Flutter 0x000000010614c9a2 __45-[FlutterMethodChannel setMethodCallHandler:]_block_invoke + 115
5 Flutter 0x0000000106169616 _ZNK5shell21PlatformMessageRou<…>
经过 24 小时的努力,我是这样解决问题的。我唯一尝试的是 "Used Try Catch Block",错误是我没有使用导致空指针异常的正确标识符名称。
这是代码:
@try {
UIStoryboard *sb = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
MultipleMarkersVC *vc = [sb instantiateViewControllerWithIdentifier:@"MultipleMarkersVC"];
UINavigationController *navCon = [[UINavigationController alloc] initWithRootViewController:vc];
[self.window.rootViewController presentViewController:navCon animated:YES completion:nil];
}
@catch (NSException * e) {
NSLog(@"Exception: %@", e);
}
加载情节提要时,无需在其名称中添加扩展名“.storyboard”。
所以,请使用以下内容:
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
关于您的最后一行,如果您从 [AppDelegate didFinishLaunchingWithOptions] 加载视图,我更愿意使用以下行:
[self.window setRootViewController: wc]
我正在尝试从 AppDelegate didViewLoadFinished 函数导航,但我无法成功,因为我对 ios 完全陌生。我正在为 ViewControllers 使用故事板。
我还检查了其他类似的解决方案:Swift - pushViewController from appDelegate, rootViewController.navigationController is nil 但这并没有解决我的问题。
这是我在故事板上的 ViewController:
这是我尝试加载新 ViewController "MultipleMarkersViewController" 的方式:
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main.storyboard" bundle:nil];
MultipleMarkersViewController *wc=[storyboard instantiateViewControllerWithIdentifier:@"MultipleMarkersView"];
[self.window.rootViewController.navigationController pushViewController:wc animated:YES];
预期结果是加载新的ViewController。 但实际结果是 "The App crashes" 并具有以下日志输出:
0 CoreFoundation 0x000000010894f6fb __exceptionPreprocess + 331
1 libobjc.A.dylib 0x0000000107ef3ac5 objc_exception_throw + 48
2 UIKitCore 0x00000001119f9624 +[UIStoryboard storyboardWithName:bundle:] + 675
3 Runner 0x00000001060b09bd __57-[AppDelegate application:didFinishLaunchingWithOptions:]_block_invoke + 909
4 Flutter 0x000000010614c9a2 __45-[FlutterMethodChannel setMethodCallHandler:]_block_invoke + 115
5 Flutter 0x0000000106169616 _ZNK5shell21PlatformMessageRou<…>
经过 24 小时的努力,我是这样解决问题的。我唯一尝试的是 "Used Try Catch Block",错误是我没有使用导致空指针异常的正确标识符名称。 这是代码:
@try {
UIStoryboard *sb = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
MultipleMarkersVC *vc = [sb instantiateViewControllerWithIdentifier:@"MultipleMarkersVC"];
UINavigationController *navCon = [[UINavigationController alloc] initWithRootViewController:vc];
[self.window.rootViewController presentViewController:navCon animated:YES completion:nil];
}
@catch (NSException * e) {
NSLog(@"Exception: %@", e);
}
加载情节提要时,无需在其名称中添加扩展名“.storyboard”。 所以,请使用以下内容:
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
关于您的最后一行,如果您从 [AppDelegate didFinishLaunchingWithOptions] 加载视图,我更愿意使用以下行:
[self.window setRootViewController: wc]