如何将数据从FormSeetController传递到ViewController?

时间:2014-07-27 15:58:35

标签: ios objective-c uiviewcontroller delegates objective-c-blocks

我试图使用这个包:

https://github.com/m1entus/MZFormSheetController

现在我的问题是,我在解除formSheet后尝试在FormSeetController和ViewController之间传递数据。

这是我到目前为止所尝试的: 在按钮中的ViewController中创建表单:

formSheet.didDismissCompletionHandler = ^(UIViewController *vc){
// maybe i can use UIViewController *vc as my SearchGardenView (form Sheet), but how?

    [self formSheetDidDismissed]; // here i tryin to get the kidGardenID but its always nil;
};

,这是来自FormSeetController:

    AddKid *kid = [[AddKid alloc]init];
    NSArray *temp = [_responseDict valueForKey:@"ID"];
    kid.kidGardenID = [temp objectAtIndex:indexPath.row];

    [self mz_dismissFormSheetControllerAnimated:YES completionHandler:^(MZFormSheetController *formSheetController) {

      //this block dismiss the formSheet.
}];

我需要将'kidGardenID'传递给ViewController,但即使在我给它一个值之后它总是为零,如上面的代码所示.. 也许是因为ViewController已经加载了,当我从fortSheet回来时它再次没有加载?

我需要找到一种传递数据的方法, 你可以帮帮我吗?

修改 我尝试使用委托没有成功,代理没有被调用。 这是我的委托代码:

设置委托(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是其代表:

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

设置我想要从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;

}

2 个答案:

答案 0 :(得分:1)

您可以尝试使用willDismissCompletionHandler。这是在MZFormSheetController.h文件中定义的。类型为showViewController参数给你的视图控制器(我假设是AddKid),你可以在这里访问AddKit类的属性。

@property (nonatomic, copy) MZFormSheetCompletionHandler willDismissCompletionHandler;

formsheet.willDismissCompletionHandler = ^(UIViewController *presentedViewController){
((AddKid*)presentedViewController).kidGardenID // should be accessible now.

};

答案 1 :(得分:0)

为了将详细信息传递回父视图控制器,您应该设置一个委托协议,该协议将允许您正在查找的内容。

如果您之前没有创建过任何委托协议,那么一旦您阅读了本教程并使用了几次,您就会发现它经常使用它。

有很多很好的教程,虽然这是一个很好的起点

Create your own Objective-C Delegate Protocol

希望这有帮助