How to push a JSQMessagesViewController correctly

2019-04-29 04:58发布

问题:

I have a Swift project and am trying to use JSQMessagesViewController within it. I have used cocoapods to install the framework and am importing it using a swift import statement.

import JSQMessagesViewController

Up to this point there are no errors and everything compiles without warning. However when I try to push a new instance onto a navigation controller the app crashes.

func openConversation(userId: Int) {
    let messageViewController = JSQMessagesViewController();
    self.navigationController?.pushViewController(messageViewController, animated: true)
}

The second line of the method causes the crash and the error message is.

2015-08-03 21:44:17.229 [4856:64097] *** Assertion failure in -[JSQMessagesViewController viewWillAppear:], /[my file path]/Pods/JSQMessagesViewController/JSQMessagesViewController/Controllers/JSQMessagesViewController.m:223
2015-08-03 21:44:17.232 [4856:64097] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Invalid parameter not satisfying: self.senderId != nil'
*** First throw call stack:
(
    0   CoreFoundation                      0x00000001067e3c65 __exceptionPreprocess + 165
    1   libobjc.A.dylib                     0x0000000108590bb7 objc_exception_throw + 45
    2   CoreFoundation                      0x00000001067e3aca +[NSException raise:format:arguments:] + 106
    3   Foundation                          0x0000000106ec298f -[NSAssertionHandler handleFailureInMethod:object:file:lineNumber:description:] + 195
    4   JSQMessagesViewController           0x00000001060e9055 -[JSQMessagesViewController viewWillAppear:] + 277
    5   UIKit                               0x00000001073f8f61 -[UIViewController _setViewAppearState:isAnimating:] + 487
    6   UIKit                               0x000000010741a355 -[UINavigationController _startCustomTransition:] + 887
    7   UIKit                               0x000000010742637f -[UINavigationController _startDeferredTransitionIfNeeded:] + 386
    8   UIKit                               0x0000000107426ece -[UINavigationController __viewWillLayoutSubviews] + 43
    9   UIKit                               0x00000001075716d5 -[UILayoutContainerView layoutSubviews] + 202
    10  UIKit                               0x00000001073449eb -[UIView(CALayerDelegate) layoutSublayersOfLayer:] + 536
    11  QuartzCore                          0x0000000108f5ded2 -[CALayer layoutSublayers] + 146
    12  QuartzCore                          0x0000000108f526e6 _ZN2CA5Layer16layout_if_neededEPNS_11TransactionE + 380
    13  QuartzCore                          0x0000000108f52556 _ZN2CA5Layer28layout_and_display_if_neededEPNS_11TransactionE + 24
    14  QuartzCore                          0x0000000108ebe86e _ZN2CA7Context18commit_transactionEPNS_11TransactionE + 242
    15  QuartzCore                          0x0000000108ebfa22 _ZN2CA11Transaction6commitEv + 462
    16  QuartzCore                          0x0000000108ec00d3 _ZN2CA11Transaction17observer_callbackEP19__CFRunLoopObservermPv + 89
    17  CoreFoundation                      0x0000000106716ca7 __CFRUNLOOP_IS_CALLING_OUT_TO_AN_OBSERVER_CALLBACK_FUNCTION__ + 23
    18  CoreFoundation                      0x0000000106716c00 __CFRunLoopDoObservers + 368
    19  CoreFoundation                      0x000000010670ca33 __CFRunLoopRun + 1123
    20  CoreFoundation                      0x000000010670c366 CFRunLoopRunSpecific + 470
    21  GraphicsServices                    0x000000010b148a3e GSEventRunModal + 161
    22  UIKit                               0x00000001072c48c0 UIApplicationMain + 1282
    23  Startana                            0x0000000105fa2807 main + 135
    24  libdyld.dylib                       0x0000000109306145 start + 1
    25  ???                                 0x0000000000000001 0x0 + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException

After searching online I cant seem to find any information about what causes this issue.

回答1:

I’m glad to hear it solved your issue!

The JSQMessagesViewController have two asset parameters, the senderId and the senderDisplayName. So you must set your senderId and display name.



回答2:

In your current controller which is inherited by JSQMessagesViewController

you will need to do couple of things first add setup function

func setup() {
    self.senderId = "1234"
    self.senderDisplayName = "TEST"
}

and latter invoke that function from

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.
    self.setup()
}