gpt4 book ai didi

ios - objective-c - 从 appDelegate 检测当前 View

转载 作者:行者123 更新时间:2023-11-29 12:38:09 25 4
gpt4 key购买 nike

我正在使用模态转场(没有导航 Controller )在 viewController A 和 viewController B 之间移动,如下所示:

viewA *first = [self.storyboard instantiateViewControllerWithIdentifier:@"viewA"];
[self presentViewController:first animated:YES completion:nil];

然后向后移动:

 [self dismissViewControllerAnimated:YES completion:nil]; 

现在,我想从 AppDelegate 知道当前 View 是 A 还是 B。问题是当我检查

[(AppDelegate *)[[UIApplication sharedApplication] delegate] window]

答案总是 View A - 第一个。

每次我使用模态转场时,我都尝试设置当前 View :

viewA *first = [self.storyboard instantiateViewControllerWithIdentifier:@"viewA"];
[self presentViewController:first animated:YES completion:^
{
[[(AppDelegate *)[[UIApplication sharedApplication] delegate] window] setRootViewController:first];
}];

但它会导致一些错误(比如无法使用“dismissViewControllerAnimated”),并且不可能在具有许多转场的大型项目中的每个转场中都这样工作。

我应该如何处理它?我应该如何以更合适的方式检测当前 View ?

最佳答案

如回答here

UIWindow *topWindow = [[[UIApplication sharedApplication].windows sortedArrayUsingComparator:^NSComparisonResult(UIWindow *win1, UIWindow *win2) {
return win1.windowLevel - win2.windowLevel;
}] lastObject];

UIView *topView = [[topWindow subviews] lastObject];

但是,在这里执行此逻辑听起来像是糟糕的架构。您需要知道 AppDelegate 中当前显示的是哪个 View 的原因是什么?

编辑 您似乎想从 View Controller 响应 applicationWillResignActive 事件。在你的游戏 View Controller 中使用类似这样的东西。

- (void) applicationWillResign {
NSLog(@"About to lose focus");
}

-(void) viewDidLoad {
[super viewDidLoad];
[[NSNotificationCenter defaultCenter]
addObserver:self
selector:@selector(applicationWillResign)
name:UIApplicationWillResignActiveNotification
object:NULL];
}

- (void)viewDidDisappear:(BOOL)animated {
[super viewDidDisappear:animated];
[[NSNotificationCenter defaultCenter]
removeObserver:self];
}

关于ios - objective-c - 从 appDelegate 检测当前 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25713281/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com