在ViewController里面不能定义protocol么?

头发还没秃a 2016-04-13 10:05:56
我用Reachability实现个网络监听,不过每个界面都需要实例化Reachability然后调用回调函数,导致有多少个界面就有多少个回调函数在运行好麻烦,所以我想弄个delegate这样每次网络改变不管有多少个界面,监听只回调一次函数,delegate代码写在ViewController文件最上面:

protocol ReachabilityDelegate: class, NSObjectProtocol{
func WhetherHaveReachability(reachability: Reachability)
}

然后就是在ViewController里面定义:

class ViewController: UIViewController {
var delegate: ReachabilityDelegate!

override func viewDidLoad() {
super.viewDidLoad()
self.setupReachability()
}

func setupReachability() {
if self.delegate.respondsToSelector("WhetherHaveReachability:") {
self.delegate.WhetherHaveReachability(reachability)
}
}
}


运行的时候var delegate: ReachabilityDelegate!这句报空。。求指教
...全文
186 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
头发还没秃a 2016-04-15
  • 打赏
  • 举报
回复
引用 5 楼 qinken547 的回复:
// The protocol to delegate the
// button tap to the ViewController
protocol ButtonDelegate {
    func onButtonTap(sender: UIButton)
}
 
class ViewWithTextAndButton: UIView {
    
    // keeping track of the delegate to use later
    weak var delegate: ButtonDelegate?
    
    func onButtonTap(sender: UIButton) {
        // invoking the delegate when the
        // button is actually tapped
        delegate?.onButtonTap(sender)
    }
}
 
class MyViewController: UIViewController, ButtonDelegate {
    
    let viewWithTextAndButton = ViewWithTextAndButton(frame: CGRect(x: 0, y: 0, width: 100, height: 100))
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        // assigning the delegate
        viewWithTextAndButton.delegate = self
        view.addSubview(viewWithTextAndButton)
    }
    
    // MARK: ButtonDelegate
    // the delegated logic is here
    func onButtonTap(sender: UIButton) {
        print("This button was clicked in the subview!")
    }
 
}
没办法了,还是直接使用回调了
qinken547 2016-04-14
  • 打赏
  • 举报
回复
// The protocol to delegate the
// button tap to the ViewController
protocol ButtonDelegate {
    func onButtonTap(sender: UIButton)
}
 
class ViewWithTextAndButton: UIView {
    
    // keeping track of the delegate to use later
    weak var delegate: ButtonDelegate?
    
    func onButtonTap(sender: UIButton) {
        // invoking the delegate when the
        // button is actually tapped
        delegate?.onButtonTap(sender)
    }
}
 
class MyViewController: UIViewController, ButtonDelegate {
    
    let viewWithTextAndButton = ViewWithTextAndButton(frame: CGRect(x: 0, y: 0, width: 100, height: 100))
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        // assigning the delegate
        viewWithTextAndButton.delegate = self
        view.addSubview(viewWithTextAndButton)
    }
    
    // MARK: ButtonDelegate
    // the delegated logic is here
    func onButtonTap(sender: UIButton) {
        print("This button was clicked in the subview!")
    }
 
}
qinken547 2016-04-14
  • 打赏
  • 举报
回复
你的写法不对
三月江城 2016-04-13
  • 打赏
  • 举报
回复
设置代理 遵守协议 实现方法
头发还没秃a 2016-04-13
  • 打赏
  • 举报
回复
我用Reachability实现个网络监听,不过每个界面都需要实例化Reachability然后调用回调函数,导致有多少个界面就有多少个回调函数在运行好麻烦,所以我想弄个delegate这样每次网络改变不管有多少个界面,回调函数只调用一次,delegate代码写在ViewController文件最上面:

protocol ReachabilityDelegate: class, NSObjectProtocol{
    func WhetherHaveReachability(reachability: Reachability)
}
然后就是在ViewController里面定义:

class ViewController: UIViewController {
    var delegate: ReachabilityDelegate!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        self.setupReachability()
    }

    func setupReachability() {
        if self.delegate.respondsToSelector("WhetherHaveReachability:") {
            self.delegate.WhetherHaveReachability(reachability)
        }
    }
}
这样写确实有错误,因为delegate还是为nil的时候,就运行了setupReachability()方法。。。或许我该在viewDidLoad() 里面self.setupReachability()之前先在这个ViewController里面实现定义的delegate?例如:

protocol ReachabilityDelegate: class, NSObjectProtocol{
    func WhetherHaveReachability(reachability: Reachability)
}
class ViewController: UIViewController,ReachabilityDelegate {
    var delegate: ReachabilityDelegate!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        self.delegate = self
        self.setupReachability()
    }

    func setupReachability() {
        if self.delegate.respondsToSelector("WhetherHaveReachability:") {
            self.delegate.WhetherHaveReachability(reachability)
        }
    }

    func WhetherHaveReachability(reachability: Reachability){
        //实现delegate方法
    }

}
但是这样写还有一个问题,就是其他controller使用不了这个delegate。。。怎么办怎么办?
qinken547 2016-04-13
  • 打赏
  • 举报
回复
weak var delegate:ReachabilityDelegate?

29,029

社区成员

发帖
与我相关
我的任务
社区描述
主要讨论与iOS相关的软件和技术
社区管理员
  • iOS
  • 大熊猫侯佩
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧