我想从 ViewControllerTwo 返回到 ViewControllerOne。我创建了一个负责执行此操作的按钮,但我的问题是该按钮是添加到 ViewControllerTwo 的自定义 UIView 类的一部分,该按钮不是 ViewControllerTwo 主视图的一部分。
所以在自定义 UIView 类中,我有一个在单击按钮时使用react的方法...
-(void)buttonClicked{
[SecondViewController performSegueWithIdentifier: "ShowFirstViewController" sender:nil];
}
当我这样做时,我得到一个错误:“performSegueWithIdentifier not a method of class”,这是有道理的。
那么我如何在两个 View Controller 之间进行切换,其中负责切换的按钮实际上不属于任何一个 View Controller ,而是在不同的类中。
最佳答案
我认为您可以让代表回拨您的 SecondViewController
并实现 performSegueWithIdentifier
在SecondViewController
中的委托(delegate)回调方法中.
它是这样的:
在您的自定义 UIView 类接口(interface)之上创建一个这样的协议(protocol)
@protocol CustomViewDelegate <NSObject>
- (void)buttonDidTap;
@end
然后在你的界面中创建一个属性
@property (nonatomic, weak) id <CustomViewDelegate> delegate;
在你的自定义 UIView *.m 中添加这个
-(void)buttonClicked{
[self.delegate buttonDidTap];
}
使协议(protocol)符合您的 SecondViewController
像这样
@interface SecondViewController: UIViewController <CustomViewDelegate>
在您的 viewDidLoadMethod
中设置委托(delegate)像这样
-(void)viewDidLoad{
[super viewDidLoad];
self.yourCustomView.delegate = self;
}
在 View Controller .m 文件中实现这个方法
- (void)buttonDidTap{
[self.performSegueWithIdentifier: "ShowFirstViewController" sender:self];
}
我比较敏捷,我认为这应该可以正常工作。
关于ios - 如何在自定义 UIView 中从一个 View Controller 返回到另一个 View Controller ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36340954/