iOS swift,当在UIView中调用touchSend时,在故事板中显示ViewController或ViewController
我想在xib中调用Toucheded函数时演示ViewController,但我不知道如何实现它 这是我项目的一部分 //ViewController.swiftiOS swift,当在UIView中调用touchSend时,在故事板中显示ViewController或ViewController,ios,swift,uiview,uiviewcontroller,presentviewcontroller,Ios,Swift,Uiview,Uiviewcontroller,Presentviewcontroller,我想在xib中调用Toucheded函数时演示ViewController,但我不知道如何实现它 这是我项目的一部分 //ViewController.swift class ViewController: UIViewController { override func viewWillAppear(_ animated: Bool) { super.viewWillAppear(animated) // add custom xib view (d
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)
}
}
class secondeViewController.swift: UIViewController {
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
}
}
//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
}
}
只有视图控制器可以显示另一个视图控制器。 您可以使用委派模式来执行此操作 创建一个新协议
protocol PresentVCDelegate {
func presentVC()
}
在ScheduleView中添加属性
var presentVCDelegate: PresentVCDelegate
在ViewController中,遵守此协议并将视图的委托设置为self
//in view will appear
view.presentVCDelegate = self
别忘了遵守协议
extension ViewController: PresentVCDelegate {
func presentVC() {
//present your secondViewController here
}
}
最后,在ScheduleView中的touchEnd方法中,调用委托方法
self.presentVCDelegate.presentVC()
这就是我所做的解决方案
// 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)
}
}
//ViewController.swift
类ViewController:UIViewController{
覆盖函数视图将出现(uo动画:Bool){
超级。视图将显示(动画)
//添加自定义xib视图(日视图)
让view=Bundle.main.loadNibNamed(“ScheduleView”,所有者:self,选项:nil)?。首先是!ScheduleView
view.frame=self.view.frame
//在视图中将出现
view.presentvcdegate=self
self.view.addSubview(视图)
}
}
扩展视图控制器:PresentVCDelegate{
func presentVC(){
//在此处展示您的第二个ViewController
}
}
//ScheduleView.swift
协议呈现vcDelegate{
func presentVC()
}
类ScheduleView:UIView{
var presentVCDelegate:presentVCDelegate
/*
//仅当执行自定义绘图时才重写绘图()。
//空实现会对动画期间的性能产生不利影响。
重写函数绘图(rect:CGRect){
//绘图代码
}
*/
覆盖func TouchesBegind(Touchs:Set,带有事件:UIEvent?){
super.touchesbeated(touches,with:event)
打印(“触摸开始”)
}
覆盖函数touchesend(touchs:Set,带有事件:UIEvent?){
super.touchesend(触摸,带有:事件)
打印(“触摸发送”)
//把场景移到这里
//显示第二视图控制器
}
}
//secondeViewController.swift
类secondeViewController:UIViewController{
覆盖函数视图将出现(uo动画:Bool){
超级。视图将显示(动画)
}
}