- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
当我尝试使用self.children[0].currentInfo = UpdatedInfo
将updatedInfo从父UIViewController传递到嵌入/子UIViewController时,出现了这个问题。事实证明,在父 View Controller 的“普通”函数中,这工作得很好。但是从父 View Controller 中的委托(delegate)函数内部,它使程序崩溃了。显然,有时 - 但并非总是 - self.children.isEmpty = true
在委托(delegate)函数内。
在不知道其原因的情况下,我不知道如何可靠地使用 self.children
来传递信息。但我不想仅仅因为我不理解它而避免使用它。任何见解将不胜感激。
由于我的应用程序引入了太多复杂的因素,我决定通过创建一个非常简单的“玩具”应用程序来检查问题。这是我所拥有的(代码位于 Storyboard屏幕截图下方):
在MainViewController文件中,有
import UIKit
class MainViewController: UIViewController, InfoDelegate {
var text = "Text from MainViewController"
override func viewDidLoad() {
super.viewDidLoad()
view.backgroundColor = UIColor.lightGray
printString("Text from MainViewController's viewDidLoad")
}
// Ordinary func
func printString(_ string: String) {
print(string)
print("In printString, self.children.isEmpty is \(self.children.isEmpty)")
}
// Delegate func
func messageString(_ string: String) {
print(string)
print("In messageString, self.children.isEmpty is \(self.children.isEmpty)")
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if let textViewController = segue.destination as? TextViewController {
textViewController.text = text
textViewController.delegate = self
}
}
}
在 TextViewController 中,有
import UIKit
protocol InfoDelegate: AnyObject { func messageString(_ string: String) }
class TextViewController: UIViewController {
@IBOutlet weak var textView: UITextView!
var text = "Text from TextViewController"
weak var delegate: InfoDelegate?
override func viewDidLoad() {
super.viewDidLoad()
textView.text = text
delegate?.messageString("Text from TextViewController's InfoDelegate")
}
}
最佳答案
在加载目标 View Controller 的 View 之前,会调用 prepare(for:sender:)
方法。它也可能在 subview Controller 安装到父 View Controller 的 children
数组之前被调用。 (我还没有测试过这一点,但它是有道理的。创建目的地后立即调用 prepare(for:sender:)
,以便您有机会进行早期设置。)
我建议将 didSet 添加到您 child 的 delegate
属性中并对其进行测试。将测试 amIAChildYet
方法添加到您的委托(delegate)协议(protocol)中,并在 subview Controller 的委托(delegate)属性的 didSet() 方法中调用它,以查看设置的顺序。
关于ios - 为什么带有 subview Controller 的 UIViewController 有时会返回children[0].isEmpty = true?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54146846/
我在自定义表格单元格中长按时添加 subview ,但是当我滚动表格时, subview 消失。 p> 我应该怎么做,以便当我向后滚动时,该 subview 仍然显示/出现在该单元格上,或者换句话说,
如何遍历 UIView 的所有 subview ,以及它们的 subview 和它们的 subview ? 最佳答案 使用递归: // UIView+HierarchyLogging.h @inter
如果我有 contentView 和 2 个 subview 。我想根据subviewB居中subviewA subviewA 和 subviewB 有相同的父 View 。 这就是我现在正在做的,将
我想向 self.view 添加一个覆盖整个区域的 subview (如调光 View )。但在它下面,有一些 View 我不希望受到添加的 subview 的影响(我不希望这些 View 具有变暗的
我正在尝试向 UITabBar 添加 subview ,它应该位于其他 UITabBar subview 之后。 我在 UITabBarController 的子类中添加了这样的 subview :
图像描述了我必须将这种 subview 添加到我现有的单元格中,并且在多次单击“添加”图标时还要添加相同的 subview 。 我在添加 subview 时遇到困难。如果有人能为我提供处理此结构的正确
我添加了一个 subview ,如下所示: func showPlayerView2() { self.view.frame = CGRect(x: 0, y: 0, width: view.
我的问题是关于 UIView/CALayer: Transform triggers layoutSubviews in superview 据报道,来自苹果的 TSI 回复: Generally,
我试图为我的主视图创建一个辅助 View 。这个 subview 需要很小的高度,它需要适合另一个带有标签的大 UIView。 问题是当我使用 UIView addSubview 时,第三个 UIVi
我正在尝试淡出整个 View ,但只有一个特定的 subview , 这将强调 subview更清晰的外观。假设 self.view 有 5 textfields和 2 UIView如subviews
我确信我在这里错过了一些愚蠢的东西,但我有 mainView、subviewA 和 subviewB。我试图将 subviewB 添加到 subviewA 并将其锚定在 subviewA 内部,但是它
我有一个包含 3 个 subview 的 View ,分别称为 view A、view B、view C。 当我删除 subview 时,意味着我需要自动设置边距以进行调整。 当我删除 view A
我有两个加载的 subview 。一个是位于 View Controller 内部的选项卡栏,它很早就加载,第二个是按下选项卡栏项目时出现的 View 。 但是,当添加此 subview 时,它会加载
在 iOS 应用中,我在主视图中添加了一个 subview : [self.view addSubview:firstUIImageSubview]; 但是在 subview 类 firstUIIma
我正在制作一个球程序,您可以通过长按手势将球 UIView subview 添加到 super View 。点击 View 会将其从父 View 中移除。 添加所有 View 效果很好,重新添加 Vi
我使用以下代码在单击按钮时将日期选择器 View 添加到我的应用程序主视图。 - (IBAction)CalenderButton_Click:(id)sender { // code to add
我正在努力向 View 添加 subview 。进度变量在其 View Controller 的 viewDidLoad() 中设置。 progressView frame size 设置正确,只是添
这是我的代码 var button = [UIButton?](count:3, repeatedValue: UIButton()) for i in 0...2{
我有一个 UIViewController,里面有几个不同的 UIView subview 。当用户调用某个 Action 时,我手动更改每个的大小、比例和位置。我想更新其中一个 UILabel( s
我的屏幕有一个主视图模型。它由 2 个 subview 模型组成。 其中一个负责注册部分。其中一个处理登录部分。其中一个负责处理菜单部分(如果经过身份验证,可以显示哪些菜单项,以及“欢迎“用户名”类型
我是一名优秀的程序员,十分优秀!