我遇到了问题:我在loadView()
功能中手动添加了所有按钮和其他插座。由于按钮数量很大,我将loadView()
函数中的它们添加到数组中。但显然loadView()
函数被调用了两次,因此我的数组中没有10个对象,但是20。任何人都可以向我解释为什么函数被调用两次以及我现在必须做什么?
我的代码:
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
}
}
.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)
}
现在有很多代码,但我不知道你需要回答什么:)
P.S。:我没有在viewDidLoad()
函数
输出:
(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)
在第二个断点之后:
(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
我感到很困惑,因为我在Objective-C中做了完全相同的事情,但它完美地运作了
提前致谢! :)
答案 0 :(得分:1)
问题在于这一行:
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.view
内提及loadView
。但在loadView
内,没有self.view
;事实上,它的工作就是创造一个,但你不是在创造它。这是循环,导致loadView
被调用两次。
问题的最终根源是你永远不应该首先实施loadView
。也许当您在Objective-C中执行此操作时,您使用了viewDidLoad
,但现在您不小心将其更改为loadView
,这让您感到困扰。
答案 1 :(得分:-1)
loadView是创建整个视图层次结构的错误位置。您应该只在loadView中创建顶视图(self.view)。首先在loadView中创建self.view,然后在viewDidLoad中执行其他操作。