如何迅速3

问题描述:

使用performSegue从UIView类我有一个UIView类,它包含一个UILabel。我加入了一个UILabelUITapGestureRecognizer想做performSegueUILabel的水龙头打开一个新的UIViewController。 的问题是,我不能在UIView类使用performSegue如何迅速3

谁能请帮助UIView类使用performSegue

我加了点触手势,以我的标签为,

nameLabel.isUserInteractionEnabled = true 

let tap = UITapGestureRecognizer(target: self, action: #selector(tapFunction)) 
tap.numberOfTapsRequired = 1 
tap.numberOfTouchesRequired = 1 
tap.isEnabled = true 
nameLabel.addGestureRecognizer(tap) 
我tapFunction

现在,

func tapFunction(sender:UITapGestureRecognizer) { 
    print("tap working") 
    self.performSegue(withIdentifier: "memberSegue", sender: self) 
} 

但我得到一个错误:

"Value of type UIViewController has no member performSegue"

,因为它是一个UIView类。

+0

您可以显示您当前正在尝试的代码。 –

+0

请分享代码UIView类 –

你应该尝试从UIViews分离的任何功能。最佳做法是您的UIViewControllers负责您的应用中的任何功能,并且您的UIViews仅用于显示元素。因此,我建议您在UIView中设置nameLabel属性,以便在实例化UIView后,您可以从UIViewController访问该属性。然后在你的UIViewController的代码应该是这样的:

let yourView = new YourView() 
let tap = UITapGestureRecognizer(target: self, action: #selector(tapFunction)) 
tap.numberOfTapsRequired = 1 
tap.numberOfTouchesRequired = 1 
tap.isEnabled = true 
yourView.nameLabel.addGestureRecognizer(tap) 

,你将有你的tap()功能在你的UIViewController

+0

它工作:)谢谢。 – manini

我建议你对那个抽头的姿态,而不是SEGUE添加操作。

TapRecognizer.addTarget(self, action: #selector(YourFunc)) 

Samle FUNC:

func YourFunc(){     
     let storyBoardHome = UIStoryboard(name: "", bundle: nil)   
    let memberController = storyBoardHome.instantiateViewController(withIdentifier: "") 
      self.navigationController?.pushViewController(memberController, animated: true) 
       } 

推荐本:https://*.com/a/26366366/6818278

+0

我已经为轻击手势添加了一个动作。 – manini

+0

但我无法使用navigationController,因为它是一个UIView类 – manini

+0

@manidev你需要向我们展示你正在尝试的代码,没有它,没有人知道你错过了什么。 –

你写的错误是错误的,UIViewController的确实有方法。你的错误必须是“UIVIew没有成员...”,至于解决方案你只需要引用任何UIViewController使用这个特定的视图。

然后使用该引用调用你的执行方法。

例如,如果您的自定义视图被编程添加,您可以将额外的属性就像

weak var myVC : UIViewController? 

然后实例,它只是把它分配给该属性后添加到您的类。

然后执行SEGUE变为:

myVC?.performSegue.... 

或者你可以从使用该视图中添加的UIViewController点击手势识别,然后有视图显示它从那里。


或者你可以只使用:

UIApplication.shared.keyWindow?.rootViewController.performSegue... 

但是,如果你这样做,你应该小心,因为这一次也许有意想不到的后果取决于你所提出和这是当前RootViewController的。

在UIView类进行委派,并用于您所使用的UIView其中Yourcontroller类

protocol UIViewDelegate 
{ 
func tapFunction() //this function will be use in controller class which uiview are using 
} 

var delegate: UIViewDelegate? 
func tapFunction(sender:UITapGestureRecognizer) 
{ 
delegate?.tapFunction() 
} 

class Yourcontroller: UIViewController, UIViewDelegate 
    { 
    let yourView = YourView() 
    yourView.delegate = self 

    func tapFunction() 
    { 
    self.performSegue(withIdentifier: "memberSegue", sender: self) 
    } 
    } 

你可以通过协议和委托来完成。因为实际上你无法从UIView中完成,所以你应该总是从控制器中完成。

/// add this protocol in your project 
protocol ViewProtocol { 
    func performSegueFromView() 
} 

class View : UIView { 

    /// add this line to your view 
    var delegate : ViewProtocol? 


    /// replace your tap function with this 
    func tapFunction(sender:UITapGestureRecognizer) { 

     print("tap working") 

     self.delegate?.performSegueFromView() 
    } 

} 


class ViewController : UIViewController, ViewProtocol { 

    override func viewDidLoad() { 

     /// create view 
     let view = View() 
     /// assign view delegate 
     view.delegate = self 

    } 

    /// delegate function 
    func performSegueFromView() { 
     ///peform segue from controller because you can not do this from uiview anyway 
     self.performSegue(withIdentifier: "memberSegue", sender: self) 
    } 

}