【问题标题】:App crash when set title in view controller在视图控制器中设置标题时应用程序崩溃
【发布时间】:2019-03-08 09:09:08
【问题描述】:

我将LGSideMenuController 用于侧边菜单。一切正常。

但是当我设置title 的viewController 时,我的应用程序崩溃了。没有标题它工作正常。

我在didFinishWithLauchingOption 中有以下代码来配置导航栏。

if #available(iOS 11.0, *) {
    UINavigationBar.appearance(whenContainedInInstancesOf: [UIDocumentBrowserViewController.self]).tintColor = .menuColor
} else {
    UINavigationBar.appearance(whenContainedInInstancesOf: [UIDocumentPickerViewController.self]).tintColor = .menuColor
}

// Configure Navigation Bar
UINavigationBar.appearance().tintColor = .white
UINavigationBar.appearance().titleTextAttributes = [
    NSAttributedString.Key.foregroundColor : UIColor.menuColor,
    NSAttributedString.Key.font : UIFont.systemFontSize
]

UIColor 扩展

extension UIColor {
    // Menu Background Color
    class var menuColor: UIColor {
        get {
            return UIColor(red: 0, green: 105, blue: 255)
        }
    }

    convenience init(red: Int, green: Int, blue: Int) {
        assert(red >= 0 && red <= 255, "Invalid red component")
        assert(green >= 0 && green <= 255, "Invalid green component")
        assert(blue >= 0 && blue <= 255, "Invalid blue component")

        self.init(red: CGFloat(red) / 255.0, green: CGFloat(green) / 255.0, blue: CGFloat(blue) / 255.0, alpha: 1.0)
    }
}

我已使用情节提要配置LGSideMenuController,如下所示:

我正在尝试使用 DashboardViewController (DashboardVC) 中的以下代码,这导致我的应用程序崩溃:

self.title = "Dashboard"

并产生以下错误:

-[__NSCFNumber renderingMode]: unrecognized selector sent to instance 0xd9c845dd03d829d0
2019-03-08 12:50:37.552233+0530 AppName[5924:95152] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFNumber renderingMode]: unrecognized selector sent to instance 0xd9c845dd03d829d0'
*** First throw call stack:
(
0   CoreFoundation                      0x0000000108f711bb __exceptionPreprocess + 331
1   libobjc.A.dylib                     0x0000000108093735 objc_exception_throw + 48
2   CoreFoundation                      0x0000000108f8ff44 -[NSObject(NSObject) doesNotRecognizeSelector:] + 132
3   CoreFoundation                      0x0000000108f75ed6 ___forwarding___ + 1446
4   CoreFoundation                      0x0000000108f77da8 _CF_forwarding_prep_0 + 120
5   UIFoundation                        0x000000010e7154b6 __NSValidateCoreTextAttributes + 1098
6   UIFoundation                        0x000000010e711836 __NSStringDrawingEngine + 2830
7   UIFoundation                        0x000000010e714304 -[NSAttributedString(NSExtendedStringDrawing) boundingRectWithSize:options:context:] + 795
8   UIKitCore                           0x0000000111698a2f -[UILabel _textRectForBounds:limitedToNumberOfLines:includingShadow:] + 583
9   UIKitCore                           0x00000001116985a4 -[UILabel textRectForBounds:limitedToNumberOfLines:] + 50
10  UIKitCore                           0x000000011169d3f3 -[UILabel _intrinsicSizeWithinSize:] + 389
11  UIKitCore                           0x000000011169cb3c -[UILabel _ensureBaselineMetricsReturningBounds] + 197
12  UIKitCore                           0x000000011169ce9d -[UILabel _firstBaselineOffsetFromTop] + 169
13  UIKitCore                           0x00000001116788e5 -[UIView(AdditionalLayoutSupport) nsli_lowerAttribute:intoExpression:withCoefficient:forConstraint:onBehalfOfLayoutGuide:] + 1361
14  UIKitCore                           0x0000000111678385 -[UIView(AdditionalLayoutSupport) nsli_lowerAttribute:intoExpression:withCoefficient:forConstraint:] + 289
15  Foundation                          0x0000000107cb8ed3 -[NSLayoutConstraint _lowerIntoExpression:reportingConstantIsRounded:] + 96
16  Foundation                          0x0000000107cb8a0f -[NSLayoutConstraint _addToEngine:mutuallyExclusiveConstraints:] + 114
17  UIKitCore                           0x0000000111662bc7 -[UIView(UIConstraintBasedLayout) _layoutEngine_didAddLayoutConstraint:roundingAdjustment:mutuallyExclusiveConstraints:] + 447
18  UIKitCore                           0x0000000111663021 -[UIView(UIConstraintBasedLayout) _tryToAddConstraintWithoutUpdatingConstraintsArray:roundingAdjustment:mutuallyExclusiveConstraints:] + 30
19  UIKitCore                           0x00000001116636c4 -[UIView(UIConstraintBasedLayout) nsli_addConstraint:] + 38
20  Foundation                          0x0000000107cbaa92 __55+[NSLayoutConstraint _addOrRemoveConstraints:activate:]_block_invoke + 65
21  Foundation                          0x0000000107ca035a -[NSISEngine withBehaviors:performModifications:] + 110
22  Foundation                          0x0000000107cba96e +[NSLayoutConstraint _addOrRemoveConstraints:activate:] + 403
23  UIKitCore                           0x00000001109ee5e7 -[_UINavigationBarContentViewLayout _keepConstraintsActiveIfNecessary:updateLayout:] + 145
24  UIKitCore                           0x00000001109eec6c -[_UINavigationBarContentViewLayout setTitleView:] + 143
25  UIKitCore                           0x00000001109e7136 -[_UINavigationBarContentView _setupTitleViewAnimated:] + 1197
26  UIKit                               0x0000000129d030e9 -[_UINavigationBarContentViewAccessibility _setupTitleViewAnimated:] + 42
27  UIKitCore                           0x00000001109e8da1 -[_UINavigationBarContentView updateContentAnimated:] + 86
28  UIKitCore                           0x0000000110a1afeb -[_UINavigationBarVisualProviderModernIOS _updateContentForTopItem:backItem:animated:] + 1926
29  UIKitCore                           0x0000000110a1b638 -[_UINavigationBarVisualProviderModernIOS _updateContentForTopItem:backItem:] + 53
30  UIKitCore                           0x0000000110a1e255 -[_UINavigationBarVisualProviderModernIOS traitCollectionDidChange:] + 414
31  UIKitCore                           0x00000001109c7156 -[UINavigationBar traitCollectionDidChange:] + 529
32  UIKitCore                           0x0000000111713a47 -[UIView _wrappedProcessTraitCollectionDidChange:forceNotification:] + 137
33  UIKitCore                           0x0000000111713b54 -[UIView _wrappedProcessTraitCollectionDidChange:forceNotification:] + 406
34  UIKitCore                           0x0000000111713d5a -[UIView _processDidChangeRecursivelyFromOldTraits:toCurrentTraits:forceNotification:] + 129
35  UIKitCore                           0x000000011173e72d -[UIView(CALayerDelegate) layoutSublayersOfLayer:] + 1337
36  QuartzCore                          0x00000001072f7b19 -[CALayer layoutSublayers] + 175
37  QuartzCore                          0x00000001072fc9d3 _ZN2CA5Layer16layout_if_neededEPNS_11TransactionE + 395
38  QuartzCore                          0x00000001072757ca _ZN2CA7Context18commit_transactionEPNS_11TransactionE + 342
39  QuartzCore                          0x00000001072ac97e _ZN2CA11Transaction6commitEv + 576
40  UIKitCore                           0x000000011126f2d0 __34-[UIApplication _firstCommitBlock]_block_invoke_2 + 139
41  CoreFoundation                      0x0000000108ed662c __CFRUNLOOP_IS_CALLING_OUT_TO_A_BLOCK__ + 12
42  CoreFoundation                      0x0000000108ed5de0 __CFRunLoopDoBlocks + 336
43  CoreFoundation                      0x0000000108ed0654 __CFRunLoopRun + 1284
44  CoreFoundation                      0x0000000108ecfe11 CFRunLoopRunSpecific + 625
45  GraphicsServices                    0x000000010e3b11dd GSEventRunModal + 62
46  UIKitCore                           0x000000011125481d UIApplicationMain + 140
47  AppName                               0x00000001038ea857 main + 71
48  libdyld.dylib                       0x000000010a527575 start + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException

即使我也尝试从情节提要中设置标题,它也会使应用程序崩溃。

我已经关注了 SO 上可用的解决方案,但它对我不起作用。

【问题讨论】:

    标签: ios swift navigationcontroller


    【解决方案1】:

    不幸的是,问题似乎出在其他地方,可能很难找到这个问题。

    从您发布的错误来看,似乎在某处输入了一些不正确的类型。 -[__NSCFNumber renderingMode]: unrecognized selector sent to instance 表示基本上意味着有一个 NSNumber 应该是其他一些对象类型,并且系统正在尝试调用该类型不存在的方法 renderingMode。

    这些事情主要发生在 Objective-C 部分,一些框架仍然在内部使用 Objective-C。通过数组或字典输入不正确的类型是最容易的。从堆栈的其余部分开始,一个好的开始可能是搜索属性字符串。您可以看到接近尾声的标签被调用,随后是属性字符串 NSAttributedString。

    属性字符串在接收属性时不会检查类型,因此很容易以这种方式崩溃。请检查您的代码是否有可能与NSAttributedString 一起使用的任何内容,并注释掉属性以查看它是否修复了您的崩溃。如果是这种情况,那么只需修复属性,您输入的内容一定有错误。

    如果不是这种情况,它仍然可能是另一个使用NSAttributedString 的系统上的错误。也许您应该不鼓励使用某些库或开源?

    或者甚至外观也可能导致同样的问题。您是否在代码中的某处为导航栏设置了外观属性?

    【讨论】:

    • 是的,我在AppDelegate 的didFinishLaunchingWithOptions 中使用UINavigationBar.appearance(),请参阅我更新的问题。
    • @MayurKarmur 如果您删除它,它会修复崩溃吗?确定问题很重要。
    • @MayurKarmur NSAttributedString.Key.font : UIFont.systemFontSize 行似乎完全可以使应用程序崩溃。你确定字体应该是数字吗?请查看此元素的文档。
    • @MayurKarmur 然后我想我们找到了您的问题并解决了它。如果您对如何使用外观还有其他问题,我想最好再问一个问题。虽然这现在应该很容易正确实施。困难的部分是找出导致崩溃的原因。
    • 我遇到了类似的错误。检查文档并阅读您无法设置字体大小是有帮助的。 :(
    【解决方案2】:

    如果你直接展示 ViewController 而不是 NavigationController,标题将为 nil。 试试这个:

    self.navigationItem.title = "Dashboard"
    

    【讨论】:

    • 我没有展示视图控制器。而且我也尝试过上面的代码,但它抛出的错误与我发布的问题相同。
    【解决方案3】:

    LGsideMenuController 作为一个根控制器和两个左右视图控制器。

    所以请验证 SideMenuController 的根视图控制器是否添加了 NavigationController。

    Ex:- 
    
    UIViewController *rootViewController = [UIViewController new];
    UITableViewController *leftViewController = [UITableViewController new];
    UITableViewController *rightViewController = [UITableViewController new];
    
    UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:rootViewController];
    
    LGSideMenuController *sideMenuController = [LGSideMenuController sideMenuControllerWithRootViewController:navigationController
                                                                                           leftViewController:leftViewController
                                                                                          rightViewController:rightViewController];
    

    在示例控制器中添加了导航控制器。

    【讨论】:

      猜你喜欢
      • 2013-12-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-05-30
      • 2016-05-05
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多