Ios 通用导航按钮操作另一个类
我为所有视图控制器制作了通用导航栏。但我需要在调用公共导航栏的按钮上单击操作Ios 通用导航按钮操作另一个类,ios,swift,Ios,Swift,我为所有视图控制器制作了通用导航栏。但我需要在调用公共导航栏的按钮上单击操作 @objc extension UIViewController { @objc func setBarButtonItem(titleLabel: String) { let view = UIView.init(frame: CGRect.init(x: 0, y: 0, width: 200 + 4, height: 38)) let Nextbtn = UIButton(type: .custom)
@objc extension UIViewController {
@objc func setBarButtonItem(titleLabel: String)
{
let view = UIView.init(frame: CGRect.init(x: 0, y: 0, width: 200 + 4, height: 38))
let Nextbtn = UIButton(type: .custom)
Nextbtn.frame = CGRect(x: 0, y: 0, width: 30, height: 30)
Nextbtn.addTarget(self, action: #selector(NextButtonClicked), for: .touchUpInside)
view.addSubview(Nextbtn)
self.navigationItem.setLeftBarButton(UIBarButtonItem(customView: view), animated: true)
/*
@objc func NextButtonClicked()
{
}*/
}
}
正在调用控制器-->setBackBarButton(“你好…”)
如果我在这个类上创建了Button操作方法,则会调用Button。但是我想在这个调用控制器类上创建按钮操作方法func NextButtonClicked(),或者我们可以访问这个类上的按钮操作的任何方法
@objc func NextButtonClicked()
{
}
您只需创建
BaseViewController
扩展,并允许所有viewcontroller从此BaseViewController继承
BaseViewController:
import UIKit
class BaseViewController: UIViewController {
typealias CompletionBarButtonClicked = (()-> Void)
var completionBarItemClicked:CompletionBarButtonClicked?
override func viewDidLoad() {
super.viewDidLoad()
}
@objc func setBarButtonItem(titleLabel: String, completion :@escaping CompletionBarButtonClicked)
{
self.completionBarItemClicked = completion
let view = UIView.init(frame: CGRect.init(x: 0, y: 0, width: 200 + 4, height: 38))
let Nextbtn = UIButton(type: .custom)
Nextbtn.frame = CGRect(x: 0, y: 0, width: 30, height: 30)
Nextbtn.addTarget(self, action: #selector(NextButtonClicked), for: .touchUpInside)
view.addSubview(Nextbtn)
self.navigationItem.setLeftBarButton(UIBarButtonItem(customView: view), animated: true)
}
@objc func NextButtonClicked()
{
if let completionHandler = completionBarItemClicked{
completionHandler()
}
}
}
import UIKit
class ViewController: BaseViewController {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
self.setBarButtonItem(titleLabel: "test") {
/// your action There
}
}
}
示例视图控制器:
import UIKit
class BaseViewController: UIViewController {
typealias CompletionBarButtonClicked = (()-> Void)
var completionBarItemClicked:CompletionBarButtonClicked?
override func viewDidLoad() {
super.viewDidLoad()
}
@objc func setBarButtonItem(titleLabel: String, completion :@escaping CompletionBarButtonClicked)
{
self.completionBarItemClicked = completion
let view = UIView.init(frame: CGRect.init(x: 0, y: 0, width: 200 + 4, height: 38))
let Nextbtn = UIButton(type: .custom)
Nextbtn.frame = CGRect(x: 0, y: 0, width: 30, height: 30)
Nextbtn.addTarget(self, action: #selector(NextButtonClicked), for: .touchUpInside)
view.addSubview(Nextbtn)
self.navigationItem.setLeftBarButton(UIBarButtonItem(customView: view), animated: true)
}
@objc func NextButtonClicked()
{
if let completionHandler = completionBarItemClicked{
completionHandler()
}
}
}
import UIKit
class ViewController: BaseViewController {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
self.setBarButtonItem(titleLabel: "test") {
/// your action There
}
}
}