@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,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.但是我想在这个调用Calling Controller类上制作按钮动作方法func NextbuttonClicked(),或者我们可以访问这个类上的按钮动作的任何方法.
@objc func NextbuttonClicked(){}解决方法 您只需创建BaseVIEwController而不是扩展,并允许所有VIEwControllers继承此BaseVIEwController
BaseVIEwController:
import UIKitclass 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,height: 38)) let Nextbtn = UIbutton(type: .custom) Nextbtn.frame = CGRect(x: 0,height: 30) Nextbtn.addTarget(self,for: .touchUpInsIDe) vIEw.addSubvIEw(Nextbtn) self.navigationItem.setleftbarbutton(UIbarbuttonItem(customVIEw: vIEw),animated: true) } @objc func NextbuttonClicked() { if let completionHandler = completionbarItemClicked{ completionHandler() } }}
示例VIEwController:
import UIKitclass 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 } }}总结
以上是内存溢出为你收集整理的ios – 常用导航按钮动作另一个类全部内容,希望文章能够帮你解决ios – 常用导航按钮动作另一个类所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)