我正在尝试设置一个委托(delegate)来处理基于 Tinder 类型示例 ( https://github.com/cwRichardKim/TinderSimpleSwipeCards ) 的 subview 上的滑动操作。
我加载我的背景 View :
SwipeController.h
#import "DraggableView.h"
@interface SwipeViewController : UIViewController <DraggableViewDelegate>
//methods called in DraggableView
-(void)cardSwipedUp:(UIView *)card;
-(void)cardSwipedLeft:(UIView *)card;
-(void)cardSwipedRight:(UIView *)card;
@end
SwipeViewController.m
....
// since we have all the data, lets show some cards
DraggableViewBackground *draggableBackground = [[DraggableViewBackground alloc]initWithFrame:self.view.frame];
[draggableBackground setupViewWithImages:_results];
[self.view addSubview:draggableBackground];
....
-(void)cardSwipedUp:(UIView *)card;
{
NSLog(@"Swiped up");
}
-(void)cardSwipedLeft:(UIView *)card;
{
NSLog(@"Swiped Left");
}
-(void)cardSwipedRight:(UIView *)card;
{
NSLog(@"Swiped Right");
}
...
DragableBackground.m
...
// assign the delegate
SwipeViewController *swipeViewController = [[SwipeViewController alloc] initWithNibName:nil bundle:nil];
draggableView.delegate = swipeViewController;
return draggableView;
...
最后是我的 DragableView,这是我试图从 SwipeController 委托(delegate)的内容:
@protocol DraggableViewDelegate <NSObject>
-(void)cardSwipedUp:(UIView *)card;
-(void)cardSwipedLeft:(UIView *)card;
-(void)cardSwipedRight:(UIView *)card;
@end
@interface DraggableView : UIView
@property (weak) id <DraggableViewDelegate> delegate;
最佳答案
SwipeViewController
您在 DraggableViewBackground.m
中设置为委托(delegate)是您在本地创建的另一个实例,该实例在该方法返回后立即被释放。
您需要为 SwipeViewController
添加一些方法告诉DraggableViewBackground
它想成为 DraggableView
的委托(delegate)。您可以添加 setViewDelegate:
DraggableViewBackground
的方法并让它调用 setDelegate:
在 DraggableView
上s,那么你可以做 [draggableBackground setViewDelegate:self]
在SwipeViewController
创建 draggableBackground
之后和 [draggableView setDelegate:self.viewDelegate]
在DraggableViewBackground
当它创建一个新的 DraggableView
关于ios - 我的 ViewController Subview Delegate 做错了什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34276392/