ios - 我的 ViewController Subview Delegate 做错了什么?

标签 ios objective-c delegates

我正在尝试设置一个委托(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/

相关文章:

ios - 从 View 模型结构中获取值

iphone - Objective-C 中的弱属性和强属性 setter 属性

html - 如何从 iOS 中的文件中解析 JSON?

objective-c - 从 UITableView 标题部分设置 UILabel 文本

swift - 自定义委托(delegate)不会从 ContainerView 执行

objective-c - 委托(delegate)问题

ios - imageWithCG图像根据仪器泄漏

ios - 这是哪个对象?

objective-c - Cocoa PDFView 不调整大小

objective-c - 在委托(delegate)调用中丢失属性地址