Ios 如何检测是否从AppDelegate推送了ViewController?
在我的AppDelegate中,如果用户在我的自定义url方案中键入内容,我将提供一个ViewController。它作为我的视图控制器工作。但是,我需要检测我的ViewController是否是从App delegate推送的。这样做的最佳方法或行动方案是什么?我只想检测它是否来自AppDelegate,而不是其他地方Ios 如何检测是否从AppDelegate推送了ViewController?,ios,iphone,uiviewcontroller,appdelegate,Ios,Iphone,Uiviewcontroller,Appdelegate,在我的AppDelegate中,如果用户在我的自定义url方案中键入内容,我将提供一个ViewController。它作为我的视图控制器工作。但是,我需要检测我的ViewController是否是从App delegate推送的。这样做的最佳方法或行动方案是什么?我只想检测它是否来自AppDelegate,而不是其他地方 - (BOOL)application:(UIApplication *)application openURL:(NSURL *)url
- (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显示我的viewcontroller时,我需要能够在我的viewcontroller中检测到它是因为appdelegate中的方法而出现的。有点像这样
视图控制器
-(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。您应该更详细地解释主要问题。你想解决什么问题?你为什么关心它是从哪里来的?你是否有可能从其他地方推送此控制器?App Delegate永远不会神奇地删除你的控制器,因此如果它被推送(我假设你说“推送”时的意思是“被驳回”),那么你的代码就完成了,你可以看到这一点。如果你所说的“推送”是指某个东西出现在它前面,那么如果这是从你的应用程序委托中发生的,那就是你在做它。如果您所说的“推送”是指其他一些代码在您的视图控制器中显示另一个视图控制器,那么您可以始终实现视图将消失
,但我不知道在这种情况下您所说的“仅从AppDelegate”是什么意思。