Ios 在ViewControllerB和ViewControllerA之间传递数据,未调用委托

Ios 在ViewControllerB和ViewControllerA之间传递数据,未调用委托,ios,objective-c,uiviewcontroller,delegates,Ios,Objective C,Uiviewcontroller,Delegates,我尝试使用委托但未成功,未调用该委托。 这是我的代表代码: 设置代理(ViewControllerB) VC A采用该协议 @interface AddKid () <MZFormSheetBackgroundWindowDelegate,SearchGardenDelegate> @end @implementation AddKid 在VC A上实现协议方法: - (void)addItemViewController:(SearchGardenTable *)contr

我尝试使用委托但未成功,未调用该委托。 这是我的代表代码:

设置代理(ViewControllerB)

VC A采用该协议

@interface AddKid () <MZFormSheetBackgroundWindowDelegate,SearchGardenDelegate>

@end

@implementation AddKid 
在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 B初始化

在拨打此电话之前,请确保self.searchGarden已初始化

self.searchGarden.delegate = self; // in VC A viewDidLoad.

请出示您的VC B初始化

在拨打此电话之前,请确保self.searchGarden已初始化

self.searchGarden.delegate = self; // in VC A viewDidLoad.

远大目标,但您是否尝试过:

 [[self delegate] addItemViewController:self didFinishWithGardenID:_kidGaedenID gardenName:_kidGardenName andCityName:_kidCityName]];
而不是

 [self.delegate addItemViewController:self didFinishWithGardenID:_kidGaedenID gardenName:_kidGardenName andCityName:_kidCityName];

远大目标,但您是否尝试过:

 [[self delegate] addItemViewController:self didFinishWithGardenID:_kidGaedenID gardenName:_kidGardenName andCityName:_kidCityName]];
而不是

 [self.delegate addItemViewController:self didFinishWithGardenID:_kidGaedenID gardenName:_kidGardenName andCityName:_kidCityName];
你在干什么

self.searchGarden = [[SearchGardenTable alloc]init];
但是你说你在故事板中有你的视图控制器。这让我想到self.searchGarden在viewDidLoad:的角度与故事板中的对象不同

当您准备推送VC B时或在执行搜索之前(如果您使用的是segue),应将一个对象指定给self.searchGarden

编辑

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

        }];
根据你的代码,我假设

UIViewController *vc = [self.storyboard instantiateViewControllerWithIdentifier:@"modal"];
是你的VC B,所以你需要做VC.delegate=self; 请记住,您的vc必须是vc B类型,在您的情况下,它是可搜索的

编辑2

考虑到当前的设置,我现在想到的最快的解决方案是NSNotification。从您的VC B发送通知:

[[NSNotificationCenter defaultCenter] postNotificationName:@"userDidTapOnCell" object:nil userInfo:dictionaryOfObjects];
使用从模型检索的对象初始化对象字典,如:

NSDictionary *dictionary = @{ @"gardenID": _kidGaedenID, @"gardenName": _kidGardenName };
在VC A中,您将自己添加为viewDidLoad通知的观察者:

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(gardenInfoDidComeBack:) name:@"userDidTapOnCell" object:nil];
并实施该方法:

- (void)gardenInfoDidComeBack:(NSNotification *)notification {
    NSDictionary *userInfo = notification.userInfo;
    // retrieve the kidGardenID and kidGardenName from the dict and use them as you whish.
}
希望这能帮助你完成任务。您的导航架构可能不是最好的,但这超出了本主题的范围,本主题已经做得够多了。

您正在做什么

self.searchGarden = [[SearchGardenTable alloc]init];
但是你说你在故事板中有你的视图控制器。这让我想到self.searchGarden在viewDidLoad:的角度与故事板中的对象不同

当您准备推送VC B时或在执行搜索之前(如果您使用的是segue),应将一个对象指定给self.searchGarden

编辑

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

        }];
根据你的代码,我假设

UIViewController *vc = [self.storyboard instantiateViewControllerWithIdentifier:@"modal"];
是你的VC B,所以你需要做VC.delegate=self; 请记住,您的vc必须是vc B类型,在您的情况下,它是可搜索的

编辑2

考虑到当前的设置,我现在想到的最快的解决方案是NSNotification。从您的VC B发送通知:

[[NSNotificationCenter defaultCenter] postNotificationName:@"userDidTapOnCell" object:nil userInfo:dictionaryOfObjects];
使用从模型检索的对象初始化对象字典,如:

NSDictionary *dictionary = @{ @"gardenID": _kidGaedenID, @"gardenName": _kidGardenName };
在VC A中,您将自己添加为viewDidLoad通知的观察者:

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(gardenInfoDidComeBack:) name:@"userDidTapOnCell" object:nil];
并实施该方法:

- (void)gardenInfoDidComeBack:(NSNotification *)notification {
    NSDictionary *userInfo = notification.userInfo;
    // retrieve the kidGardenID and kidGardenName from the dict and use them as you whish.
}

希望这能帮助你完成任务。可能您的导航架构不是最好的,但这超出了本主题的范围,本主题已经做得够多了。

我从您的查询和关注点中了解到,我发现了一些东西

   UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];

    searchVC = (ControllerA *)[storyboard instantiateViewControllerWithIdentifier:@"controllerA"];
    searchVC.delegate = self;

    [self.navigationController pushViewController:searchVC animated:YES];

希望这能对您有所帮助:)

从您的询问和担忧中我了解到了一些东西

   UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];

    searchVC = (ControllerA *)[storyboard instantiateViewControllerWithIdentifier:@"controllerA"];
    searchVC.delegate = self;

    [self.navigationController pushViewController:searchVC animated:YES];


希望这能对您有所帮助:)

您指定VC A采用SearchGardenDelegate协议了吗?是的,我指定了。。请参阅我的编辑。您从哪里发送委托方法的消息?您的VC B类中的哪个方法?其中有[self.delegate addItemViewController:self didFinishWithGardenID:_kidGaedIdGardenName:_kidGardenNameand CityName:_kidCityName];didSelectRowAtIndexPath:tableview方法您指定VC A采用SearchGardenDelegate协议了吗?是的,我指定了。。请参阅我的编辑。您从哪里发送委托方法的消息?您的VC B类中的哪个方法?其中有[self.delegate addItemViewController:self didFinishWithGardenID:_kidGaedIdGardenName:_kidGardenNameand CityName:_kidCityName];DidSelectRowAtIndex路径:tableviewmethod@property(非原子,弱)id委托;弱????我认为这是在它被使用之前发布的-Try Strong上次尝试时,我被告知它应该是赋值的,但肯定不是Strong,我不知道。@property(非原子,弱)id delegate;弱????我想这是在它被使用之前发布的-Try Strong我上次尝试时被告知它应该被分配,但肯定不是Strong我不确定。我在ViewControllerA@property(nonatomic,Strong)SearchGardenTable*searchGarden中有一个属性;我看得出来。但是您正在viewDidLoad中初始化一个新的VC B实例,因此它可能与您正在显示的实例不同。值得一看您为从VC AIt中推送VC B而编写的代码。您仍然缺少再次从VC Aedit中推送VC B的代码,现在所有代码都显示在VC B弹出窗口中。@OshriALM请参阅我的编辑2。我在ViewControllerA@property(非原子,强)SearchGardentTable*searchGarden中有一个属性;我看得出来。但是您正在viewDidLoad中初始化一个新的VC B实例,因此它可能与您正在显示的实例不同。值得一看你写的从VC AIt中推送VC B的代码仍然缺少再次从VC AIt中推送VC B的代码,现在有了VC B弹出窗口的所有代码。@OshriALM请参见我的编辑2。我不能使用segue,因为ViewController B是一个弹出窗口,我使用gitHub MZFormSheetController的这个包实现它。当我点击ViewControllerB中的一个单元格时,弹出窗口被关闭。因此,在关闭弹出窗口(VC B)之前,我必须准备好要传递的数据。是的,在单击执行的位置收集数据。您只需调用[self-PerformsgueWithIdentifier:@“controllerB”发送者:self];在您收集数据之后。它将调用方法-(void)prepareforsgue:(UIStoryboardSegu