gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 01:23:45 26 4
gpt4 key购买 nike

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

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com