使用 UIStoryBoard 根据自定义 URL 方案处理屏幕
Handle screens based on Custom URL Scheme using UIStoryBoard
我有两个名为
的应用程序
- SendDataApp
- ReceiveDataApp
这是我的 ReceiveDataApp
故事板
我可以将数据发送到我的接收应用程序并可以通过以下方法处理它,
- (BOOL)application:(UIApplication *)app openURL:(NSURL *)url options:(NSDictionary<NSString *,id> *)options;
但是,这里的问题是我想将从 SendDataApp 接收到的数据显示到 ReceivedDataApp[=52= 的 DetailViewController
] 我正在尝试用下面的方法来处理它,
Appdelegate.m
- (BOOL)application:(UIApplication *)app openURL:(NSURL *)url options:(NSDictionary<NSString *,id> *)options {
UIStoryboard *storyBoard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
ViewController *viewController = (ViewController *)[storyBoard instantiateViewControllerWithIdentifier:@"View"];
[viewController receivedURL:url];
return YES;
}
ViewController.m
- (void)receivedURL:(NSURL *)url {
[self performSegueWithIdentifier:@"detailIdentifier" sender:url];
}
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([[segue identifier] isEqualToString:@"detailIdentifier"]) {
DetailViewController *detail = [segue destinationViewController];
detail.receivingURL = (NSURL *)sender;
}
}
但是,它给我一些错误
Terminating app due to uncaught exception 'NSGenericException', reason: 'Could not find a navigation controller for segue 'detailIdentifier'. Push segues can only be used when the source controller is managed by an instance of UINavigationController.
我的两个视图控制器仅嵌入 UINavigationController
。而且,通常我可以通过按钮操作查看详细信息页面。但是,不是通过 URL Scheme
这里有什么错误?而且,我在这里做错了什么吗?
给我一些建议或想法来处理这个问题。
您在 AppDelegate 的 openURL
方法中实例化您的 ViewController
,ARC 在 openURL
方法 returns 时立即释放视图控制器。它存活的时间不够长,无法添加到导航控制器中
您真正需要做的是获取当前显示的视图控制器(它可能是也可能不是您的“View Controller
”),然后对其执行 segue。
换句话说,在您的应用委托中:
- (BOOL)application:(UIApplication *)app openURL:(NSURL *)url options:(NSDictionary<NSString *,id> *)options {
UIViewController *rootvc = self.window.rootViewController;
// make sure the root view controller is the ViewController class you want
if ([rootvc isKindOfClass: [ViewController class]])
{
ViewController * vc = (ViewController *)rootvc;
[vc receivedURL:url];
}
return YES;
}
你可以看到更多我试图用答案做的事情 in this question and this question。
最后,你真的必须重命名你的子类视图控制器,使其远离超级通用"View Controller"和"Detail View Controller" 名字。
我有两个名为
的应用程序- SendDataApp
- ReceiveDataApp
这是我的 ReceiveDataApp
故事板我可以将数据发送到我的接收应用程序并可以通过以下方法处理它,
- (BOOL)application:(UIApplication *)app openURL:(NSURL *)url options:(NSDictionary<NSString *,id> *)options;
但是,这里的问题是我想将从 SendDataApp 接收到的数据显示到 ReceivedDataApp[=52= 的 DetailViewController
] 我正在尝试用下面的方法来处理它,
Appdelegate.m
- (BOOL)application:(UIApplication *)app openURL:(NSURL *)url options:(NSDictionary<NSString *,id> *)options {
UIStoryboard *storyBoard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
ViewController *viewController = (ViewController *)[storyBoard instantiateViewControllerWithIdentifier:@"View"];
[viewController receivedURL:url];
return YES;
}
ViewController.m
- (void)receivedURL:(NSURL *)url {
[self performSegueWithIdentifier:@"detailIdentifier" sender:url];
}
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([[segue identifier] isEqualToString:@"detailIdentifier"]) {
DetailViewController *detail = [segue destinationViewController];
detail.receivingURL = (NSURL *)sender;
}
}
但是,它给我一些错误
Terminating app due to uncaught exception 'NSGenericException', reason: 'Could not find a navigation controller for segue 'detailIdentifier'. Push segues can only be used when the source controller is managed by an instance of UINavigationController.
我的两个视图控制器仅嵌入 UINavigationController
。而且,通常我可以通过按钮操作查看详细信息页面。但是,不是通过 URL Scheme
这里有什么错误?而且,我在这里做错了什么吗?
给我一些建议或想法来处理这个问题。
您在 AppDelegate 的 openURL
方法中实例化您的 ViewController
,ARC 在 openURL
方法 returns 时立即释放视图控制器。它存活的时间不够长,无法添加到导航控制器中
您真正需要做的是获取当前显示的视图控制器(它可能是也可能不是您的“View Controller
”),然后对其执行 segue。
换句话说,在您的应用委托中:
- (BOOL)application:(UIApplication *)app openURL:(NSURL *)url options:(NSDictionary<NSString *,id> *)options {
UIViewController *rootvc = self.window.rootViewController;
// make sure the root view controller is the ViewController class you want
if ([rootvc isKindOfClass: [ViewController class]])
{
ViewController * vc = (ViewController *)rootvc;
[vc receivedURL:url];
}
return YES;
}
你可以看到更多我试图用答案做的事情 in this question and this question。
最后,你真的必须重命名你的子类视图控制器,使其远离超级通用"View Controller"和"Detail View Controller" 名字。