简体   繁体   English

快速调用LoadView函数两次

[英]LoadView function called twice in swift

I have a problem: I add all buttons and other outlets manually in the loadView() function. 我有一个问题:我在loadView()函数中手动添加了所有按钮和其他插座。 Due to the big amount of buttons I add them in the loadView() function to an array. 由于大量的按钮,我将它们在loadView()函数中添加到数组中。 But apparently the loadView() function is called twice so that I don't have 10 objects in my array but 20. Can anyone explain to me why the function is called twice and what I have to do now? 但是很显然, loadView()函数被调用了两次,所以我的数组中没有10个对象,而是20个。有人可以向我解释为什么调用该函数两次以及现在要做的事情吗?

My Code: 我的代码:

private let myCreator = CreatorClass()
private var loadGameSlots = [UIButton]()

override func loadView() {
    UIApplication.sharedApplication().statusBarHidden = true

    for var i=0;i<5;i++ {
        var stringD:String
        var stringE:String
        if (data.delegate.allFileNames.count == 0) || (data.delegate.allFileNames.count == i+1) {
            stringD = "Nichts gespeichert"
            stringE = "Nothing saved"
        }
        else {
            stringD = data.delegate.allFileNames[i] as String
            stringE = data.delegate.allFileNames[i] as String
        }
        let myButton:UIButton = UIButton.buttonWithType(UIButtonType.Custom) as UIButton
        self.myCreator.createButton(myButton, frameForIPhone5: CGRect(x: 45, y: 195, width: 230, height: 25), titleD: stringD, titleE: stringE, bold: false, size: 18.0, titleColor: UIColor.whiteColor(), highlightedColor: UIColor.whiteColor(), shadowColor: nil, offset: 0, backgroundImage: "StartingPageButton.png", backgroundColor: UIColor.clearColor(), action: "loadGameSlotTouched:", target: self, viewToAdd: self.view)
        self.loadGameSlots += [myButton]
        myButton.hidden = true
    }
}

The .createButton method is kind of a custom class: .createButton方法是一个自定义类:

func createButton(button:UIButton!,var frameForIPhone5:CGRect?,titleD:String?,titleE:String?,bold:Bool?,var size:CGFloat,titleColor:UIColor?,highlightedColor:UIColor?,shadowColor:UIColor?,var offset:CGFloat?,backgroundImage:String?,backgroundColor:UIColor,action:Selector,target:AnyObject?,viewToAdd:UIView?) {

    func adjustSizes () {
        let constantY:CGFloat = viewRect.height / 568.0
        let constantX:CGFloat = viewRect.width / 320.0
        frameForIPhone5! = CGRect(x: frameForIPhone5!.origin.x * constantX, y: frameForIPhone5!.origin.y * constantY, width: frameForIPhone5!.width * constantX, height: frameForIPhone5!.height * constantY)
        size *= constantY
        offset! *= constantY
    }
    adjustSizes()
    button.frame = frameForIPhone5!
    if myData.delegate.selectedLanguage == "Deutsch" {
        button.setTitle(titleD!, forState: UIControlState.Normal)
    }
    else {
        button.setTitle(titleE!, forState: UIControlState.Normal)
    }
    if bold! {
        button.titleLabel!.font = UIFont(name: "Futura-CondensedExtraBold", size: size)
    }
    else {
        button.titleLabel!.font = UIFont(name: "Futura-Medium", size: size)
    }
    button.setTitleColor(titleColor?, forState: UIControlState.Normal)
    button.setTitleColor(highlightedColor?, forState: UIControlState.Highlighted)
    button.titleLabel!.shadowColor = shadowColor?
    button.titleLabel!.shadowOffset = CGSize(width: offset!, height: offset!)
    button.setBackgroundImage(UIImage(named: backgroundImage!), forState: UIControlState.Normal)
    button.backgroundColor = backgroundColor
    button.addTarget(target, action: action, forControlEvents: UIControlEvents.TouchUpInside)
    viewToAdd?.addSubview(button)
}

It's a lot of code now, but I don't know what you need to answer :) 现在有很多代码,但是我不知道您需要回答什么:)

PS: I didn't write anything in the viewDidLoad() function PS:我没有在viewDidLoad()函数中写任何东西

Output: 输出:

(lldb) bt
* thread #1: tid = 0x66195, 0x0000000102bebbbd Airline Star Swift`Airline_Star_Swift.StartingPageViewController.loadView (self=0x00007fcffbe26120)() -> () + 10413 at StartingPageViewController.swift:51, queue = 'com.apple.main-thread', stop reason = breakpoint 1.1
  * frame #0: 0x0000000102bebbbd Airline Star Swift`Airline_Star_Swift.StartingPageViewController.loadView (self=0x00007fcffbe26120)() -> () + 10413 at StartingPageViewController.swift:51
    frame #1: 0x0000000102bec4f2 Airline Star Swift`@objc Airline_Star_Swift.StartingPageViewController.loadView (Airline_Star_Swift.StartingPageViewController)() -> () + 34 at StartingPageViewController.swift:0
    frame #2: 0x0000000103798ef9 UIKit`-[UIViewController loadViewIfRequired] + 75
    frame #3: 0x000000010379938e UIKit`-[UIViewController view] + 27
    frame #4: 0x00000001036c3964 UIKit`-[UIWindow handleStatusBarChangeFromHeight:toHeight:] + 948
    frame #5: 0x00000001036c6308 UIKit`+[UIWindow _noteStatusBarHeightChanged:oldHeight:forAutolayoutRootViewsOnly:] + 235
    frame #6: 0x0000000103676a2e UIKit`__79-[UIApplication _setStatusBarHidden:animationParameters:changeApplicationFlag:]_block_invoke + 141
    frame #7: 0x00000001036e5c42 UIKit`+[UIView(UIViewAnimationWithBlocks) _setupAnimationWithDuration:delay:view:options:factory:animations:start:animationStateGenerator:completion:] + 473
    frame #8: 0x00000001036e5ed8 UIKit`+[UIView(UIViewAnimationWithBlocks) animateWithDuration:animations:completion:] + 59
    frame #9: 0x0000000103676901 UIKit`-[UIApplication _setStatusBarHidden:animationParameters:changeApplicationFlag:] + 490
    frame #10: 0x0000000103676cf2 UIKit`-[UIApplication setStatusBarHidden:withAnimation:] + 126
    frame #11: 0x0000000102be93a8 Airline Star Swift`Airline_Star_Swift.StartingPageViewController.loadView (self=0x00007fcffbe26120)() -> () + 152 at StartingPageViewController.swift:18
    frame #12: 0x0000000102bec4f2 Airline Star Swift`@objc Airline_Star_Swift.StartingPageViewController.loadView (Airline_Star_Swift.StartingPageViewController)() -> () + 34 at StartingPageViewController.swift:0
    frame #13: 0x0000000103798ef9 UIKit`-[UIViewController loadViewIfRequired] + 75
    frame #14: 0x000000010379938e UIKit`-[UIViewController view] + 27
    frame #15: 0x00000001036b8db9 UIKit`-[UIWindow addRootViewControllerViewIfPossible] + 58
    frame #16: 0x00000001036b9152 UIKit`-[UIWindow _setHidden:forced:] + 276
    frame #17: 0x00000001036c565c UIKit`-[UIWindow makeKeyAndVisible] + 42
    frame #18: 0x0000000103670191 UIKit`-[UIApplication _callInitializationDelegatesForMainScene:transitionContext:] + 2628
    frame #19: 0x0000000103672e5c UIKit`-[UIApplication _runWithMainScene:transitionContext:completion:] + 1350
    frame #20: 0x0000000103671d22 UIKit`-[UIApplication workspaceDidEndTransaction:] + 179
    frame #21: 0x00000001064c82a3 FrontBoardServices`__31-[FBSSerialQueue performAsync:]_block_invoke + 16
    frame #22: 0x0000000102d1fabc CoreFoundation`__CFRUNLOOP_IS_CALLING_OUT_TO_A_BLOCK__ + 12
    frame #23: 0x0000000102d15805 CoreFoundation`__CFRunLoopDoBlocks + 341
    frame #24: 0x0000000102d155c5 CoreFoundation`__CFRunLoopRun + 2389
    frame #25: 0x0000000102d14a06 CoreFoundation`CFRunLoopRunSpecific + 470
    frame #26: 0x0000000103671799 UIKit`-[UIApplication _run] + 413
    frame #27: 0x0000000103674550 UIKit`UIApplicationMain + 1282
    frame #28: 0x0000000102be6ede Airline Star Swift`top_level_code + 78 at AppDelegate.swift:13
    frame #29: 0x0000000102be6f1a Airline Star Swift`main + 42 at AppDelegate.swift:0
    frame #30: 0x0000000105107145 libdyld.dylib`start + 1
(lldb) 

And after the second breakpoint: 在第二个断点之后:

(lldb) bt
* thread #1: tid = 0x66195, 0x0000000102bebbbd Airline Star Swift`Airline_Star_Swift.StartingPageViewController.loadView (self=0x00007fcffbe26120)() -> () + 10413 at StartingPageViewController.swift:51, queue = 'com.apple.main-thread', stop reason = breakpoint 1.1
  * frame #0: 0x0000000102bebbbd Airline Star Swift`Airline_Star_Swift.StartingPageViewController.loadView (self=0x00007fcffbe26120)() -> () + 10413 at StartingPageViewController.swift:51
    frame #1: 0x0000000102bec4f2 Airline Star Swift`@objc Airline_Star_Swift.StartingPageViewController.loadView (Airline_Star_Swift.StartingPageViewController)() -> () + 34 at StartingPageViewController.swift:0
    frame #2: 0x0000000103798ef9 UIKit`-[UIViewController loadViewIfRequired] + 75
    frame #3: 0x000000010379938e UIKit`-[UIViewController view] + 27
    frame #4: 0x00000001036b8db9 UIKit`-[UIWindow addRootViewControllerViewIfPossible] + 58
    frame #5: 0x00000001036b9152 UIKit`-[UIWindow _setHidden:forced:] + 276
    frame #6: 0x00000001036c565c UIKit`-[UIWindow makeKeyAndVisible] + 42
    frame #7: 0x0000000103670191 UIKit`-[UIApplication _callInitializationDelegatesForMainScene:transitionContext:] + 2628
    frame #8: 0x0000000103672e5c UIKit`-[UIApplication _runWithMainScene:transitionContext:completion:] + 1350
    frame #9: 0x0000000103671d22 UIKit`-[UIApplication workspaceDidEndTransaction:] + 179
    frame #10: 0x00000001064c82a3 FrontBoardServices`__31-[FBSSerialQueue performAsync:]_block_invoke + 16
    frame #11: 0x0000000102d1fabc CoreFoundation`__CFRUNLOOP_IS_CALLING_OUT_TO_A_BLOCK__ + 12
    frame #12: 0x0000000102d15805 CoreFoundation`__CFRunLoopDoBlocks + 341
    frame #13: 0x0000000102d155c5 CoreFoundation`__CFRunLoopRun + 2389
    frame #14: 0x0000000102d14a06 CoreFoundation`CFRunLoopRunSpecific + 470
    frame #15: 0x0000000103671799 UIKit`-[UIApplication _run] + 413
    frame #16: 0x0000000103674550 UIKit`UIApplicationMain + 1282
    frame #17: 0x0000000102be6ede Airline Star Swift`top_level_code + 78 at AppDelegate.swift:13
    frame #18: 0x0000000102be6f1a Airline Star Swift`main + 42 at AppDelegate.swift:0
    frame #19: 0x0000000105107145 libdyld.dylib`start + 1

I'm confused because I did the exactly same thing in Objective-C but it worked perfectly 我很困惑,因为我在Objective-C中做了完全相同的事情,但是效果很好

Thanks in advance! 提前致谢! :) :)

The problem is this line: 问题是这一行:

 self.myCreator.createButton(myButton, frameForIPhone5: CGRect(x: 45, y: 195, width: 230, height: 25), titleD: stringD, titleE: stringE, bold: false, size: 18.0, titleColor: UIColor.whiteColor(), highlightedColor: UIColor.whiteColor(), shadowColor: nil, offset: 0, backgroundImage: "StartingPageButton.png", backgroundColor: UIColor.clearColor(), action: "loadGameSlotTouched:", target: self, viewToAdd: self.view)

You are mentioning self.view inside loadView . 您在loadView中提到了self.view But inside loadView there is no self.view ; 但是在loadView内部没有self.view ; its job, in fact, is to create one, but you are not creating it. 实际上,它的工作是创建一个,但是您不是在创建它。 That's circular and causes loadView to be called twice. 这是循环的,并导致loadView被调用两次。

The ultimate source of the problem is that you should never have implemented loadView in the first place. 问题的最终根源是,您永远不应该首先实现loadView Perhaps when you did this in Objective-C you used viewDidLoad , but now you have accidentally changed it to loadView and this is messing you up. 也许当您在Objective-C中执行此操作时,您使用了viewDidLoad ,但是现在您不小心将其更改为loadView ,这使您感到困惑。

loadView is wrong place to create your entire view hierarchy. loadView是创建整个视图层次结构的错误位置。 You should only create top view (which is self.view) in loadView. 您只应在loadView中创建顶视图(即self.view)。 First create self.view in loadView then do other stuff in viewDidLoad. 首先在loadView中创建self.view,然后在viewDidLoad中执行其他操作。

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

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