Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/wix/2.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
Ios 如何使用performsgueforidentifier_Ios_Objective C_Segue_Uistoryboardsegue_Unwind Segue - Fatal编程技术网

Ios 如何使用performsgueforidentifier

Ios 如何使用performsgueforidentifier,ios,objective-c,segue,uistoryboardsegue,unwind-segue,Ios,Objective C,Segue,Uistoryboardsegue,Unwind Segue,我刚学过iOS和objective-c。我正在学习如何使用segue,尤其是放松segue 阅读时,我对“shouldPerformSegueForIdentifier”和“performSegueForIdentifier”的用法有点困惑 我创建了一个包含两个“ViewController”、“ViewController.m”的示例,如下面发布的代码“VC_1”和“ServiceViewController”所示 我的问题是: -何时以及如何使用“PerformsgueForIdentifi

我刚学过iOS和objective-c。我正在学习如何使用segue,尤其是放松segue

阅读时,我对“shouldPerformSegueForIdentifier”和“performSegueForIdentifier”的用法有点困惑

我创建了一个包含两个“ViewController”、“ViewController.m”的示例,如下面发布的代码“VC_1”和“ServiceViewController”所示

我的问题是:

-何时以及如何使用“PerformsgueForIdentifier”

-何时以及如何使用“shouldiperformsgueforidentifier”

VC_1:

img

prepareForSegue方法在segue执行之前被调用,并允许在ViewController之间传递数据。除此之外,您可以通过示例检查segue的标识符是否为XxX并传递一些数据,或者是否为方法的YYY调用

- (void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender 
{
    if ([segue.identifier isEqualToString:@"seguePassInterval"]) {
    ((TestViewController*)segue.destinationViewController).data = @"testData"; //passing data to destinationViewController of type "TestViewController"
    NSLog(@"SEGUE");
}
}
方法performsguewithidentifier用于执行segue,正如他的名字所说,使用他的标识符,您可以在需要时执行segue

最后,shouldPerformSegue用于避免在应用程序处于某种状态时执行segue,例如,如果您没有destinationViewController数据,但在获得该数据之前,您可以返回false


希望这有助于

对于shouldPerformSegueForIdentifier,确定是否应执行具有指定标识符的segue。对于performSegueForIdentifier,您可以使用它将数据从一个VC传递到另一个VC。请提供一个示例,说明如何使用shouldPerformSegueForIdentifier?当您自己调用performSegueWithIdentifier时,不会调用shouldPerformSegueWithIdentifier,因为可以假定您知道自己在做什么。调用performsguewithidentifier没有任何意义,但是从shouldPerformSegueWithIdentifier1st返回一个no。DestinationViewController未定义。2.由于无论启动segue的是按钮还是单元格,都会自动调用prepareForSegue,为什么我们需要PerformsGueforIdentifier?@user2121 TestViewController是一个示例,必须是destinationViewController类名,PerformsGueforIdentifier在调用prepareForSegue后执行segue,请检查我编辑的回答谢谢,但是我如何才能在destinationViewController@user2121您需要在destinationViewController中声明数据变量在我的示例中,您需要在TestViewController声明中将数据声明为字符串,因此,当调用prepareForSegue方法时,数据被传递,当TestViewController在数据中显示时,将在我的示例中传递值,字符串–testData–希望此帮助将“data”作为NSString*添加到destinationVieController.h中作为属性。但我收到上面发布的图像中显示的错误
- (void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender 
{
    if ([segue.identifier isEqualToString:@"seguePassInterval"]) {
    ((TestViewController*)segue.destinationViewController).data = @"testData"; //passing data to destinationViewController of type "TestViewController"
    NSLog(@"SEGUE");
}
}