公告
财富商城
积分规则
提问
发文
2018-12-30 23:58发布
泪湿衣
I know how delegates work, and I know how I can use them.
But how do I create them?
In my point of view create separate class for that delegate method and you can use where you want.
in my Custom DropDownClass.h
typedef enum { DDSTATE, DDCITY }DropDownType; @protocol DropDownListDelegate <NSObject> @required - (void)dropDownDidSelectItemWithString:(NSString*)itemString DropDownType:(DropDownType)dropDownType; @end @interface DropDownViewController : UIViewController { BOOL isFiltered; } @property (nonatomic, assign) DropDownType dropDownType; @property (weak) id <DropDownListDelegate> delegate; @property (strong, nonatomic) NSMutableArray *array1DropDown; @property (strong, nonatomic) NSMutableArray *array2DropDown;
after that in.m file create array with objects,
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{ CGFloat rowHeight = 44.0f; return rowHeight; } -(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { return 1; } - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{ return isFiltered?[self.array1DropDown count]:[self.array2DropDown count]; } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *simpleTableIdentifier = @"TableCell"; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier]; if (cell == nil) { cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier]; } if (self.delegate) { if (self.dropDownType == DDCITY) { cell.textLabel.text = [self.array1DropDown objectAtIndex:indexPath.row]; } else if (self.dropDownType == DDSTATE) { cell.textLabel.text = [self.array2DropDown objectAtIndex:indexPath.row]; } } return cell; } - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { [self dismissViewControllerAnimated:YES completion:^{ if(self.delegate){ if(self.dropDownType == DDCITY){ [self.delegate dropDownDidSelectItemWithString:[self.array1DropDown objectAtIndex:indexPath.row] DropDownType:self.dropDownType]; } else if (self.dropDownType == DDSTATE) { [self.delegate dropDownDidSelectItemWithString:[self.array2DropDown objectAtIndex:indexPath.row] DropDownType:self.dropDownType]; } } }]; }
Here all are set for Custom delegate class.after that you can use this delegate method where you want.for example...
in my another viewcontroller import after that
create action for calling delegate method like this
- (IBAction)dropDownBtn1Action:(id)sender { DropDownViewController *vehicleModelDropView = [[DropDownViewController alloc]init]; vehicleModelDropView.dropDownType = DDCITY; vehicleModelDropView.delegate = self; [self presentViewController:vehicleModelDropView animated:YES completion:nil]; }
after that call delegate method like this
- (void)dropDownDidSelectItemWithString:(NSString *)itemString DropDownType:(DropDownType)dropDownType { switch (dropDownType) { case DDCITY:{ if(itemString.length > 0){ //Here i am printing the selected row [self.dropDownBtn1 setTitle:itemString forState:UIControlStateNormal]; } } break; case DDSTATE: { //Here i am printing the selected row [self.dropDownBtn2 setTitle:itemString forState:UIControlStateNormal]; } default: break; } }
最多设置5个标签!
In my point of view create separate class for that delegate method and you can use where you want.
in my Custom DropDownClass.h
after that in.m file create array with objects,
Here all are set for Custom delegate class.after that you can use this delegate method where you want.for example...
in my another viewcontroller import after that
create action for calling delegate method like this
after that call delegate method like this