简体   繁体   English

当点击带有手势的子视图时,如何选择另一个视图控制器?

[英]How to segue to another view controller when a subview with a gesture is tapped?

I have a view controller with a subview: gestureView that is referenced by the view controller via an outlet. 我有一个带有子视图的视图控制器:gestureView,该视图控制器是通过插座引用的。 I have assigned a tap gesture from viewDidLoad of the parent view controller to gestureView like: 我已经将父视图控制器的viewDidLoad中的轻击手势分配给了gestureView,如下所示:

UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self.gestureView action:@selector(handleTap)];
[self.gestureView addGestureRecognizer:tapGesture];

gestureView uses a custom class where handleTap: is implemented and it works fine, I can log from gestureView's handleTap:, however how can I perform a segue from the current view controller to another view controller when gestureView is tapped. gestureView使用自定义类,其中实现了handleTap:,并且工作正常,我可以从gestureView的handleTap:进行日志记录,但是,当轻按gestureView时,如何执行从当前视图控制器到另一个视图控制器的排序。

I understand that there may be easier ways to do this, but this works best for my situation. 我了解可能有更简单的方法来执行此操作,但这对我的情况最有效。

You can use Delegate pattern or Notification pattern. 您可以使用代理模式或通知模式。 Both will work in your case, As -handleTap() method is implemented in your subview you can create protocol in your subview class . 两种情况都可以使用,因为-handleTap()方法在子视图中实现,因此可以在子视图类中创建协议 And when you get handleTap method you can call delegate method which should be implemented in your parent class. 当您获得handleTap方法时,您可以调用应该在您的父类中实现的委托方法。

In your subview class Above class declaration define protocol 在您的子视图类中, 在类声明上方定义协议

@protocol SubViewClassDelegate <NSObject>
@required;
- (void) subviewDidTapped:(id) sender;
@end

Define property of delegate. 定义委托的属性。

@property (nonatomic, unsafe_unretained) id<SubViewClassDelegate> delegate;

In implementation file of subview's class call delegate method. 在子视图的类调用委托方法的实现文件中。

- (void) handleTap   {
        if ([self.delegate respondsToSelector:@selector(subviewDidTapped:)]) {
[self.delegate subviewDidTapped:yourObject]; // in yourObject you can pass data if require.
}
}

In your parent class implemented delegate of subview class 在您的父类中实现的子视图类的委托

@interface ParentViewController : UIViewController <SubViewClassDelegate>

In viewDidLoad viewDidLoad中

self.gestureView.delegate = self;

And Implement the delegate method of Subview class in Parent controller. 并在Parent控制器中实现Subview类的委托方法。

-(void) subviewDidTapped:(id)sender {

 // Navigate using performSegueWithIdentifier:
[self performSegueWithIdentifier: @"segueIdentifier" sender: self];
 }

In the handleTap , perform a segue to the other view controller you intend to display next. handleTap ,对接下来要显示的另一个视图控制器执行搜索。

 [self performSegueWithIdentifier: @"YOU_SEGUE_IDENTIFIER" sender: self];

The segue that you will perform is the segue in the storyboard that is from current view controller to the next view controller. 您将要执行的Segue是情节提要中从当前视图控制器到下一个视图控制器的序列。 (You need to create this). (您需要创建此)。

You can pass parameters on to the next view controller in the prepareForSegue:sender: , once you identify that it is the correct sender by checking the segue identifier. 一旦通过检查segue标识符确定它是正确的发送者,就可以将参数传递给prepareForSegue:sender:的下一个视图控制器。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何从另一个控制器加载xib文件而没有segue和不作为子视图 - How to load a xib file from another controller without segue and not as a subview 当点击表视图内的元素时,如何转到另一个视图控制器? - How to go to another View Controller when an element inside table view is being tapped? 当我轻按任何集合视图单元格时(不点击最后一个单元格),如何移动另一个视图控制器? - How to move another view controller when i tapped any collection view cell (without taping last cell)? 如何在swift中将数组转换为另一个视图控制器? - How to segue array to another view controller in swift? IOS如何将一个数组转移到另一个视图控制器 - IOS how to segue an array to another view controller 如何在选择时“取消” AVAudioSession到另一个视图控制器? - How To “Cancel” AVAudioSession on segue to another view controller? 在外面点击时从另一个子视图中删除一个子视图 - removing a subview from another subview when tapped outside 在另一个视图控制器中将视图控制器添加为子视图 - Add a view controller as a subview in another view controller 添加视图 controller 作为另一个视图中的子视图 controller - Adding a view controller as a subview in another view controller 如何通过单击视图来切换到另一个视图控制器? - How can I segue to another view controller by clicking a view?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM