如果没有viewController暴露,如何将cocoapod转换为打字稿?

时间:2017-03-27 22:55:12

标签: javascript objective-c typescript cocoapods nativescript

做了很好的"在ns"中使用objc库。由Nathan Walker撰写的关于egghead.io的教程,我想测试一下我的新技能。我在material-snackbar上启动了相同的转化过程。

这个pod似乎没有公开UIView或viewController本身(它在管理器中)。您如何建议我将其添加到appWindow.rootViewController

原始代码:

MDCSnackbarMessage *message = [[MDCSnackbarMessage alloc] init];
message.text = @"How much wood would a woodchuck chuck if a woodchuck could chuck wood?";
[MDCSnackbarManager showMessage:message];

我的打字稿:

constuctor(){
    this._message = MDCSnackbarMessage.alloc().init(); 
}

public showMessage(m:string){
    this._message.text = m;
    MDCSnackbarManager.showMessage(this._message)
}

有效! 但是当我从我的应用控制器调用它时,我的应用中没有显示任何内容。所以,我的第一个猜测是挖掘MDCSnackBarManager代码以找到设置容器的方法。 我找到了它:

(void)setPresentationHostView:(UIView *)hostView {
  NSAssert([NSThread isMainThread], @"setPresentationHostView must be called on main thread.");
  MDCSnackbarManagerInternal *manager = [MDCSnackbarManagerInternal sharedInstance];
  manager.presentationHostView = hostView;
}

所以,我尝试了这个

MDCSnackbarManager.setPresentationHostView(rootVC());
const rootVC = function(){
    let appWindow = UIApplication.sharedApplication.keyWindow;
    return appWindow.rootViewController;
}

它崩溃了下面的消息。如何确保将管理器/消息正确添加到根视图中?

*** JavaScript call stack:
    (
        0   UIApplicationMain@[native code]
        1   start@file:///app/tns_modules/tns-core-modules/application/application.js:251:26
        2   bootstrapApp@file:///app/tns_modules/nativescript-angular/platform-common.js:68:28
        3   bootstrapModule@file:///app/tns_modules/nativescript-angular/platform-common.js:56:26
        4   anonymous@file:///app/main.js:4:57
        5   evaluate@[native code]
        6   moduleEvaluation@:1:11
        7   @:7:50
        8   promiseReactionJob@:1:11
    )
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UINavigationControllerImpl bounds]: unrecognized selector sent to instance 0x7ff366920a00'
    *** First throw call stack:
    (
        0   CoreFoundation                      0x000000010e7f9d4b __exceptionPreprocess + 171
        1   libobjc.A.dylib                     0x000000010e25b21e objc_exception_throw + 48
        2   CoreFoundation                      0x000000010e869f04 -[NSObject(NSObject) doesNotRecognizeSelector:] + 132
        3   CoreFoundation                      0x000000010e77f005 ___forwarding___ + 1013
        4   CoreFoundation                      0x000000010e80ca08 __forwarding_prep_1___ + 120
        5   MaterialComponents                  0x000000010aced0d4 -[MDCSnackbarManagerInternal activateOverlay:] + 612
        6   MaterialComponents                  0x000000010acebeeb -[MDCSnackbarManagerInternal displaySnackbarViewForMessage:] + 1163
        7   MaterialComponents                  0x000000010aceba40 -[MDCSnackbarManagerInternal showNextMessageIfNecessaryMainThread] + 496
        8   MaterialComponents                  0x000000010aced917 -[MDCSnackbarManagerInternal showMessageMainThread:] + 487
        9   MaterialComponents                  0x000000010aceec34 __34+[MDCSnackbarManager showMessage:]_block_invoke + 84
        10  libdispatch.dylib                   0x000000010f4ff808 _dispatch_call_block_and_release + 12
        11  libdispatch.dylib                   0x000000010f52112e _dispatch_client_callout + 8
        12  libdispatch.dylib                   0x000000010f5084fb _dispatch_main_queue_callback_4CF + 1054
        13  CoreFoundation                      0x000000010e7bde49 __CFRUNLOOP_IS_SERVICING_THE_MAIN_DISPATCH_QUEUE__ + 9
        14  CoreFoundation                      0x000000010e78337d __CFRunLoopRun + 2205
        15  CoreFoundation                      0x000000010e782884 CFRunLoopRunSpecific + 420
        16  GraphicsServices                    0x0000000111619a6f GSEventRunModal + 161
        17  UIKit                               0x000000010c015c68 UIApplicationMain + 159
        18  NativeScript                        0x000000010b48797d ffi_call_unix64 + 85
        19  ???                                 0x000000012a5fdf70 0x0 + 5005893488
    )
Mar 27 16:50:04 PODMB0079 SpringBoard[5820]: [KeyboardArbiter] HW kbd: Failed to set (null) as keyboard focus
Mar 27 16:50:04 PODMB0079 com.apple.CoreSimulator.SimDevice.CB2CCBE6-20D1-4283-908C-2232692E7AC0.launchd_sim[5804] (UIKitApplication:org.nativescript.podtest[0x8461][7291]): Service exited due to Abort trap: 6

1 个答案:

答案 0 :(得分:1)

我将根viewController而不是视图传递给方法。相反,它应该是这样的:

MDCSnackbarManager.setPresentationHostView(rootVC().view); //duh
const rootVC = function(){
    let appWindow = UIApplication.sharedApplication.keyWindow;
    return appWindow.rootViewController;
}