【发布时间】:2014-07-28 09:54:31
【问题描述】:
我尝试使用委托但没有成功,委托没有被调用。 这是我的委托代码:
设置委托(ViewControllerB)
@class SearchGardenTable;
@protocol SearchGardenDelegate <NSObject>
- (void)addItemViewController:(SearchGardenTable *)controller didFinishWithGardenID:(NSString *)gardenID gardenName:(NSString*)gardenName andCityName:(NSString*)cityName;
@end
@interface SearchGardenTable : UITableViewController
@property (nonatomic, weak) id <SearchGardenDelegate> delegate;
@end
告诉 VC B VC A 是它的代表:
// in VC A viewDidLoad.
self.searchGarden = [[SearchGardenTable alloc]init];
[self.searchGarden setDelegate:self];
VC A 采用协议
@interface AddKid () <MZFormSheetBackgroundWindowDelegate,SearchGardenDelegate>
@end
@implementation AddKid
设置我想从 VC B 传回 VC A 的参数:
_kidGaedenID = [temp objectAtIndex:indexPath.row];
_kidGardenName = [temp1 objectAtIndex:indexPath.row];
[self.delegate addItemViewController:self didFinishWithGardenID:_kidGaedenID gardenName:_kidGardenName andCityName:_kidCityName];
在VC A实现协议方法:
- (void)addItemViewController:(SearchGardenTable *)controller didFinishWithGardenID:(NSString *)gardenID gardenName:(NSString*)gardenName andCityName:(NSString*)cityName
{
_kidGardenID = gardenID;
_gardenName.text = gardenName;
_kidCity.text = cityName;
}
编辑
ViewController B didSelectRowAtIndexPath: code
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath;
NSArray *temp = [_responseDict valueForKey:@"ID"];
NSArray *temp1 = [_responseDict valueForKey:@"name"];
_kidGaedenID = [temp objectAtIndex:indexPath.row];
_kidGardenName = [temp1 objectAtIndex:indexPath.row];
[[self delegate] addItemViewController:self didFinishWithGardenID:_kidGaedenID gardenName:_kidGardenName andCityName:_kidCityName];
[self mz_dismissFormSheetControllerAnimated:YES completionHandler:^(MZFormSheetController *formSheetController) {
//dismiss the popup view which i create it VC A with that gitHub package:
https://github.com/m1entus/MZFormSheetController/issues/98
}];
编辑 2 呈现 ViewControllerB 的代码:
- (IBAction)chooseGardenAndCityBtn:(id)sender {
UIViewController *vc = [self.storyboard instantiateViewControllerWithIdentifier:@"modal"];
MZFormSheetController *formSheet = [[MZFormSheetController alloc] initWithViewController:vc];
formSheet.presentedFormSheetSize = CGSizeMake(300, 298);
// formSheet.transitionStyle = MZFormSheetTransitionStyleSlideFromTop;
formSheet.shadowRadius = 2.0;
formSheet.shadowOpacity = 0.3;
formSheet.shouldDismissOnBackgroundViewTap = YES;
formSheet.shouldCenterVertically = YES;
formSheet.movementWhenKeyboardAppears = MZFormSheetWhenKeyboardAppearsCenterVertically;
// formSheet.keyboardMovementStyle = MZFormSheetKeyboardMovementStyleMoveToTop;
// formSheet.keyboardMovementStyle = MZFormSheetKeyboardMovementStyleMoveToTopInset;
// formSheet.landscapeTopInset = 50;
// formSheet.portraitTopInset = 100;
__weak MZFormSheetController *weakFormSheet = formSheet;
// If you want to animate status bar use this code
formSheet.didTapOnBackgroundViewCompletionHandler = ^(CGPoint location) {
UINavigationController *navController = (UINavigationController *)weakFormSheet.presentedFSViewController;
if ([navController.topViewController isKindOfClass:[SearchCityTable class]]) {
SearchCityTable *mzvc = (SearchCityTable *)navController.topViewController;
mzvc.showStatusBar = NO;
}
[UIView animateWithDuration:0.3 animations:^{
if ([weakFormSheet respondsToSelector:@selector(setNeedsStatusBarAppearanceUpdate)]) {
[weakFormSheet setNeedsStatusBarAppearanceUpdate];
}
}];
};
formSheet.willPresentCompletionHandler = ^(UIViewController *presentedFSViewController) {
// Passing data
UINavigationController *navController = (UINavigationController *)presentedFSViewController;
navController.topViewController.title = @"בחר עיר";
};
formSheet.transitionStyle = MZFormSheetTransitionStyleCustom;
[MZFormSheetController sharedBackgroundWindow].formSheetBackgroundWindowDelegate = self;
[self mz_presentFormSheetController:formSheet animated:YES completionHandler:^(MZFormSheetController *formSheetController) {
}];
}
编辑
【问题讨论】:
-
你指定VC A采用SearchGardenDelegate协议了吗?
-
是的,我做了.. 看我编辑。
-
您从哪里向委托方法发送消息?来自您的 VC B 类中的哪个方法?
-
你在哪里有这个 [self.delegate addItemViewController:self didFinishWithGardenID:_kidGaedenID gardenName:_kidGardenName andCityName:_kidCityName];
-
didSelectRowAtIndexPath: tableview 方法
标签: ios objective-c uiviewcontroller delegates