在这种情况下,考虑使用NSNotificationCenter在视图控制器之间进行通信,而不是使用委托来传递数据。
在您的第一个视图控制器中,您将注册以侦听通知:
- (void)viewDidLoad
{
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(handleFourthViewSubmit:)
name:@"fourthViewSubmit"
object:nil];
}
并创建发送通知时要运行的方法:
- (void)handleFourthViewSubmit:(NSNotification *)notification {
NSDictionary *theData = [notification userInfo]; // theData is the data from your fourth view controller
// pop views and process theData
}
在您的第一个视图控制器的 dealloc 方法中,请务必取消注册为观察者(以避免潜在的崩溃):
-(void) dealloc {
[[NSNotificationCenter defaultCenter] removeObserver:self];
[super dealloc];
}
然后在您的第四个视图控制器中,在按下回车按钮时广播通知:
// note: dataDict should be an NSDictionary containing the data you want to send back to your first view controller
[[NSNotificationCenter defaultCenter] postNotificationName:@"fourthViewSubmit"
object:self
userInfo:dataDict];