一类:
在以下方法中,我使用块来替换普通委托。可以吗?方法中的块didSelectRowAtIndexPath
,我用它来替换普通委托,但是如果它运行,我单击表格单元格并且代码崩溃。
typedef void (^Block)(NSString *id,NSString *cityName);
@interface WLCCityListViewController : WLCBaseSquareViewController <UITableViewDataSource,UITableViewDelegate>
{
Block _block;
id<commonDelegate>delegate;
}
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil Block:(Block)block
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
_block=block;
}
return self;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
WLCMainViewCityListData *data=[[self citylists]objectAtIndex:[indexPath section]];
// [self.delegate seleteCityID:[[data.cityList objectAtIndex:indexPath.row]objectForKey:@"id"] CityName:[[data.cityList objectAtIndex:indexPath.row]objectForKey:@"name"]];
NSString *a1 =[[data.cityList objectAtIndex:indexPath.row]objectForKey:@"id"];
NSString *a2 =[[data.cityList objectAtIndex:indexPath.row]objectForKey:@"name"];
_block(a1,a2);
}
B类:
@interface WLCMainViewController : WLCBaseSquareViewController
{
}
@implementation WLCMainViewController
-(void)viewDidLoad {
WLCCityListViewController *tableViewController = [[[WLCCityListViewController alloc]initWithNibName:@"WLCCityListViewController" bundle:nil Block:^(NSString *id, NSString *cityName) {
self.cityID=id;
WLCMainViewModel *model=(WLCMainViewModel *)self.mainviewModel;
model.cityID=id;
[model sendRequest];
[self.view startWaiting];
}] autorelease];
}