简体   繁体   English

“接口”构建器上的“自定义格式化程序”的用途是什么?

[英]What is the purpose of the Custom Formatter on Interface builder?

Interface builder has this Custom Formatter that can be dragged to text fields. “界面”构建器具有此自定义格式化程序,可以将其拖动到文本字段。

在此输入图像描述

But this thing has no properties at all and the documentation, as typical with Apple, is non-existent. 但是这个东西根本就没有属性,而Apple的典型文档也不存在。

I need to create a formatter than can accept numbers, texts from the alphanumeric set and underscore and reject everything else. 我需要创建一个格式化程序,而不是接受数字,来自字母数字集和下划线的文本,并拒绝其他所有内容。

I suspect this Custom Formatter is what I need but how do I use that? 我怀疑这个自定义格式化器是我需要的,但我该如何使用它? Or is that possible to do what I need using regular formatters existent on interface builder? 或者可以使用接口构建器上存在的常规格式化程序来执行我需要的操作吗?

Can you give an example using interface builder? 你能举一个使用界面构建器的例子吗?

Thanks. 谢谢。

The NSFormatter class is an abstract class, so you need to subclass it. NSFormatter类是一个抽象类,因此您需要对其进行子类化。 On that you need to implement the following methods. 在那你需要实现以下方法。

- (BOOL)isPartialStringValid:(NSString *)partialString newEditingString:(NSString **)newString errorDescription:(NSString **)error;
- (NSString *)stringForObjectValue:(id)obj;
- (BOOL)getObjectValue:(out id *)obj forString:(NSString *)string errorDescription:(out NSString **)error;

Create a subclass like: 创建一个子类,如:

.h 。H

@interface MyFormatter : NSFormatter

@end

.m .M

@implementation MyFormatter

- (BOOL)isPartialStringValid:(NSString *)partialString newEditingString:(NSString **)newString errorDescription:(NSString **)error
{
    // In this method you need to write the validation
    // Here I'm checking whether the first character entered in the textfield is 'a' if yes, It's invalid in my case.
    if ([partialString isEqualToString:@"a"])
    {
        NSLog(@"not valid");
        return false;
    }
    return YES;
}

- (NSString *)stringForObjectValue:(id)obj
{
    // Here you return the initial value for the object
    return @"Midhun";
}

- (BOOL)getObjectValue:(out id *)obj forString:(NSString *)string errorDescription:(out NSString **)error
{
    // In this method we can parse the string and pass it's value (Currently all built in formatters won't support so they just return NO, so we are doing the same here. If you are interested to do any parsing on the string you can do that here and pass YES after a successful parsing
    // You can read More on that here: https://developer.apple.com/library/mac/documentation/Cocoa/Reference/Foundation/Classes/NSFormatter_Class/index.html#//apple_ref/occ/instm/NSFormatter/getObjectValue:forString:errorDescription:
    return NO;
}
@end

That object is mostly useful for OS X. On OS X, you can attach formatters to controls/cells. 该对象主要用于OS X.在OS X上,您可以将格式化程序附加到控件/单元格。 In iOS, I suppose you can add a formatter to a NIB or storyboard and connect an outlet to it, but there's not much advantage to that over creating it programmatically. 在iOS中,我认为您可以将格式化程序添加到NIB或故事板并将其连接到它,但是以编程方式创建它并没有太大的优势。

In particular, the generic Custom Formatter is for when you want to add an instance of a custom subclass of NSFormatter . 特别是,通用的自定义格式化程序适用于您想要添加NSFormatter的自定义子类的NSFormatter You drag in the Custom Formatter to the relevant control/cell, and then set its class in the Identity inspector. 您将自定义格式化程序拖动到相关的控件/单元格,然后在标识检查器中设置其类。

If that object weren't available in the object library, you could only drag in instances of specific formatter classes (eg NSNumberFormatter ). 如果该对象在对象库中不可用,则只能拖动特定格式化程序类的实例(例如NSNumberFormatter )。 You would be able to set the class of the resulting instance, but only to a subclass of that specific formatter class. 您可以设置结果实例的类,但只能设置该特定格式化程序类的子类。

If you need to learn how to write a custom formatter class, consult the class reference for NSFormatter and the Data Formatting Guide . 如果您需要学习如何编写自定义格式化程序类,请参阅NSFormatter的类参考和数据格式指南

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM