故事板:' UIStoryboardSegue的子类必须覆盖-perform。'

时间:2014-12-04 13:39:31

标签: ios storyboard xcode6 uistoryboardsegue

我只想创建自定义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

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

1 个答案:

答案 0 :(得分:2)

我找到了解决方案。为了创建上面的自定义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类型动画并解决上述错误。