reason: '-[UIView setSelected:]: unrecognized selector sent to instance 0x7f980ed1cac0'

程序崩溃提示:reason: '-[UIView setSelected:]: unrecognized selector sent to instance 0x7f980ed1cac0';


屏幕快照 2019-10-29 下午4.26.16.png

找不到setSelect 方法,分析程序后发现是titleButtonClick:方法中的问题
屏幕快照 2019-10-29 下午4.38.15.png

UIView 并没有setSelected方法,而UIButton中有setSelected方法,此时怀疑本应该是UIButton而此时传进去了UIView,进一步验证
屏幕快照 2019-10-29 下午4.29.34.png
调试崩溃前的代码,发现此时titleButton果然是UIView类.找到了崩溃的原因.
怎么解决呢?发现了这行代码
 TitleButton *titleButton = [self.titlesView viewWithTag:index];

问题出在了这个方法

(nullable __kindof UIView *)viewWithTag:(NSInteger)tag; // recursive search. includes self

注释的内容是递归查找.包括自身
index值的范围是0~4,当index值是0的时候,既有UIView又有UIButton,此时会把UIView传进去,所以程序就崩溃了.
解决方法是修改tag值,或者使用这个方法.

TitleButton *titleButton = self.titlesView.subviews[index];

你可能感兴趣的:(reason: '-[UIView setSelected:]: unrecognized selector sent to instance 0x7f980ed1cac0')