gpt4 book ai didi

ios - 无法创建class的实例,导致应用崩溃

转载 作者:行者123 更新时间:2023-12-01 21:23:35 24 4
gpt4 key购买 nike

解决方案-信贷Sanzio Angeli,用过lazy var尝试访问pageIndex以更新页面指示器,我尝试创建一个具有pageIndex的public属性的类的实例,但是当我尝试这样做会使应用程序崩溃时,请问有人可以建议在哪里我做错了,如果我不全局声明实例,但在方法内部,奇怪的是,应用程序不会崩溃
我得到的错误-Thread 1: EXC_BAD_ACCESS (code=2, address=0x7ffeeebccea8)下面是我要创建其实例的类,之后是我要通过在顶部创建全局实例来调用它的类,我正尝试在func moveToNext()中使用它

import UIKit

class ContentViewController: UIViewController {
let contentDesign = ContentView()
var pageIndex = 0
var pageHeading = ""
var pageContent = ""
var pageImage = ""
override func viewDidLoad() {
super.viewDidLoad()
view.addSubview(contentDesign)
contentDesign.pagerContent.text = pageContent
contentDesign.pagerHeader.text = pageHeading
contentDesign.pagerImage.image = UIImage(named: pageImage)
// Do any additional setup after loading the view.
}

}
////// ---------
class MasterView: UIViewController {

var container = UIView()
var lowerCotainer = UIView()
var pageNumbering: UIPageControl = UIPageControl()
var nextButton = UIButton()
var skipButton = UIButton()
var pageController = PageViewController()

**var content = ContentViewController()**

override func viewDidLoad() {
super.viewDidLoad()

commonInit()
}

func commonInit()
{
container.translatesAutoresizingMaskIntoConstraints = false
view.addSubview(container)
container.widthAnchor.constraint(equalToConstant: UIScreen.main.bounds.width).isActive = true
container.heightAnchor.constraint(equalToConstant: UIScreen.main.bounds.height * 0.6).isActive = true

}

@objc func moveToNext() {
// let index = pageController.currentIndex
let index = content.pageIndex
pageNumbering.currentPage = index + 1
print(index)
switch index {
case 0...1:

pageController.forwardPage()


case 2:
self.view.window?.rootViewController?.dismiss(animated: true, completion: nil)

default:
break

}
}

}
var pagerHeader = UILabel()的另一个类中,应用程序崩溃的地方很奇怪
import UIKit

class ContentView: UIView {

var pagerImage = UIImageView()
var pagerHeader = UILabel()
var pagerContent = UILabel()
let master = MasterView()
override init(frame: CGRect) {
super.init(frame: frame)
commonInit()
}

required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}

func commonInit()
{
pagerImage.translatesAutoresizingMaskIntoConstraints = false
addSubview(pagerImage)

}

}
enter image description here

最佳答案

在使用多重宇宙之后:

lazy var content = ContentViewController()
代替:
var content = ContentViewController()
已解决问题

关于ios - 无法创建class的实例,导致应用崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63627821/

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