简体   繁体   中英

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?

You can't call performSegue from an UIView . You can use a protocol, make the viewController implement it and set it as delegate for your custom view.

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];
}

Hope this helps. Let me know if you have questions

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM