ios - 如何从 View Controller 的 subview 的自定义类中调用 performSegueWithIdentifier?

标签 ios objective-c xcode6

我有一个 View Controller ,它有一个使用自定义类的 subview 。当我从 View Controller 的上下文中执行 performSegueWithIdentifier 时,它工作正常,但是,如何从子 subview 的上下文中调用 performSegueWithIdentifier?

最佳答案

您不能从 UIView 调用 performSegue .您可以使用协议(protocol),让 viewController 实现它并将其设置为自定义 View 的委托(delegate)。

自定义 View .h

@protocol CustomViewDelegate

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

@end

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

自定义 View .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];
}

希望这可以帮助。如果您有任何问题,请告诉我

关于ios - 如何从 View Controller 的 subview 的自定义类中调用 performSegueWithIdentifier?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29437209/

相关文章:

ios - 实例化模态视图 Controller 会卡住应用程序 - Xcode 7 可能存在问题

ios - iOS8 popover 中的 setContentView Controller 方法导致应用程序崩溃

c++ - 你如何在 Xcode 6 中更新文件路径

ios - 将尝试通过打破约束来恢复 <NSLayoutConstraint :0x17008b130 UILabel

ios - 如何检测照片是否是用 iPhone 拍摄的

iOS 音频文件无法在桌面网络浏览器中播放

iphone - UITableViewCell 附件 Checkmarx 问题,许多单元格仅通过检查一行来检查

objective-c - 准备不使用 swift 的 segue

objective-c - 播放声音与显示的图像一一对应

iOS Objective C substringToIndex 崩溃