UIScrollView没有响应代理 [英] UIScrollView not responding to delegates

查看:229
本文介绍了UIScrollView没有响应代理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我以前用我的 UIView 和几个代表工作得很好,其中一些如下。现在我改变了 UIView IB 成为 UIScrollView (现在用作主视图)。

I used to have my UIView that worked fine with several delegates some of them below. Now I changed that UIView from IB to be a UIScrollView (Now used as main view).

由于我已更改为 UIScrollView 我的事件委托不再工作了。例如键盘,我也有一个元素,我可以移动,而不是它是静态的。

Since I've changed to UIScrollView my event delegates such as those below dont work anymore. Such as keyboard and also I had an element that I could move around and not it is just static.

我分配了所有的委托,形成IB,我可以想到,大多数事情,我知道。但是我的想法为什么事件没有被触发....

I assigned all delegates form the IB that I could think of and did most things that I know. but i am running out of ideas on why the events are not getting triggered....

如果我回到旧的 UIView 通过 cmd + z 操作。

任何人都能指向正确的方向吗? ?

Could anyone point me in the right direction??

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

   [self.view endEditing:TRUE];
}
- (BOOL)textViewShouldBeginEditing:(UITextView *)textView { 
    // register for keyboard notifications

    return YES;
}

EDIT - 补充答案

@Wezly 答案是完全有效的。
但是如果任何人不想子类化 UIScrollView 并且只使用 UITextFieldDelegate 方法。

@Wezly Answer is totally valid. But if anyone doesn't want to subclass the UIScrollView and use only UITextFieldDelegate methods.

另一种方式是添加到 viewDidLoad

注意:您仍然无法访问许多内容,但它仍然是另一种解决方法。

Note: you still cant access many things, but its another workaround

///
/// DelegateNotifications
[[NSNotificationCenter defaultCenter] addObserver:self
                                         selector:@selector(keyboardWillShow:)
                                             name:UIKeyboardWillShowNotification
                                           object:self.view.window];
// register for keyboard notifications
[[NSNotificationCenter defaultCenter] addObserver:self
                                         selector:@selector(keyboardWillHide:)
                                             name:UIKeyboardWillHideNotification
                                           object:self.view.window];
[[NSNotificationCenter defaultCenter] addObserver:self
                                         selector:@selector(textFieldDidEndEditing:)
                                             name:UITextFieldTextDidEndEditingNotification
                                           object:self.view.window];


推荐答案

我建议你子类化UIScrollView对象,触摸内部的事件像下面..

I suggest that you subclass your UIScrollView object and add your touch events inside of it like below..

canvasObject.h

canvasObject.h

#import <UIKit/UIKit.h>

@interface canvasObject : UIScrollView 

@end

canvasObject.m

canvasObject.m

#import "canvasObject.h"

@implementation canvasObject

- (id)initWithFrame:(CGRect)frame
{
  self = [super initWithFrame:frame];
  if (self) {
    self.canCancelContentTouches = false;
  }
  return self;
}

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *) event
{
  //Do Stuff
}

- (BOOL)textViewShouldBeginEditing:(UITextView *)textView { 
  return YES;
}

@end

$ c> UIScrollView 使用接口构建器中的身份检查器到新的滚动视图子类,如下所示。

Then link your UIScrollView using the identity inspector in interface builder to the new scroll view subclass like below..

这篇关于UIScrollView没有响应代理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆