当我单击ViewController1上的按钮时,我出现了一个弹出式tableview(ViewController2)。然后,当我在表中选择一行时,我希望将这些值发送回ViewController1。我有一个NSDictionary设置。它在常规导航控制器中工作正常,但尝试使用dismissModalViewControllerAnimated,因此tableview下降,数据出现在第一个视图中。
这是我的代码:
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];
}
}
答案 0 :(得分:0)
在ViewController2代码中,您将创建一个新的ViewController1对象并为其提供NSDictionary。当您关闭ViewController2时,您将返回到原始 ViewController1,它从未到过任何地方,并且从未发送过NSDictionary。您需要为ViewController2提供对呈现它的ViewController1对象的访问权限(我建议通过委托模式进行)以便在那里设置NSDictionary。