简体   繁体   English

如何向 UITextField & UITextView 添加方法?

[英]How to add a method to UITextField & UITextView?

I want to put something like this in a method for UITextField & UITextView .我想把这样的东西放在UITextField & UITextView的方法中。

- (void)changeKeyboardType:(UIKeyboardType)keyboardType {
    paymentTextView.keyboardType = UIKeyboardTypeAlphabet;
    [paymentTextView resignFirstResponder];
    [paymentTextView becomeFirstResponder];
}

How do I do this?我该怎么做呢? I know I can create categories for both UITextField & UITextView but is it possible to do it in one shot?我知道我可以为UITextFieldUITextView创建类别,但是可以一次性完成吗?

By one shot, I mean add it to both classes with one protocol instead of making two categories, one for UITextView & one for UITextField .一口气,我的意思是用一个协议将它添加到两个类中,而不是创建两个类别,一个用于UITextView ,一个用于UITextField I've heard a protocol is similar to a Ruby module, but in a Ruby module, I can implement the method.我听说一个协议类似于 Ruby 模块,但是在 Ruby 模块中,我可以实现该方法。 In a protocol, it only seems that I can declare the method but not implement it.在一个协议中,似乎我只能声明方法但不能实现它。 Can I also implement the method in the protocol, and then include this protocol in UITextField & UITextView ?我是否也可以实现协议中的方法,然后将此协议包含在UITextField & UITextView中?

How to add a method to an existing protocol in Cocoa? 如何向 Cocoa 中的现有协议添加方法? is close but not quite.接近但不完全。

What about something like this?像这样的事情呢?

// UIView+UITextInputTraits.h

@interface UIView (UITextInputTraits)
- (void)changeKeyboardType:(UIKeyboardType)keyboardType;    
@end


// UIView+Additions.m

#import "UIView+UITextInputTraits.h"

@implementation UIView (UITextInputTraits)

- (void)changeKeyboardType:(UIKeyboardType)keyboardType {
    if ([self conformsToProtocol:@protocol(UITextInputTraits)]) {
        id<UITextInputTraits> textInput = (id<UITextInputTraits>)self;
        if (textInput.keyboardType != keyboardType) {
            [self resignFirstResponder];
            textInput.keyboardType = keyboardType;
            [self becomeFirstResponder];
        }
    }
}

@end

For each of these, you can create a category.对于其中的每一个,您都可以创建一个类别。

Interface file:接口文件:

@interface UITextField (ChangeKeyboard)
- (void)changeKeyboardType:(UIKeyboardType)keyboardType;
@end

Implementation file:实现文件:

@implementation UITextField (ChangeKeyboard)
- (void)changeKeyboardType:(UIKeyboardType)keyboardType {
    self.keyboardType = keyboardType;
    [self resignFirstResponder];
    [self becomeFirstResponder];
}
@end

That would be the way to add these, but I haven't tested the functionality.这将是添加这些的方法,但我还没有测试过功能。

Like @Josh said, method swizzling isn't what you are looking for.就像@Josh 说的那样,方法调配不是您想要的。 However what I actually had in mind (My bad for not researching more into it before submitting an answer) is to add method at runtime on UITextView and UITextField.但是我真正想到的(我在提交答案之前没有对其进行更多研究是不好的)是在运行时在 UITextView 和 UITextField 上添加方法。 While this needs a bit more code to implement, it can give you the sort of one-shot you are looking for (You create a method and add it to both UITextView & UITextField at run-time)虽然这需要更多代码来实现,但它可以为您提供您正在寻找的那种一次性(您创建一个方法并在运行时将其添加到 UITextView 和 UITextField)

Here's a blog post about it:这是一篇关于它的博客文章:

http://theocacao.com/document.page/327 http://theocacao.com/document.page/327

http://www.mikeash.com/pyblog/friday-qa-2010-11-6-creating-classes-at-runtime-in-objective-c.html http://www.mikeash.com/pyblog/friday-qa-2010-11-6-creating-classes-at-runtime-in-objective-c.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将UI对象(图像)添加到UITextView或UITextField? - How to add UI Objects (Image) to a UITextView or UITextField? 在UITextfield或UITextview中会触发哪种委托方法? - Which delegate method will get fired in UITextfield or UITextview? 如何在没有UITextField或UITextView的情况下启动UIKeyboard? - How to pull up a UIKeyboard without a UITextField or UITextView? 如何在iPhone UITextView或UITextField中更改书写方向 - How to change the writing direction in iPhone UITextView or UITextField 如何获取当前活动的UITextField / UITextView和resignFirstResponder? - How to get the currently active UITextField/UITextView and resignFirstResponder? 如何在uilabel或uitextview中显示图标或图像(uitextfield) - how to display an icon or image in a uilabel or uitextview(uitextfield) 有没有一种方法可以使UITextView具有类似UITextField框的类似周围阴影? - Is there a method to get a UITextView with similar surrounding shadow like a UITextField box? 使用UITextField和UITextView时textFieldShouldReturn方法的问题 - Problem with textFieldShouldReturn method when using UITextField along with UITextView 如何在UITextView上添加省略号? - How to add ellipses on a UITextView? 如何将UILongPressGestureRecognizer添加到UITextField? - How to add UILongPressGestureRecognizer to a UITextField?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM