有的時候同一個頁面有好幾按鈕苹威,每一個按鈕對應(yīng)一個方法調(diào)用撞叨,一般代碼我們都會用switch來進行區(qū)分实辑,代碼如下:
switch([x intValue])
{
case 0:
{
[self loadFastRegisterView];
break;
}
case 1:
{
[self loadRealNameView];
break;
}
case 2:
{
[self loadBindPhoneView];
break;
}
case 3:
{
[self loadChangePasswordView];
break;
}
case 4:
{
[self loadServicerCenterView];
break;
}
default:
{
break;
}
}
是不是感覺很low漱病?我們可以使用runtime來實現(xiàn)這一需求:
SEL selectors[] =
{
@selector(loadFastRegisterView),
@selector(loadRealNameView),
@selector(loadBindPhoneView),
@selector(loadChangePasswordView),
@selector(loadServicerCenterView)
};
if ([x intValue] < sizeof(selectors) / sizeof(SEL))
{
void(*imp)(id, SEL) = (typeof(imp))[self methodForSelector:selectors[[x intValue]]];
imp(self, selectors[[x intValue]]);
}
瞬間逼格高了很多买雾。
還有tableView的cell點擊跳轉(zhuǎn)不同的控制器
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
Class class = NSClassFromString(self.sourceArr[indexPath.row][@"control"]);
[self.navigationController pushViewController:[class new] animated:YES];
}