从弹出表视图返回 NSDictionary 数据
当我单击 ViewController1 上的按钮时,会出现一个弹出表格视图 (ViewController2)。然后,当我在表中选择一行时,我希望将这些值发送回 ViewController1。我设置了 NSDictionary。它在常规导航控制器中工作得很好,但尝试使用missModalViewControllerAnimated 来做到这一点,因此表视图会下降,并且数据会出现在第一个视图中。
我认为这与这里的问题类似: http ://www.iphonedevsdk.com/forum/iphone-sdk-development/39995-return-data-dismissmodalviewcontrolleranimated.html
这是我的代码:
ViewController1.h:
@protocol ViewController1Delegate;
@interface ViewController1 : UIViewController <ViewController2Delegate> {
id <ViewController1Delegate> delegate;
}
@property (nonatomic, assign) id <ViewController1Delegate> delegate;
@end
@protocol ViewController1Delegate
- (void)viewController1DidFinish:(ViewController1 *)controller;
@end
ViewController1.m:
-(void)buttonGoToViewController2 {
ViewController2 *controller = [[ViewController2 alloc] initWithNibName:@"ViewController2" bundle:nil];
// this controller.delegate = self causes it to crash if i have it uncommented for some reason...
// controller.delegate = self;
controller.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
[self presentModalViewController:controller animated:YES];
[controller release];
}
ViewController2.m:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
if(searching) {
// Navigation logic may go here. Create and push another view controller.
NSDictionary *selectedCountry = [self.copyListOfItems objectAtIndex:indexPath.row];
ViewController1 *dvController = [[ViewController1 alloc] initWithNibName:@"ViewController1" bundle:nil andDictionary: selectedCountry];
NSLog(@"selected hit this %@",selectedCountry);
// Pass the selected object to the new view controller.
[self.navigationController pushViewController:dvController animated:YES];
[dvController release];
[self dismissModalViewControllerAnimated:YES];
}
else {
// Navigation logic may go here. Create and push another view controller.
NSDictionary *dictionary = [self.listOfItems objectAtIndex:indexPath.row];
ViewController1 *dvController = [[ViewController1 alloc] initWithNibName:@"ViewController1" bundle:nil andDictionary: dictionary];
NSLog(@"normal hit this %@",dictionary);
// Pass the selected object to the new view controller.
[self.navigationController pushViewController:dvController animated:YES];
[dvController release];
}
}
I have a popup tableview (ViewController2) appearing when I click a button on ViewController1. Then when I select a row in the table, I want those values to be sent back to ViewController1. I have a NSDictionary set up. It works fine in a regular navigation controller, but trying to do it with dismissModalViewControllerAnimated, so the tableview drops back down, and the data is appearing in the first view.
This is similar to this question here I think: http://www.iphonedevsdk.com/forum/iphone-sdk-development/39995-return-data-dismissmodalviewcontrolleranimated.html
Here is my code:
ViewController1.h:
@protocol ViewController1Delegate;
@interface ViewController1 : UIViewController <ViewController2Delegate> {
id <ViewController1Delegate> delegate;
}
@property (nonatomic, assign) id <ViewController1Delegate> delegate;
@end
@protocol ViewController1Delegate
- (void)viewController1DidFinish:(ViewController1 *)controller;
@end
ViewController1.m:
-(void)buttonGoToViewController2 {
ViewController2 *controller = [[ViewController2 alloc] initWithNibName:@"ViewController2" bundle:nil];
// this controller.delegate = self causes it to crash if i have it uncommented for some reason...
// controller.delegate = self;
controller.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
[self presentModalViewController:controller animated:YES];
[controller release];
}
ViewController2.m:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
if(searching) {
// Navigation logic may go here. Create and push another view controller.
NSDictionary *selectedCountry = [self.copyListOfItems objectAtIndex:indexPath.row];
ViewController1 *dvController = [[ViewController1 alloc] initWithNibName:@"ViewController1" bundle:nil andDictionary: selectedCountry];
NSLog(@"selected hit this %@",selectedCountry);
// Pass the selected object to the new view controller.
[self.navigationController pushViewController:dvController animated:YES];
[dvController release];
[self dismissModalViewControllerAnimated:YES];
}
else {
// Navigation logic may go here. Create and push another view controller.
NSDictionary *dictionary = [self.listOfItems objectAtIndex:indexPath.row];
ViewController1 *dvController = [[ViewController1 alloc] initWithNibName:@"ViewController1" bundle:nil andDictionary: dictionary];
NSLog(@"normal hit this %@",dictionary);
// Pass the selected object to the new view controller.
[self.navigationController pushViewController:dvController animated:YES];
[dvController release];
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在 ViewController2 代码中,您将创建一个新的 ViewController1 对象并为其提供 NSDictionary。当您关闭 ViewController2 时,您将返回到原始 ViewController1,它从未去任何地方,也从未发送给 NSDictionary。您需要为 ViewController2 提供对呈现它的 ViewController1 对象的访问权限(我建议通过委托模式执行此操作),以便在那里设置 NSDictionary 。
In your ViewController2 code you are creating a new ViewController1 object and giving it your NSDictionary. When you dismiss ViewController2 you go back to the original ViewController1, which never went anywhere and was never sent the NSDictionary. You need to provide ViewController2 with access to the ViewController1 object that presented it (I suggest doing it via a delegate pattern) in order to set the NSDictionary there.