如何从视图的子视图的视图的自定义类中调用performSegueWithIdentifier?

时间:2015-04-03 17:48:44

标签: ios objective-c xcode6

我有一个视图控制器,它有一个使用自定义类的子视图。当我从视图控制器的上下文中执行performSegueWithIdentifier时,它工作正常,但是,如何在子子视图的上下文中调用performSegueWithIdentifier?

1 个答案:

答案 0 :(得分:3)

您无法从UIView调用performSegue。您可以使用协议,使viewController实现它并将其设置为自定义视图的委托。

<强> CustomView.h

@protocol CustomViewDelegate

-(void)customView:(CustomView *)view performSegueWithIdentifier:(NSString *)identifier;

@end

@property (nonatomic, weak) id<CustomViewDelegate> delegate;

<强> CustomView.m

// This is the event on which you would like to perform the segue
-(void)didClickButton {
     [self.delegate customView:self performSegueWithIdentifier:@"mySegue"];
}

<强> ViewController.m

-(void)viewDidLoad {
    // All you other stuff...

    // Set the delegate
    self.customView.delegate = self;
}

-(void)customView:(CustomView *)view performSegueWithIdentifier:(NSString *)identifier {
    [self performSegueWithIdentifier:identifier];
}

希望这会有所帮助。如果您有疑问,请告诉我