gpt4 book ai didi

ios - 使用自定义按钮类调用特定 View Controller

转载 作者:行者123 更新时间:2023-11-30 12:55:57 25 4
gpt4 key购买 nike

大家。我有一个自定义类的个人资料按钮,单击后会显示 ProfileViewController。 ProfileButton 继承自 UIButton,并出现在多个 View Controller 的导航栏上。虽然我知道我可以向每个 VC 的代码添加一个函数,但我更喜欢以更优雅的方式执行此操作。到目前为止我尝试过的是:-

  1. 从 ProfileButton 类中调用 ProfileViewController:我无法使用 Storyboard.instantiateViewControllerWithIdentifier 引用 ProfileViewController。

  2. 使用委托(delegate):这仍然意味着向每个 VC 添加代码。提前致谢。

我已经发布了按钮类的代码。我正在寻找一种将代码放置在仍能实现此结果的单个位置的方法。

class ProfileButton: UIButton {

override func awakeFromNib() {

self.addTarget(self, action: #selector(self.profileButtonPressed), forControlEvents: .TouchDown)

}

}

编辑:-

问题不在于介绍或解雇 VC。我只是想找到使用配置文件按钮呈现 ProfileVC 的最优雅的方式,而不将 self.presentViewController(animated: true,completion: nil) 添加到 ViewController1、ViewController2、ViewController3 等。

这是显示其设置方式的图像:-

Illustration

最佳答案

您可以通过为所有具有带有“个人资料”按钮的导航栏的 View Controller 创建一个基类来实现此目的。

在基类中编写配置文件按钮操作,扩展类中的任何操作都将调用基类方法。

关于ios - 使用自定义按钮类调用特定 View Controller ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40387856/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com