简体   繁体   English

iOS Swift按钮显示延迟

[英]iOS Swift Button Display Delay

Please consider the following code: 请考虑以下代码:

override func viewDidLoad() {
    super.viewDidLoad()

    let images = [
        "Add To Inventory","Move To Display Room","Move To Store Room","Move To Storage","Scrap Inventory"
    ]

    var buttons:[UIButton] = []
    // For use in foreground
    let name=defaults.valueForKey("name") as! String
    tasksTitle.title = "Tasks for \(name)"
    var y=CGFloat(140)

    for var i=0;i<images.count;i++ {
        print(i)
        buttons.append(UIButton(type: .Custom))
        buttons[i].frame = CGRectMake(60,y,650,100)

        buttons[i].setImage(UIImage(named: "\(images[i]).png"), forState: .Normal)
        buttons[i].tag = i
        buttons[i].addTarget(self, action: "taskPressed:", forControlEvents: UIControlEvents.TouchUpInside)
        self.view.addSubview(buttons[i])
        y+=130
        print(y)
    }

My problem is that it takes about 20 seconds for the buttons to appear and when they do only the first (Add To Inventory) and the last (Scrap Inventory) actually appear. 我的问题是,显示按钮大约需要20秒钟,而当它们执行时,实际上仅显示第一个(添加到库存)和最后一个(废品库存)。 I have tried recreating the view and using normal buttons with the same results. 我尝试过重新创建视图并使用具有相同结果的普通按钮。 Does anyone have any idea as what my problem might be? 有谁知道我的问题可能是什么?

Are you using auto layout ? 您在使用自动布局吗? If you are, it may explain why you are not seeing all your buttons. 如果是这样,则可以解释为什么看不到所有按钮。 The other possibility is that some images are not included in your application bundle (which may cause the framework to spend time trying to find them elsewhere) or are very large. 另一种可能是某些图像未包含在您的应用程序捆绑包中(这可能会导致框架花费时间尝试在其他位置找到它们)或非常大。

Turns out the reason fro the delay was because of threading. 证明延迟的原因是线程。 When I enclosed my code in 当我将代码包含在其中时

dispatch_async(dispatch_get_main_queue()){
}

the buttons appeared immediately. 按钮立即出现。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM