本文主要是介绍Swift开发-代理,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
在iOS开发中经常会用到代理,Swift开发中的代理这样写:
1、首先定义一个协议
// swift中如何定义协议: 必须遵守NSObjectProtocol
protocol VisitorViewDelegate : NSObjectProtocol{// 登录回调func loginBtnWillClick()// 注册回调func regiserBtnWillClick()}
2、方法实现
func loginBtnClick(){delegate?.loginBtnWillClick()}func regiserBtnClick(){delegate?.regiserBtnWillClick()}
3、方法调用
private func setupVisitorView(){// 1、初始化未登录界面let customView = VisitorView()customView.delegate = selfview = customViewvisitorView = customView// 2、设置导航条未登录按钮
// navigationController?.navigationBar.tintColor = UIColor.orangeColor()navigationItem.leftBarButtonItem = UIBarButtonItem(title: "注册", style: UIBarButtonItemStyle.Plain, target: self, action: #selector(regiserBtnWillClick))navigationItem.rightBarButtonItem = UIBarButtonItem(title: "登录", style: UIBarButtonItemStyle.Plain, target: self, action: #selector(loginBtnWillClick))}func loginBtnWillClick() {print(#function)}func regiserBtnWillClick() {print(#function)}
这篇关于Swift开发-代理的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!