在为UIViewController分配标题时会发生一些奇怪的事情

时间:2014-10-13 13:13:39

标签: ios objective-c swift

我有一些viewControllers,每个都由segues连接。 rootViewController连接到NavigationController。现在,在一个UIViewController中,我正在执行一个segue转到另一个这样的UIViewController并且它可以工作:

self.performSegueWithIdentifier("toLoginTypeActivity", sender: self)

在执行segue之后我被带到的UIViewController是:

import Foundation
import UIKit

class LoginTypeActivityViewController: UIViewController{

    override func viewDidLoad() {
        super.viewDidLoad()
        println("I am here.")
        //self.title = "Namaskar"         // Here seems to be something strange.
        println("I am here also.")
    }
}

但是,如果我在代码中注释self.title(标记为奇怪),则segue不起作用。虽然,所有的println都在工作。甚至,"我也在这里。"得到印刷。但是,segues没有得到执行。它给出了运行时错误。

问题应该是什么以及如何解决?我确信我已正确命名了标识符。

这是运行时错误:

2014-10-13 17:21:27.878 mobilepay[29387:1382271] -[Swift._NSContiguousString set]: unrecognized selector sent to instance 0x7b6b33d0
2014-10-13 17:21:27.881 mobilepay[29387:1382271] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[Swift._NSContiguousString set]: unrecognized selector sent to instance 0x7b6b33d0'
*** First throw call stack:
(
0   CoreFoundation                      0x00bb5df6 __exceptionPreprocess + 182
1   libobjc.A.dylib                     0x0083fa97 objc_exception_throw + 44
2   CoreFoundation                      0x00bbda75 -[NSObject(NSObject) doesNotRecognizeSelector:] + 277
3   CoreFoundation                      0x00b069c7 ___forwarding___ + 1047
4   CoreFoundation                      0x00b0658e _CF_forwarding_prep_0 + 14
5   UIFoundation                        0x067eeb91 __NSStringDrawingEngine + 29221
6   UIFoundation                        0x067e784d -[NSString(NSExtendedStringDrawing) drawWithRect:options:attributes:context:] + 171
7   UIKit                               0x013be151 -[UILabel _drawTextInRect:baselineCalculationOnly:] + 6626
8   UIKit                               0x013bbe30 -[UILabel drawTextInRect:] + 581
9   UIKit                               0x013be256 -[UILabel drawRect:] + 98
10  UIKit                               0x0123354b -[UIView(CALayerDelegate) drawLayer:inContext:] + 519
11  QuartzCore                          0x01077d51 -[CALayer drawInContext:] + 118
12  QuartzCore                          0x01077c87 _ZL16backing_callbackP9CGContextPv + 96
13  QuartzCore                          0x00f5c7ae CABackingStoreUpdate_ + 2788
14  QuartzCore                          0x01077c1f ___ZN2CA5Layer8display_Ev_block_invoke + 93
15  QuartzCore                          0x010ad406 x_blame_allocations + 15
16  QuartzCore                          0x01077a85 _ZN2CA5Layer8display_Ev + 1591
17  QuartzCore                          0x01077cd6 -[CALayer _display] + 33
18  QuartzCore                          0x01077446 _ZN2CA5Layer7displayEv + 142
19  QuartzCore                          0x01077cb0 -[CALayer display] + 33
20  QuartzCore                          0x0106bee6 _ZN2CA5Layer17display_if_neededEPNS_11TransactionE + 322
21  QuartzCore                          0x0106bf6c _ZN2CA5Layer28layout_and_display_if_neededEPNS_11TransactionE + 38
22  QuartzCore                          0x00fca676 _ZN2CA7Context18commit_transactionEPNS_11TransactionE + 284
23  QuartzCore                          0x00fcba3c _ZN2CA11Transaction6commitEv + 392
24  QuartzCore                          0x00fcc108 _ZN2CA11Transaction17observer_callbackEP19__CFRunLoopObservermPv + 92
25  CoreFoundation                      0x00ad8fbe __CFRUNLOOP_IS_CALLING_OUT_TO_AN_OBSERVER_CALLBACK_FUNCTION__ + 30
26  CoreFoundation                      0x00ad8f00 __CFRunLoopDoObservers + 400
27  CoreFoundation                      0x00ace93a __CFRunLoopRun + 1226
28  CoreFoundation                      0x00ace1ab CFRunLoopRunSpecific + 443
29  CoreFoundation                      0x00acdfdb CFRunLoopRunInMode + 123
30  GraphicsServices                    0x0317124f GSEventRunModal + 192
31  GraphicsServices                    0x0317108c GSEventRun + 104
32  UIKit                               0x011a8e16 UIApplicationMain + 1526
33  mobilepay                           0x001410de top_level_code + 78
34  mobilepay                           0x0014111b main + 43
35  libdyld.dylib                       0x03ad7ac9 start + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException

请注意,分配给self.title可以在其他ViewControllers中工作。

编辑:

这是更新的代码。

class LoginTypeActivityViewController: UIViewController{

    var delegate = UIApplication.sharedApplication().delegate as AppDelegate

    override func viewDidLoad() {
        super.viewDidLoad()
        println("viewDidLoad self.title = \(self.title)")
        println("viewDidLoad self.navigationItem.title = \(self.navigationItem.title)")
    }

    override func viewWillAppear(animated: Bool) {
        println("viewWillAppear self.title = \(self.title)")
        println("viewWillAppear self.navigationItem.title = \(self.navigationItem.title)")
    }

    override func viewDidAppear(animated: Bool) {
        println("viewDidAppear self.title = \(self.title)")
        println("viewDidAppear self.navigationItem.title = \(self.navigationItem.title)")
        self.navigationItem.title = "Koshish"
        println("printing in viewDidAppear after setting self.navigatioItem.title = \(self.navigationItem.title)")
}

}

这是输出:

viewDidLoad self.title = nil
viewDidLoad self.navigationItem.title = nil
viewWillAppear self.title = nil
viewWillAppear self.navigationItem.title = nil
viewDidAppear self.title = nil
viewDidAppear self.navigationItem.title = nil
printing in viewDidAppear after setting self.navigatioItem.title = Optional("Koshish")
2014-10-14 12:46:48.931 mobilepay[34547:1593230] -[Swift._NSContiguousString set]: unrecognized selector sent to instance 0x7a162090
2014-10-14 12:46:48.933 mobilepay[34547:1593230] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[Swift._NSContiguousString set]: unrecognized selector sent to instance 0x7a162090'
*** First throw call stack:
(
0   CoreFoundation                      0x00bd3df6 __exceptionPreprocess + 182
1   libobjc.A.dylib                     0x0085da97 objc_exception_throw + 44
2   CoreFoundation                      0x00bdba75 -[NSObject(NSObject) doesNotRecognizeSelector:] + 277
3   CoreFoundation                      0x00b249c7 ___forwarding___ + 1047
4   CoreFoundation                      0x00b2458e _CF_forwarding_prep_0 + 14
5   UIFoundation                        0x0680cb91 __NSStringDrawingEngine + 29221
6   UIFoundation                        0x0680584d -[NSString(NSExtendedStringDrawing) drawWithRect:options:attributes:context:] + 171
7   UIKit                               0x013dc151 -[UILabel _drawTextInRect:baselineCalculationOnly:] + 6626
8   UIKit                               0x013d9e30 -[UILabel drawTextInRect:] + 581
9   UIKit                               0x013dc256 -[UILabel drawRect:] + 98
10  UIKit                               0x0125154b -[UIView(CALayerDelegate) drawLayer:inContext:] + 519
11  QuartzCore                          0x01095d51 -[CALayer drawInContext:] + 118
12  QuartzCore                          0x01095c87 _ZL16backing_callbackP9CGContextPv + 96
13  QuartzCore                          0x00f7a7ae CABackingStoreUpdate_ + 2788
14  QuartzCore                          0x01095c1f ___ZN2CA5Layer8display_Ev_block_invoke + 93
15  QuartzCore                          0x010cb406 x_blame_allocations + 15
16  QuartzCore                          0x01095a85 _ZN2CA5Layer8display_Ev + 1591
17  QuartzCore                          0x01095cd6 -[CALayer _display] + 33
18  QuartzCore                          0x01095446 _ZN2CA5Layer7displayEv + 142
19  QuartzCore                          0x01095cb0 -[CALayer display] + 33
20  QuartzCore                          0x01089ee6 _ZN2CA5Layer17display_if_neededEPNS_11TransactionE + 322
21  QuartzCore                          0x01089f6c _ZN2CA5Layer28layout_and_display_if_neededEPNS_11TransactionE + 38
22  QuartzCore                          0x00fe8676 _ZN2CA7Context18commit_transactionEPNS_11TransactionE + 284
23  QuartzCore                          0x00fe9a3c _ZN2CA11Transaction6commitEv + 392
24  QuartzCore                          0x00fea108 _ZN2CA11Transaction17observer_callbackEP19__CFRunLoopObservermPv + 92
25  CoreFoundation                      0x00af6fbe __CFRUNLOOP_IS_CALLING_OUT_TO_AN_OBSERVER_CALLBACK_FUNCTION__ + 30
26  CoreFoundation                      0x00af6f00 __CFRunLoopDoObservers + 400
27  CoreFoundation                      0x00aec93a __CFRunLoopRun + 1226
28  CoreFoundation                      0x00aec1ab CFRunLoopRunSpecific + 443
29  CoreFoundation                      0x00aebfdb CFRunLoopRunInMode + 123
30  GraphicsServices                    0x0318f24f GSEventRunModal + 192
31  GraphicsServices                    0x0318f08c GSEventRun + 104
32  UIKit                               0x011c6e16 UIApplicationMain + 1526
33  mobilepay                           0x00160f5e top_level_code + 78
34  mobilepay                           0x00160f9b main + 43
35  libdyld.dylib                       0x03af5ac9 start + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException

解决

我终于在我的代码中发现了问题:

在我的didFinishLaunchingWithOptions中,我这样做:

var navigationBarAppearance = UINavigationBar.appearance()
UIApplication.sharedApplication().statusBarStyle = UIStatusBarStyle.LightContent
navigationBarAppearance.tintColor = UIColor(rgba: "#000000", alpha: 1.0)
navigationBarAppearance.barTintColor = UIColor(rgba: "#000000", alpha: 1.0)
navigationBarAppearance.titleTextAttributes = [NSFontAttributeName: UIFont(name: "Arial-BoldMT", size: 20.0), NSForegroundColorAttributeName: "#000000", alpha: 1.0)]

正如你所看到的,在NSForegroundColorAttributeName中,我只是设置字符串而不是用它来调用UIColor函数。

我觉得Swift应该给我一些暗示,我没有用正确的值调用NSForegroundColorAttribute。

1 个答案:

答案 0 :(得分:0)

我不能给你答案,但对如何解决它有建议。不知何故,你的UINavigationItem的title属性被设置为某个对象而不是NSString。

所以,你有#34; // self.title"行,记录标题的值,你的班级的价值' UINavigationItem标题,如果其中任何一个不是nil,则记录该类。我的猜测它以某种方式被设置为一个不是字符串的数字,但它可能只是垃圾或释放的东西。或者,您的故事板文件可能已损坏。

编辑:所以这里真的很奇怪。其他想法:

  • 如果在演示视图的viewDidLoad之前触发了这个segue,可能会发生奇怪的事情。

  • 实现'覆盖func prepareForSegue(segue:UIStoryboardSegue,sender:AnyObject?)'在调用类中,然后验证您是否在主线程

  • 使用调试器,添加一个异常断点,当你得到崩溃时,验证崩溃是在线程0上。另外,捅一下 - 有一个对象没有响应选择器,你有它的地址,所以做一个' po 0x .......'在调试器中看看它是什么。

  • 如果做不到这一点,你需要花一些时间并尝试创建一个以真实模型为模型的演示项目,它有同样的问题。如果你能做到这一点,你可以将它上传到像Dropbox这样的公共网站,更新你的问题,像我这样的人会看它(我至少会这样做)。它是传统的,如果你达到这一点提供一些赏金,我只是说,所以你知道,但我会看到它没有一个(我不需要更多的点)。