2015-12-14 52 views
0

我想设置一个委托来处理基于Tinder类型示例(https://github.com/cwRichardKim/TinderSimpleSwipeCards)的子视图上的滑动操作。我在做什么我的ViewController子视图委托错了?

我打开我的背景观点:

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; 
... 

最后我DragableV IEW,这是我想从SwipeController到委托:

@protocol DraggableViewDelegate <NSObject> 

-(void)cardSwipedUp:(UIView *)card; 
-(void)cardSwipedLeft:(UIView *)card; 
-(void)cardSwipedRight:(UIView *)card; 

@end 

@interface DraggableView : UIView 

@property (weak) id <DraggableViewDelegate> delegate; 
+0

你得到的结果是什么,你不满意?如果有任何方法可以更清楚地解决问题,它将有助于诊断它。 – diatrevolo

+0

cardSwipedUp,cardSwipedLeft和cardSwipeRight没有在SwitchController委托中被触发。 – LeviXC

回答

1

SwipeViewController,你是里面DraggableViewBackground.m设置为代表的是你在本地创建一个不同的实例和获取方法后立即释放回报。

您需要为SwipeViewController添加一些方法来告诉DraggableViewBackground它希望成为DraggableView的代表。你可以一个setViewDelegate:方法添加到您的DraggableViewBackground并将它称之为setDelegate:DraggableView S,那么你就可以在SwipeViewController[draggableBackground setViewDelegate:self]DraggableViewBackground创建draggableBackground[draggableView setDelegate:self.viewDelegate]后,当它创建一个新DraggableView

0

想通了.. 。所以对于下一个可怜的灵魂..

在我的例子中,SwipeViewController是DraggableViewBACKGROUND视图的父代,并且该视图是DraggableView的父代。想像它将责任传递给它的父代理视图的每个步骤(卡>卡背景>刷卡控制器)。所以对于我的工作示例,我需要DraggableViewBACKGROUND来委托一些DraggableView,并且我需要SwipeViewController来委托DraggableViewBACKGROUND。

要做到这一点,那么

#import "DraggableViewBackground.h" 

@interface SwipeViewController : UIViewController <DraggableViewBackgroundDelegate> 

//methods called in DraggableViewBACKGROUND 
-(void)cardSwipedUp:(UIView *)card; 
-(void)cardSwipedLeft:(UIView *)card; 
-(void)cardSwipedRight:(UIView *)card; 

@end 

DraggableViewBACKGROUND了

@protocol DraggableViewBackgroundDelegate <NSObject> 

-(void)cardSwipedUp:(UIView *)card; 
-(void)cardSwipedLeft:(UIView *)card; 
-(void)cardSwipedRight:(UIView *)card; 

@end 


@interface DraggableViewBackground : UIView <DraggableViewDelegate> 

@property (weak) id <DraggableViewBackgroundDelegate> delegate; 

//methods called in DraggableView 
-(void)cardSwipedUp:(UIView *)card; 
-(void)cardSwipedLeft:(UIView *)card; 
-(void)cardSwipedRight:(UIView *)card; 

和DraggableView了

@protocol DraggableViewDelegate <NSObject> 

-(void)cardSwipedUp:(UIView *)card; 
-(void)cardSwipedLeft:(UIView *)card; 
-(void)cardSwipedRight:(UIView *)card; 

@end 

@interface DraggableView : UIView 

@property (weak) id <DraggableViewDelegate> delegate; 

因此,在每个代表我可以执行所需要的动作,然后调用它是家长代表。 DraggableViewBACKGROUND委托处理行动,这被称为从DraggableView以同样的方式..

-(void)cardSwipedUp:(UIView *)card; 
{ 
    NSLog(@"Swiped up"); 
    [delegate cardSwipedUp:self]; 
} 

该诀窍对我来说,作为DraggableViewBACKGROUND会处理弹出元素脱证阵列和收集,这不是其他数据DraggableView对象或SwipeViewController可访问。希望能帮助别人!