Presenting view controllers on detached view controllers is discouraged

本文探讨了解决iOS开发中Presentingviewcontrollers警告的方法。该警告通常出现在从一个未托管的视图控制器中调用present操作时。文章提供了一种有效方案,即通过addChildViewController替代直接添加视图控制器。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

控制台警告:Presenting view controllers on detached view 

                     controllers is discouraged



》》登录过后present到一个tab管理的视图控制器,在这里面oneVC又addObject一个视图控制器twoVC。当从oneVC present到 twoVC的时候这个警告就出现了。并且dismiss后返回到oneVC时里面的tableView的y值发生改变。


》》分析原因是present一个视图后再present出另一个视图就会出现这样的警告

(在网上查找的资料解决方法: [self.view.window.rootViewControllerpresentViewController:controller
animated:YEScompletion:nil];)


》》

最终解决方法:

原先是通过[self.controlArray addObject:homeWork];这样添加present会出警告

再通过addChildViewController一次就不会出现警告了   [self addChildViewController:homeWork];



》》避免类似的警告

一般来说我们呈现一个viewcontroller 里面的内容,经常采用的方式是用navigationcontroller与tabbarcontroller来管理视图控制器。在这种情况下,呈现的视图控制器都在一个app维护的控制器堆栈里面。

而有一种情况就是通过self.view addsubview:AviewController.view也可以呈现出某个视图控制器。这种情况虽然看上去跟 push的结果没什么不同,但是实际上这个被呈现的view的controller并没有在当前app的视图堆栈里,也就是所谓的

detachviewcontroller,如果你在这个分离之外的视图控制器里面做present 调用,就会出现上述问题。

所以判定是否为分离视图,就是看它是不是处在当前app的控制器堆栈里面。

显然app的 rootviewcontroller 当然是处于堆栈里,并且是堆栈最底层的视图控制器。所以这种解决方案在一定程度上是可行的。


Presenting view controllers on detached view controllers is discouraged

P》》resenting view controllers on detached view controllers is discouraged

### ActivityView in iOS Development In iOS development, `UIActivityViewController` serves as a standard interface for presenting activity options to users. This component allows developers to integrate sharing functionalities within applications easily. The class provides built-in support for common activities such as posting content on social media platforms or sending emails. When implementing transitions between views involving an `UIActivityViewController`, one can utilize segues defined within storyboards. Whenever a user triggers a segue from the current scene, the storyboard runtime invokes the `prepare(for:sender:)` method of the present view controller[^1]. Within this overridden function, necessary information may be passed along with the transition process to prepare the upcoming screen adequately. For scenarios where direct instantiation without using segues is preferred, obtaining instances through storyboard identifiers becomes relevant. To achieve this goal, three main steps are involved: 1. Acquire the UIStoryboard object by specifying its name. 2. Retrieve the desired UIViewController subclass instance via its unique identifier (`myView`) set inside the storyboard file. 3. Present the newly created viewController modally over the existing context. ```swift // Swift code example demonstrating how to instantiate and display another ViewController programmatically if let destinationViewController = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "myView") as? MyViewController { self.present(destinationViewController, animated: true, completion: nil) } ``` Regarding deployment targets, it's important to note differences when working across devices versus simulators. Applications packaged into `.ipa` files cater specifically towards real-world usage on physical hardware while those compiled under simulator settings produce `.app` bundles suitable only for testing purposes during development phases[^3].
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值