標籤:style class blog code http tar
關於UIScrollView有些你很難知曉的崩潰情形
為了實現以下的功能(按鈕之間的轉場效果):
簡短的代碼如下:
//// RootViewController.m// BUG//// Copyright (c) 2014年 Y.X. All rights reserved.//#import "RootViewController.h"@interface RootViewController (){ UIView *_showView;}@end@implementation RootViewController- (void)viewDidLoad{ [super viewDidLoad]; _showView = [[UIView alloc] initWithFrame:self.view.bounds]; [self.view addSubview:_showView]; NSArray *title = @[@"YouXianMing", @"XianHui", @"XianMing", @"XianNeng", @"XianRen"]; [title enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) { // 初始化button UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(50, 50*(idx + 1), 130, 30)]; button.layer.borderWidth = 1.f; [_showView addSubview:button]; // 設定字型 button.titleLabel.font = [UIFont fontWithName:@"HelveticaNeue-Thin" size:15.f]; // 設定標題以及標題顏色 [button setTitle:obj forState:UIControlStateNormal]; [button setTitleColor:[UIColor redColor] forState:UIControlStateNormal]; // 添加事件 [button addTarget:self action:@selector(buttonsEvent:) forControlEvents:UIControlEventTouchUpInside]; }];}- (void)buttonsEvent:(UIButton *)button{ [_showView.subviews enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) { UIButton *tmpButton = obj; if ([tmpButton isEqual:button]) { [tmpButton setTitleColor:[UIColor redColor] forState:UIControlStateNormal]; } else { [tmpButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal]; } }];}@end
之後,將UIView替換成UIScrollView後:
然後就會崩潰-_-!!
崩潰資訊:
*** Terminating app due to uncaught exception ‘NSInvalidArgumentException‘, reason: ‘-[UIImageView setTitleColor:forState:]: unrecognized selector sent to instance 0xa590390‘
崩潰原因是_showView.subviews裡面有一個UIImageView
我們並沒有添加這個東西UIImageView到subviews中呢,其實,這個東西是UIScrollView自己的一個東西......
寫上以下保護性語句就沒問題了.
話說,UIScrollView跟你偷偷加了點東西讓你崩潰了都不知道咋回事-_-!!!