Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/117.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Objective c 使用委托通过多个视图控制器传回数据_Objective C_Ios_Delegation - Fatal编程技术网

Objective c 使用委托通过多个视图控制器传回数据

Objective c 使用委托通过多个视图控制器传回数据,objective-c,ios,delegation,Objective C,Ios,Delegation,在IOS中,我非常熟悉在视图之间传递数据,这些视图直接分为使用prepareforsegue向前传递数据和委托向后传递数据 我遇到的问题是,我正在构建一个通过4个视图进行分段的应用程序,然后当用户在第四个视图上点击enter键时,我弹出其余视图以返回到第一个视图控制器及其视图,但我不知道如何将数据委托回第一个视图 我认为问题在于在第一个视图控制器中设置代理。我无法像通常使用segue.destinationviewcontroller那样设置它,因为该视图控制器尚不存在。我应该把它放在别的地方吗

在IOS中,我非常熟悉在视图之间传递数据,这些视图直接分为使用prepareforsegue向前传递数据和委托向后传递数据

我遇到的问题是,我正在构建一个通过4个视图进行分段的应用程序,然后当用户在第四个视图上点击enter键时,我弹出其余视图以返回到第一个视图控制器及其视图,但我不知道如何将数据委托回第一个视图


我认为问题在于在第一个视图控制器中设置代理。我无法像通常使用segue.destinationviewcontroller那样设置它,因为该视图控制器尚不存在。我应该把它放在别的地方吗?这样做的正确方法是什么?

< P>而不是使用委托来传递数据,请考虑在这种情况下使用视图控制器之间的通信。 在第一个视图控制器中,您将注册以侦听通知:

- (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

}
在first view controller的dealloc方法中,确保取消注册为观察员(以避免潜在的崩溃):

然后,在第四个视图控制器中,按下enter按钮时广播通知:

// 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];

代替使用委托来传递数据,请考虑在这种情况下使用视图控制器之间的通信。

在第一个视图控制器中,您将注册以侦听通知:

- (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

}
在first view controller的dealloc方法中,确保取消注册为观察员(以避免潜在的崩溃):

然后,在第四个视图控制器中,按下enter按钮时广播通知:

// 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];

这非常有效,谢谢!我只有一个问题——我正在使用自动引用计数。我只使用过ARC,我不太熟悉它们之间的区别。我需要担心dealloc方法吗,还是这是自动的?我还有别的事要做吗?再次感谢!如果您正在使用ARC,只需删除对
[super dealloc]
的调用即可。编译器会自动为您添加。这非常有效,谢谢!我只有一个问题——我正在使用自动引用计数。我只使用过ARC,我不太熟悉它们之间的区别。我需要担心dealloc方法吗,还是这是自动的?我还有别的事要做吗?再次感谢!如果您正在使用ARC,只需删除对
[super dealloc]
的调用即可。编译器会自动为您添加这些内容。