子类GMSAutocompleteViewController与Eureka一起使用

时间:2016-09-07 15:29:18

标签: ios swift google-maps google-places-api eureka-forms

我正在尝试将GMSAutocompleteViewController形式GooglePlaces SDK子类化为:

class AddressFinderViewController: GMSAutocompleteViewController, TypedRowControllerType {

public var row: RowOf<String>!
public var completionCallback : ((UIViewController) -> ())?



convenience public init(_ callback: (UIViewController) -> ()){
    self.init(nibName: nil, bundle: nil)
    completionCallback = callback
}



override func viewDidLoad() {
    super.viewDidLoad()

      }
  }

然后我用它来称呼它:

AddressFinderViewController(){ _ in }
            }, completionCallback: { vc in
                vc.navigationController?.popViewControllerAnimated(true)
        }

但是我收到了这个错误:

2016-09-07 17:21:19.445 PriumCity[77058:3790134] *** Terminating app 

due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[__NSArrayM insertObject:atIndex:]: object cannot be nil'
*** First throw call stack:
(
    0   CoreFoundation                      0x000000010e129d85 __exceptionPreprocess + 165
    1   libobjc.A.dylib                     0x000000010ede3deb objc_exception_throw + 48
    2   CoreFoundation                      0x000000010dfeacc5 -[__NSArrayM insertObject:atIndex:] + 901
    3   UIKit                               0x000000010c863cec -[UIViewController _addChildViewController:performHierarchyCheck:notifyWillMove:] + 541
    4   UIKit                               0x000000010c885087 -[UIViewController(UIContainerViewControllerProtectedMethods) addChildViewController:] + 83
    5   PriumCity                           0x000000010ac0bc9c -[GMSAutocompleteViewController viewWillAppear:] + 301
    6   UIKit                               0x000000010c8652bd -[UIViewController _setViewAppearState:isAnimating:] + 710
    7   UIKit                               0x000000010c865958 -[UIViewController __viewWillAppear:] + 149
    8   UIKit                               0x000000010c895750 -[UINavigationController _startCustomTransition:] + 1203
    9   UIKit                               0x000000010c8a5b9b -[UINavigationController _startDeferredTransitionIfNeeded:] + 712
    10  UIKit                               0x000000010c8a6d0b -[UINavigationController __viewWillLayoutSubviews] + 57
    11  UIKit                               0x000000010ca55503 -[UILayoutContainerView layoutSubviews] + 248
    12  UIKit                               0x000000010c77f980 -[UIView(CALayerDelegate) layoutSublayersOfLayer:] + 703
    13  QuartzCore                          0x000000010c474c00 -[CALayer layoutSublayers] + 146
    14  QuartzCore                          0x000000010c46908e _ZN2CA5Layer16layout_if_neededEPNS_11TransactionE + 366
    15  QuartzCore                          0x000000010c468f0c _ZN2CA5Layer28layout_and_display_if_neededEPNS_11TransactionE + 24
    16  QuartzCore                          0x000000010c45d3c9 _ZN2CA7Context18commit_transactionEPNS_11TransactionE + 277
    17  QuartzCore                          0x000000010c48b086 _ZN2CA11Transaction6commitEv + 486
    18  UIKit                               0x000000010c6f119b _afterCACommitHandler + 174
    19  CoreFoundation                      0x000000010e04ec37 __CFRUNLOOP_IS_CALLING_OUT_TO_AN_OBSERVER_CALLBACK_FUNCTION__ + 23
    20  CoreFoundation                      0x000000010e04eba7 __CFRunLoopDoObservers + 391
    21  CoreFoundation                      0x000000010e0447fb __CFRunLoopRun + 1147
    22  CoreFoundation                      0x000000010e0440f8 CFRunLoopRunSpecific + 488
    23  GraphicsServices                    0x00000001139c0ad2 GSEventRunModal + 161
    24  UIKit                               0x000000010c6c4f09 UIApplicationMain + 171
    25  PriumCity                           0x000000010aa99482 main + 114
    26  libdyld.dylib                       0x000000010f8b292d start + 1
    27  ???                                 0x0000000000000001 0x0 + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException
(lldb) 

2 个答案:

答案 0 :(得分:1)

您可能需要在 #input-field { background-color: rgba(255,255,255,0.4); border-radius: 5px; width: 100px; height: 40px; padding: 5px; } 的初始化函数中调用super.init(),以确保AddressFinderViewController已正确初始化。

答案 1 :(得分:0)

看来,出于某种原因,您无法为Google的GMSAutocompleteViewController创建自定义初始化。如果您尝试创建自定义init,那么它可能会显示您的子类视图控制器,但它只是一个黑色视图控制器。

这是我做的:

我的GMSAutocompleteViewController子类:

 import UIKit
import GooglePlaces

class AddressSearchViewController: GMSAutocompleteViewController {
    var state: RiderFormRow.Kind = .pickUp

    override func viewDidLoad() {
        super.viewDidLoad()
        setSearchBounds()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    private func setSearchBounds() {
        //Bloomington, IN coordinates
        let northEastCorner = CLLocationCoordinate2D(latitude: 39.20512, longitude: -86.435595)
        let southwestCorner = CLLocationCoordinate2D(latitude: 39.09969, longitude: -86.641937)
        autocompleteBounds = GMSCoordinateBounds(coordinate: northEastCorner, coordinate: southwestCorner)
        autocompleteBoundsMode = .restrict
    }
}

然后,这就是我呈现我的子视图控制器的方式:

let autocompleteController = AddressSearchViewController()
autocompleteController.state = row.type
autocompleteController.delegate = self
present(autocompleteController, animated: true, completion: nil)

因此,如果您需要将任何特殊变量传递到此子类变量中,则必须在init之后执行此操作。它不是一个完美的解决方案,因为能够拥有自定义init会很好,但不幸的是GMSAutocompeleteViewController需要调用init(nibName,bundle)才能正常工作。这可能是因为Google的框架正在为GMSAutocompeleteViewController使用故事板。您甚至无法将逻辑放在子类的init函数中。它必须放在ViewDidLoad中,否则逻辑会在视图控制器的生命周期中被覆盖。无论如何,这是我能找到的最适合我的解决方法。我的一部分想知道谷歌是否真的不希望你搞乱地址控制器,因为看起来他们会想到这个。