簡體   English   中英

UIButton在uitouch事件上崩潰

[英]UIButton crashes on uitouch event

我有一個UIButton,將draginside事件發送到:

-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {

它通過viewDidLoad中的以下代碼完成此操作:

[colourButton1 addTarget:self action:@selector(touchesBegan:withEvent:) forControlEvents:UIControlEventTouchDown];

在它發送到的方法內,有以下行:

UITouch *myTouch = [touches anyObject];

並且由於某種原因,當在UIButton中拖動時,這會使應用程序崩潰。 有什么想法嗎?

編輯:解決方案。

-(IBAction)buttonDragged:(id)button withEvent:(UIEvent*)event {
    NSSet *touches = [event touchesForView:colourButton1];
    [self touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event];
}

當您添加目標以進行控制時,您可以通過3種選擇器進行操作-

- (void)action
- (void)action:(id)sender
- (void)action:(id)sender withEvent:(UIEvent*)event

名稱無關緊要,重要的是參數的數量。 如果控件向目標發送一條帶有2個參數的消息,則第一個參數將是控件本身(在您的情況下為UIButton的實例),第二個參數為UIEvent一個實例。 但是您希望將NSSet實例作為第一個參數, anyObject其發送一條消息, UIButton無法理解。 這是導致崩潰的原因。

您為什么首先嘗試將事件從UI控件發送到觸摸處理方法touchesMoved:withEvent: 它可能會執行與您的意思不同的操作。

更新:

- (void)touchesMoved:(NSSet*)touches withEvent:(UIEvent *)event {
    UITouch *t = [touches anyObject];
    CGPoint touchLocation = [t locationInView:self.view];
    NSLog(@"%@", NSStringFromCGPoint(touchLocation));
}


- (IBAction)buttongDragged:(id)button withEvent:(UIEvent*)event {
    NSSet *touches = [event touchesForView:button];
    [self touchesMoved:touches withEvent:event];
}

請注意,由於touchesMoved:withEvent:UIResponder的方法,而控制器的視圖 UIResponder因此也會在此視圖的觸摸事件上UIResponder此方法。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM