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


UIView 并没有setSelected方法,而UIButton中有setSelected方法,此时怀疑本应该是UIButton而此时传进去了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];
网友评论