在我的AppDelegate中,如果用户在我的自定义url方案中键入,我会显示一个ViewController。它就像我的ViewController一样工作。但是,我需要检测我的ViewController是否是从应用程序代理推送的。做到这一点的最佳方法或行动方案是什么?我只想检测它是否来自AppDelegate,而不想检测其他地方。
- (BOOL)application:(UIApplication *)application
openURL:(NSURL *)url
sourceApplication:(NSString *)sourceApplication
annotation:(id)annotation {
// attempt to extract a token from the url
ViewController *controller = [[ViewController alloc] initWithNibName:@"ViewController" bundle:nil];
[self.navigationController pushViewController:controller animated:YES];
编辑:为了澄清我想做得更好,当appdelegate呈现我的视图控制器时,我需要能够在我的视图控件内检测到它是由于appdelegate内的方法而出现的。有点像这个
ViewController
-(void) ViewDidLoad{
if (this controller was presented from App delegate){
do this
}
else{
do nothing
}
简单地这样做:
- (BOOL)application:(UIApplication *)application
openURL:(NSURL *)url
sourceApplication:(NSString *)sourceApplication
annotation:(id)annotation {
ViewController *controller = [[ViewController alloc] initWithNibName:@"ViewController" bundle:nil];
[self.navigationController pushViewController:controller animated:YES];
// here you know that your controller is pushed from the AppDelegate, then you can do other things just after the push
[controller callControllerPublicMethod];
}
当你把它推入另一个控制器时,什么都不要做。
为什么不在ViewController
中编写自己的自定义初始值设定项方法,当从应用程序委托调用时,将参数设置为true,而在其他任何地方将其设置为false。