我的ViewController子视图委托我做错了什么?

时间:2015-12-14 20:53:58

标签: ios objective-c delegates

我尝试设置委托来处理基于Tinder类型示例(https://github.com/cwRichardKim/TinderSimpleSwipeCards)的子视图上的滑动操作。

我加载了我的背景视图:

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委派的内容:

@protocol DraggableViewDelegate <NSObject>

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

@end

@interface DraggableView : UIView

@property (weak) id <DraggableViewDelegate> delegate;

2 个答案:

答案 0 :(得分:1)

您在SwipeViewController中设置为委托的DraggableViewBackground.m是您在本地创建的不同实例,并且在该方法返回后立即取消分配。

您需要为SwipeViewController添加一些方法,告诉DraggableViewBackground它希望成为DraggableView的代理人。您可以向setViewDelegate:添加DraggableViewBackground方法,并在setDelegate:上呼叫DraggableView,然后您可以在[draggableBackground setViewDelegate:self]中执行SwipeViewControllerdraggableBackground创建新[draggableView setDelegate:self.viewDelegate]

后{}创建DraggableViewBackgroundDraggableView

答案 1 :(得分:0)

想出来......所以对于下一个可怜的灵魂......

在我的示例中,SwipeViewController是DraggableViewBACKGROUND视图的父级,该视图是DraggableView的父级。想想它就像在视图的每一步将责任传递给它的父代表一样(Card&gt; Card背景&gt;滑动控制器)。所以对于我的工作示例,我需要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无法访问的其他数据。希望能帮到别人!

相关问题