gpt4 book ai didi

ios - UITouchUpInside 不调用委托(delegate)方法

转载 作者:行者123 更新时间:2023-11-29 04:05:00 25 4
gpt4 key购买 nike

我有一个 MYView 类(UIView 的子类),我将 MyView 添加到我当前的 ViewController 作为 subview 。 MyView 有一个按钮,如果 TouchUpInsideEvent 被触发,该按钮会调用委托(delegate)方法。我的问题是,当我点击按钮时,控件转到委托(delegate)方法,但如果我触发 TouchUpinsideEvent prgrammaticaaly,它不会调用委托(delegate)方法。这是我的代码:

MyView.h

@protocol MyViewDelegate;
@interface MyView : UIView
@property(nonatomic,assign)id<MyViewDelegate> delegate;

@protocol MyViewDelegate <NSObject>
- (void)selctedOption:(MyView *)slideView withOption:(UILabel *)option withIndex: (NSInteger *)labelIndex;

@end

MyView.m

toggleButton = [UIButton buttonWithType:UIButtonTypeCustom];
[toggleButton setTitle:@"" forState:UIControlStateNormal];


//adding target works fine it goes to the finishedDraggingVertical:withEvent method and from there I am calling the delegate method

[toggleButton addTarget:self action:@selector(finishedDraggingVertical:withEvent:)
forControlEvents:UIControlEventTouchUpInside];

toggleButton.frame = CGRectMake(frame.origin.x, frame.origin.y, frame.size.width, height);

toggleButton.backgroundColor=[UIColor colorWithRed:0.1 green:1.1 blue:0.3 alpha:0.2];
[toggleButton.layer setBorderWidth:10.0];
[toggleButton.layer setBorderColor:[[UIColor lightGrayColor] CGColor]];
toggleButton.layer.cornerRadius=10.0;



[self addSubview:toggleButton];
**//But If I fire the event programatically this doesn't work it goes to finishedDraggingVertical:withEvent but from there it does not go to the delegate method.**
[toggleButton sendActionsForControlEvents:UIControlEventTouchUpInside]

MyViewController.h

@interface MyViewController : UIViewController  <MyViewDelegate>

MyViewController.m

//这里我添加myView并实现委托(delegate)方法

- (void)viewDidLoad
{
[super viewDidLoad];
MyView *myiew1=[[MyView alloc]init];
myiew1.delegate=self;

[self.view addSubview:slideView1];
}

最佳答案

我知道这是一种解决方法,但是您是否尝试过以下问题的接受答案:

UIControl: sendActionsForControlEvents omits UIEvent

此外,请检查使用 [self actionsForTarget:target forControlEvent:controlEvent] 检索的操作是否与您之前在代码中设置的操作一致。如果没有,那么就有问题了。

关于ios - UITouchUpInside 不调用委托(delegate)方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15402927/

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