gpt4 book ai didi

ios - 在 swift 中使用延迟调用数组项 - 获取 UIResponder 错误

转载 作者:行者123 更新时间:2023-11-28 08:38:28 26 4
gpt4 key购买 nike

四处搜索似乎并没有提供我立即理解的任何内容。

我正在尝试为一个应用程序制作一个愚蠢的“old skool”加载屏幕,它将打印出“类似 DOS”的语句。

我当前的代码看起来像这样,但我收到了 UIResponder、UIApplicationDelegate 错误 - EXE_BAD_ACCESS

我试过将其复制到新项目等。仍然出现相同的错误。

代码如下:

class ViewController: UIViewController {

@IBOutlet weak var loadingLBL: UILabel!

@IBOutlet weak var logoIMG: UIImageView!

override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
//fades logo image in
logoIMG.alpha = 0
UIView.animateWithDuration(5, animations: {
self.logoIMG.alpha = 1;
})

//array for loading text

var loading = ["Loading please wait.......\n", "Registry , OK!\n", `"Available ram, 256k\n", "BIOS Load... OK\n", "Welcome" ]`

var i = 0
var str: String = ""
var timer = NSTimer()

//delay function for loading array text

func delayFunc() {
str += "\(loading[i])\n"
loadingLBL.text = str
if i == loading.count - 1 {
timer.invalidate()
}
i += 1
}

timer = NSTimer.scheduledTimerWithTimeInterval(1, target: self, selector: Selector(delayFunc()), userInfo: nil, repeats: true)
}

override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}

正如我提到的,我遇到了崩溃,但我不知道自己做错了什么。

如有任何帮助,我们将不胜感激。

最佳答案

我认为你的问题是 delayFunc() 在 vi​​ewDidLoad 中,选择器在 viewController 中查找但找不到函数(因为它在 viewDidLoad 中)

编辑:我不是 100% 清楚为什么选择器在 viewDidLoad 中找不到 delayFunc。如果您在另一个函数的主体中声明一个函数,它的生命周期至多是其“父”函数的生命周期。因此,当您的计时器尝试调用 delayFunc 时,delayFunc 不再存在,因为 viewDidLoad 已完成运行。

尝试:

var i = 0
var str: String = ""
var timer = NSTimer()

var loading = ["Loading please wait.......\n", "Registry , OK!\n", "Available ram, 256k\n", "BIOS Load... OK\n", "Welcome" ]

override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
//fades logo image in
logoIMG.alpha = 0
UIView.animateWithDuration(5, animations: {
self.logoIMG.alpha = 1;
})

timer = NSTimer.scheduledTimerWithTimeInterval(1, target: self, selector: "delayFunc", userInfo: nil, repeats: true)
}

func delayFunc() {
str += "\(loading[i])\n"
loadingLBL.text = str
if i == loading.count - 1 {
timer.invalidate()
}
i += 1
}

override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}

关于ios - 在 swift 中使用延迟调用数组项 - 获取 UIResponder 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37013121/

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