1

我正在尝试像这样子类GMSAutocompleteViewControllerGooglePlacesSDK:

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) 
4

2 回答 2

1

您可能需要调用init 函数super.init()AddressFinderViewController确保GMSAutocompleteViewController已正确初始化。

于 2016-09-08T00:20:09.840 回答
0

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

这是我所做的:

我的 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 否则逻辑会在视图控制器的生命周期后期被覆盖。无论如何,这是我能找到的最适合我的解决方法。我的一部分想知道谷歌是否真的不希望你弄乱地址控制器,因为看起来他们会考虑到这一点。

于 2018-04-18T05:27:43.897 回答