2017-11-08 11 views
0

저는 storyboard에서 MainViewController를 1 개, xib에서 1 개의 ModalUIView를 보유하고 있습니다.Swift4 ViewController 하위보기의 이벤트가 실행되지 않았습니다.

ModalUIView에는 모달을 닫기위한 모달 및 닫기 기능을 표시하는 기능이 있습니다.

단계 : MainViewController -> OpenModal -> ModalUIView -> 다음

내 코드입니다 CloseModal :

UIViewUtil.swift

import Foundation 
import UIKit 

extension UIView { 
    // Load xib as the same name of CustomView that want to use xib 
    func loadXib() -> UIView{ 
     let bundle = Bundle(for: type(of: self)) 
     let nibName = type(of: self).description().components(separatedBy: ".").last! 
     let nib = UINib(nibName: nibName, bundle: bundle) 
     return nib.instantiate(withOwner: self, options: nil).first as! UIView 
    } 
} 

MainViewController.swift

의 UIViewController의 서브 클래스
@IBAction func guideButton(_ sender: Any) { 
    let modal = ModalUIView() 
    modal.present(targetView: self.view) 
} 

ModalUIVi ew.swift 내가 modalUIView의 존재라고 할 때

var view : UIView? 

override init(frame: CGRect) { 
    super.init(frame: frame) 

    setup() 
} 

required init?(coder aDecoder: NSCoder) 
{ 
    super.init(coder: aDecoder) 

    setup() 
} 

func setup() { 
    view = loadXib() 
} 

func present(targetView: UIView) { 
    view!.layer.cornerRadius = 10.0 
    view!.clipsToBounds = true 

    targetView.addSubview(view!) 

    // Set size 
    let popupWidth: CGFloat = targetView.frame.width - (targetView.frame.width * 0.04) 
    let popupHeight: CGFloat = targetView.frame.height - (targetView.frame.height * 0.08) 

    view!.frame = CGRect(x: targetView.frame.origin.x, y: targetView.frame.origin.y, 
         width: popupWidth, height: popupHeight) 

    view!.center = targetView.center 

    view!.transform = CGAffineTransform.init(scaleX: 1.3, y: 1.3) 
    view!.alpha = 0 

    UIView.animate(withDuration: 0.4){ 
     self.view!.alpha = 1 
     self.view!.transform = CGAffineTransform.identity 
    } 
} 

@objc func dismiss(sender: UIButton!) { 
    print("dismiss") 
} 

내 문제가 mainViewController입니다 UIView의 하위 클래스이며, 그때 modalUIView에 closeButton으로에 탭

내가 @IBAction으로 시도 작업을 해고하지 않고 작동하지 :

@IBAction func CloseButtonAction(_ sender: UIButton) { 
} 

나는 또한 프로그램에 의해 수동 추가 조치를 시도했지만 너무 작동하지 :

let closeButton: UIButton? = view?.viewWithTag(10) as! UIButton 
closeButton!.addTarget(self, action: #selector(dismiss), for: .touchUpInside) 

참고 : 모달에서 closeButton을보고 탭할 수 있습니다. 이미 ModalUIView를 xib 파일 소유자에게 추가합니다.

답변

0

그래, 지금 해결책이 있지만 그게 최선의 대답인지는 모르겠다.

내 솔루션은 사람이 아닌 다른 최선의 해결책이 있다면 저를 제안 해주십시오하지 modalUIView

에 mainViewController를 통해 modalUIView의 closeButton으로에 액션을 추가하는 것입니다.

0

다음과 같이 viewcontroller에서 조치를 취할 수도 있습니다.

yourSubView.button.addTarget(self, action: #selector(buttonPress(sender:)), for: .touchUpInside) 

그리고 메서드가 호출됩니다.

func buttonPress(sender:UIButton){ 
print("Button pressed") 
}