當我單擊ViewController1上的一個按鈕時,出現一個彈出式tableview(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 –