ViewController1のボタンをクリックするとポップアップテーブルビュー(ViewController2)が表示されます。テーブル内の行を選択すると、その値をViewController1に戻す必要があります。私はNSDictionaryをセットアップしました。通常のナビゲーションコントローラでうまく動作しますが、dismissModalViewControllerAnimatedを使って操作しようとするので、tableviewが元に戻り、データが最初のビューに表示されます。ポップアップテーブルビューからNSDictionaryデータを返します
これは、ここで私が思うこの質問に似ています。ここでは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];
}
}
私のViewController2 didSelectRowAtIndexPathには、このようなものがありますか? \t 'ViewController1Delegate * appDelegate =(ViewController1Delegate *)[[UIApplication sharedApplication] delegate]; \t NSString * nodeText = [[listOfItems objectAtIndex:indexPath.row] objectForKey:@ "station"]; \t NSString * passThis = [appDelegate nodeText]; ' \t そして、私のViewController1には、このようなものがありますか? \t 'ViewController1Delegate * appDelegate =(ViewController1Delegate *)[[UIApplication sharedApplication] delegate]; \t PassedString = [appDelegate passThis]; ' – Aaron
いいえ、それほど複雑ではありません。このようなデリゲートとプロトコルに関するチュートリアルをチェックしてください:http://iphonedevelopertips.com/objective-c/the-basics-of-protocols-and-delegates.html –