Quantcast
Channel: C++博客-天使的白骨
Viewing all articles
Browse latest Browse all 11

solve crosse language of object-c & swift UIScrollViewDelegate/UITableViewDelegate rac_signalForSelector("scrollViewDidEndDragging:willDecelerate:") crash

$
0
0
Chinese readers annotation:这个问题google,stackoverflow,github都还没有相关资料,所以把内容写成英文的,以便于帮助国外的朋友。
English readers annotation:This problem has not been found by google or stackoverflow or github yet. So I writed it at here so that you could get help from this.(By the way, I am a Chinese)

I want to use rac_signalForSelector to observe the UITableView's delegate so I could let the dataDelegate and delegate being reused from a independent class. So If they have the same cell, I would not need to implement them triples.

If you use the RAC(which means ReactiveCocoa), Firstly come into you head is a code sample like this:
self.rac_signalForSelector(Selector("scrollViewDidEndDecelerating:")).subscribeNext{ _ in
            self.setTabBarVisiable(false, animated:true)
        }
scrollView.delegate = self

well...It's fine if you don't need this method:
self.rac_signalForSelector("scrollViewDidEndDragging:willDecelerate:").subscribeNext{ _ in
            self.setTabBarVisiable(false, animated:true)
        }
But if you do, you will get a crash message(EXE_BAD_ACCESS) which caused by the RAC.It means RAC can't find a method form string '@' (which should be @"scrollViewDidEndDragging:willDecelerate:
"),  So I have work around with it,  and I guessed it's the reason of swift Protocol isn't a NSObject(you could see from UIScrollViewDelegate's declaration). 

By a chance I found there is a method named 'NSProtocolFromString' and RAC provided a method 
- (RACSignal *)rac_signalForSelector:(SEL)selector fromProtocol:(Protocol *)protocol;


So I combined them both
self.rac_signalForSelector("scrollViewDidEndDragging:willDecelerate:",
            fromProtocol:NSProtocolFromString("UITableViewDelegate")).subscribeNext{ _ in
            self.setTabBarVisiable(false, animated:true)
        }

It doesn't crash any more and the function of my design worked fine. So, that's the way! done!
Hope this helps you out, it's really struggle.

何清龙 2015-01-08 10:34 发表评论

Viewing all articles
Browse latest Browse all 11

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>