[英]How to call performSegueWithIdentifier from within a custom class of a view that's a subview of view controller?
I have a view controller that has a subview that uses a custom class. 我有一个视图控制器,它有一个使用自定义类的子视图。 When I perform performSegueWithIdentifier from the context of the view controller, it works fine, however, how can I call performSegueWithIdentifier from within the context of child subview?
当我从视图控制器的上下文中执行performSegueWithIdentifier时,它工作正常,但是,如何在子子视图的上下文中调用performSegueWithIdentifier?
You can't call performSegue from an UIView
. 你不能从
UIView
调用performSegue。 You can use a protocol, make the viewController implement it and set it as delegate for your custom view. 您可以使用协议,使viewController实现它并将其设置为自定义视图的委托。
CustomView.h CustomView.h
@protocol CustomViewDelegate
-(void)customView:(CustomView *)view performSegueWithIdentifier:(NSString *)identifier;
@end
@property (nonatomic, weak) id<CustomViewDelegate> delegate;
CustomView.m 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 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];
}
Hope this helps. 希望这可以帮助。 Let me know if you have questions
如果您有疑问,请告诉我
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.