我只想创建自定义segue,所以我写了下面的代码。该代码似乎很好,但当我运行此代码时,它给出以下错误:

由于未捕获的异常而终止应用程序
“NSInternalInconsistencyException”,原因:“的子类
UIStoryboardSegue必须覆盖-perform。”

下面是代码:

#import "ViewController.h"
#import "Temp-2.h"
@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
 [super viewDidLoad];

 Temp_2 *toViewController = [self.storyboard instantiateViewControllerWithIdentifier:@"Temp2"];
 segue1=[[UIStoryboardSegue alloc] initWithIdentifier:@"temp" source:self destination:toViewController];
// Do any additional setup after loading the view, typically from a nib.
}

- (void)didReceiveMemoryWarning {
 [super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}

- (IBAction)actionPush:(id)sender {

 [self prepareForSegue:segue1 sender:sender];
 [segue1 perform];
}


-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
 if ([segue1.identifier isEqualToString:@"temp"])
 {
     [segue1.destinationViewController setStr:@"string passed"];

 }
}
 @end

任何人都可以帮助我,这段代码有什么问题。

最佳答案

我找到了解决方案。为了创建上述的自定义segue,我们需要将UIStoryBoardSegue类子类化,并覆盖perform方法。下面是我已经实现的代码。

#import "MyCustomSegue.h"

@implementation MyCustomSegue
- (void)perform
{
UIViewController *source = self.sourceViewController;
UIViewController *destination = self.destinationViewController;

UIWindow *window = source.view.window;

CATransition *transition = [CATransition animation];
[transition setDuration:1.0];
[transition setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut]];
[transition setType:kCATransitionPush];
[transition setSubtype:kCATransitionFromRight];
[transition setFillMode:kCAFillModeForwards];
[transition setRemovedOnCompletion:YES];


[window.layer addAnimation:transition forKey:kCATransition];
[window setRootViewController:destination];
}

这段代码将创建Push型动画,并解决上述错误。

07-27 13:40