0

Here a piece of code:

import UIKit
import RxCocoa
import RxSwift
import FTPopOverMenu_Swift

public let popoverShowLists: PublishSubject<([List], UIEvent?)> = PublishSubject<([List], UIEvent?)>()

private func setupPopoverShowList() {
    popoverShowLists
        .subscribe(onNext: { lists, event in
            if let event = event {
                let cellConfi = FTCellConfiguration()
                cellConfi.textFont = UIFont.systemFont(ofSize: 18)
                var cellConfis = Array(repeating: cellConfi, count: lists.count)
                FTPopOverMenu.showForEvent(event: event, with: lists.map{ $0.name }, menuImageArray: nil, cellConfigurationArray: cellConfis, done: { selectedIndex in
                    print(selectedIndex)
                }, cancel: {})
            }
        }).disposed(by: disposeBag)
}

public func getListOfLists(event: UIEvent?) {
    print(event) // First Print event
    URLRequest.loadRequestGET(resource: APIManager.getAllLists, tokenUser: User.token!)
        .subscribe(onNext: { [weak self] response, result, error in
            print(event) // Second Print event
            switch response.statusCode {
            case HTTPStatusCodes.OK.rawValue:
                if let result = result {
                    self?.popoverShowLists.onNext((result.list, event))
                }
            case HTTPStatusCodes.BadRequest.rawValue:
                if let error = error { print("[-] Error[\(error.opcode)] \(error.message)") }
            default:
                break
            }
        }).disposed(by: disposeBag)
}

@IBAction func selectListButtonAction(_ sender: UIBarButtonItem, event: UIEvent) {
    getListOfLists(event: event)
}

I'm trying to display a popover window with the content of my request's response, which is a simple String list. The FTPopoverMenu needs the event in parameter. Unfortunately I lose the value of the event after the execution of my request, so my popover window doesn't show up.

First Print of event:

Optional(<UITouchesEvent: 0x600002b8c6c0> timestamp: 196495 touches: {(
    <UITouch: 0x7f8401715f10> phase: Ended tap count: 1 force: 0.000 window: <UIWindow: 0x7f8401516b60; frame = (0 0; 1024 768); gestureRecognizers = <NSArray: 0x6000017e0d50>; layer = <UIWindowLayer: 0x6000019ec580>> view: <_UIButtonBarButton: 0x7f8401617120; frame = (0 0; 49 44); tintColor = UIExtendedSRGBColorSpace 0.156794 0.261571 0.36564 1; layer = <CALayer: 0x6000019d7a60>> location in window: {31.5, 43} previous location in window: {31.5, 43} location in view: {19.5, 20} previous location in view: {19.5, 20}
)})

Second Print of event:

Optional(<UITouchesEvent: 0x600002b8c6c0> timestamp: 196495 touches: {(
)})

How can I solve this ? Thank you :)

  • Please tell us what the desired outcome is. It's difficult for me to tell what you even mean. UIEvents aren't intended to be used that way. Maybe if you explain your needs we can think of an alternative. – cookednick May 20 '19 at 20:19
  • I updated the post and added my code. – Jeremy Thiriez May 29 '19 at 07:19
  • I see. I still can't really tell what the event's data is used for in this code, but the reason you can't access the touches anymore is because they've left memory. And there's no way AFAIK to copy a UITouch or init a new one with similar data. That data is in private variables which Apple will reject your app for using. Try to find a way to extract the data you need (like a touch's coordinates) at the moment the event happens (within selectListButtonAction) and then just pass THAT data onto the rest. – cookednick May 29 '19 at 15:18

0 Answers0