ios - 如何在自定义 UIView 中从一个 View Controller 返回到另一个 View Controller ?

标签 ios objective-c uiview segue

我想从 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并实现 performSegueWithIdentifierSecondViewController中的委托(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/

相关文章:

ios - NSPredicate 无法解析格式字符串

ios - 如何使用 segue 进行导航?

ios - EXC_BAD_ACCESS中的code参数是什么意思?

ios - Swift 中的 NSDictionary :Cannot subscript a value of type 'AnyObject?' with a index of type 'Int'

ios - 如何将 UIActivity Indicator 添加到每个自定义 UItableviewCell

ios - 渐变不填充 IBDesignable UIView

objective-c - 调整 UIView 的大小

iphone - 从 UIView 推送 UIViewController

ios - FBFriendPickerViewController 在 iOS 上显示和空表。 Place Picker 运行良好

objective-c - 处理线程中变量的问题