2017-11-02 3 views
1

xib에서 touchedEnded 함수가 호출 될 때 presentViewController를 표시하고 싶습니다. 그러나 구현 방법을 모릅니다.iOS swift, touchesEnded가 UIView에서 호출 될 때 presencetViewController Storyboard의 다른 xib 또는 ViewController

내 프로젝트의 일부입니다. // secondeViewController.swift

class secondeViewController.swift: UIViewController { 

    override func viewWillAppear(_ animated: Bool) { 
     super.viewWillAppear(animated) 

    } 


} 

답변

1

// ViewController.swift

class ViewController: UIViewController { 

    override func viewWillAppear(_ animated: Bool) { 
     super.viewWillAppear(animated) 

     // add custom xib view (day view) 
     let view = Bundle.main.loadNibNamed("ScheduleView", owner: self, options: nil)?.first as! ScheduleView 
     view.frame = self.view.frame 

     self.view.addSubview(view) 
    } 


} 

// ScheduleView.swift

class ScheduleView.swift: UIView { 

    /* 
    // Only override draw() if you perform custom drawing. 
    // An empty implementation adversely affects performance during animation. 
    override func draw(_ rect: CGRect) { 
     // Drawing code 
    } 
    */ 

    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) { 
     super.touchesBegan(touches, with: event) 

     print("touchesBegan") 
    } 
    override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) { 
     super.touchesEnded(touches, with: event) 

     print("touchesEnd") 

     // move scene in here 
     // show secondViewController 
    } 



} 

전용 컨트롤러는 다른 뷰 컨트롤러를 제공 할 수 볼. 위임 패턴을 사용하여 위임 패턴을 사용할 수 있습니다.

이 프로토콜을 준수, 당신의 ViewController에서 ScheduleView에

var presentVCDelegate: PresentVCDelegate 

을 속성을 추가 새로운 프로토콜

protocol PresentVCDelegate { 
    func presentVC() 
} 

를 만들고 자체로보기의 대리자를 설정

//in view will appear 
view.presentVCDelegate = self 

돈 프로토콜을 준수하는 것을 잊지 마십시오

extension ViewController: PresentVCDelegate { 
    func presentVC() { 
     //present your secondViewController here 
    } 
} 

마지막 ScheduleView 안에 당신의 touchEnd 방법에 위임 방법

self.presentVCDelegate.presentVC() 
+0

감사합니다. 그것은 작동합니다. – user3525889

0

이것은 내가 할 솔루션을 호출합니다.

// ViewController.swift 

class ViewController: UIViewController { 

    override func viewWillAppear(_ animated: Bool) { 
     super.viewWillAppear(animated) 

     // add custom xib view (day view) 
     let view = Bundle.main.loadNibNamed("ScheduleView", owner: self, options: nil)?.first as! ScheduleView 
     view.frame = self.view.frame 

     //in view will appear 
     view.presentVCDelegate = self 

     self.view.addSubview(view) 
    } 


} 

extension ViewController: PresentVCDelegate { 
    func presentVC() { 
     //present your secondViewController here 
    } 
} 

// ScheduleView.swift 

protocol PresentVCDelegate { 
    func presentVC() 
} 

class ScheduleView: UIView { 
    var presentVCDelegate: PresentVCDelegate 
    /* 
    // Only override draw() if you perform custom drawing. 
    // An empty implementation adversely affects performance during animation. 
    override func draw(_ rect: CGRect) { 
     // Drawing code 
    } 
    */ 

    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) { 
     super.touchesBegan(touches, with: event) 

     print("touchesBegan") 
    } 
    override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) { 
     super.touchesEnded(touches, with: event) 

     print("touchesEnd") 

     // move scene in here 
     // show secondViewController 

    } 



} 

// secondeViewController.swift 

class secondeViewController: UIViewController { 

    override func viewWillAppear(_ animated: Bool) { 
     super.viewWillAppear(animated) 

    } 


} 
관련 문제