简体   繁体   English

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

[英]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.

相关问题 从自定义类调用视图控制器方法 - Call view controller method from custom class 如何从情节提要中的子视图(另一个视图控制器)内的按钮推动新的视图控制器 - How can I push a new View Controller from a button within a subView (another View Controller) in Storyboard 如何从自定义视图控制器类加载初始视图控制器 - How to load an Initial View Controller from Custom View controller class 如何将子ViewController的视图添加到父View控制器的子视图? - How to add child ViewController's view to subview of the parent View controller? performSegueWithIdentifier未按预期显示视图控制器 - performSegueWithIdentifier not showing view controller as expected 将另一个视图控制器的视图添加为子视图 - Adding another view controller's view as subview 从tableView委托类中调用视图控制器segue - call view controller segue from within tableView delegate class 子视图中的导航视图控制器 - Navigation View Controller from Subview 将子视图控制器的视图添加到父视图控制器的子视图 - Add a child view controller's view to a subview of the parent view controller 如何在自定义视图类中动态替换自定义子视图? - How to replace a custom subview dynamically in a custom view class?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM