简体   繁体   English

ios添加自定义inputView

[英]ios adding custom inputView

I'm stuck on a concept in iOS that I can't seem to understand, no matter how much I read about it. 无论我读了多少,我都坚持使用iOS中的一个概念,我似乎无法理解。 I'm trying to override the standard iOS number pad with a custom design. 我试图用自定义设计覆盖标准iOS数字键盘。 When the user touches the UITextField, I want the custom inputView to reveal instead of the standard number pad. 当用户触摸UITextField时,我希望显示自定义inputView而不是标准数字键盘。

I created an separate .h/.m/.xib ViewController class for my custom inputView called "customInputViewController" Right now, it's just a dark background and one button that obscures about half of the screen when the UITextField is touched (similar to the number pad, but it just looks different). 我为我的自定义inputView创建了一个单独的.h / .m / .xib ViewController类,名为“customInputViewController”现在,它只是一个黑暗的背景和一个按钮,当触摸UITextField时,它会遮挡大约一半的屏幕(类似于数字垫,但它看起来不同)。 My implementation fails when I click the one button in my custom inputView -- iOS throws an EXC_BAD_ACCESS error. 当我单击自定义inputView中的一个按钮时,我的实现失败 - iOS会抛出EXC_BAD_ACCESS错误。

This is how I load the .xib file at runtime and attach the custom inputView to the UITextField object: 这是我在运行时加载.xib文件并将自定义inputView附加到UITextField对象的方法:

UIViewController *v = [[customInputViewController alloc] initWithNibName:@"customInputDesign" bundle:nil];
myTextInput.inputView = v.view;

In the .xib file of the custom inputView, I set the File's Owner to be "customInputViewController" and I created an (IBAction) method and attached it to a UIButton. 在自定义inputView的.xib文件中,我将File的Owner设置为“customInputViewController”,然后创建了一个(IBAction)方法并将其附加到UIButton。 When that button is clicked, the (IBAction) is set up to send an NSLog(@"Button Clicked") message. 单击该按钮时,(IBAction)设置为发送NSLog(@“按钮单击”)消息。 Nothing special. 没什么特别的。 It's just a simple boilerplate implementation that continues to throw an error. 它只是一个简单的样板实现,继续引发错误。

Maybe I'm doing this entirely wrong. 也许我这样做完全错了。 Can anyone provide a simple example? 有谁可以提供一个简单的例子?

The view v.view is retained as the inputView property is defined as (readwrite, retain). 视图v.view被保留,因为inputView属性被定义为(readwrite,retain)。 However, if you release your customInputViewController v somewhere before the input button is clicked, you will get a crash (EXC_BAD_ACCESS) 但是,如果在单击输入按钮之前在某处释放customInputViewController v,则会出现崩溃(EXC_BAD_ACCESS)

You can try this in your main controller: 您可以在主控制器中尝试:

- (IBAction) keyboardButtonClicked
{
    NSLog(@"keyboard Button Clicked");
}

- (void) viewDidLoad
{
    // do your stuff here ...

    UIView *v = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 100)]; // add autorelease if you don't use ARC
    v.backgroundColor = [UIColor darkGrayColor];
    UIButton *b = [UIButton buttonWithType:UIButtonTypeCustom];
    [b setTitle:@"Test button" forState:UIControlStateNormal];
    [b addTarget:self action:@selector(keyboardButtonClicked) forControlEvents:UIControlEventTouchUpInside];
    b.frame = CGRectMake(80, 25, 160, 50);
    [v addSubview:b];
    myTextInput.inputView = v;
}

Should work fine ... 应该工作得很好......

First of all, Take a look at this 首先,看看这个

The UIKit framework includes support for custom input views and input accessory views. UIKit框架包括对自定义输入视图和输入附件视图的支持。 Your application can substitute its own input view for the system keyboard when users edit text or other forms of data in a view. 当用户在视图中编辑文本或其他形式的数据时,您的应用程序可以用自己的输入视图替换系统键盘。 For example, an application could use a custom input view to enter characters from a runic alphabet. 例如,应用程序可以使用自定义输入视图从符文字母表中输入字符。 You may also attach an input accessory view to the system keyboard or to a custom input view; 您还可以将输入附件视图附加到系统键盘或自定义输入视图; this accessory view runs along the top of the main input view and can contain, for example, controls that affect the text in some way or labels that display some information about the text. 此附件视图沿主输入视图的顶部运行,并且可以包含(例如)以某种方式影响文本的控件或显示有关文本的某些信息的标签。

To get this feature if your application is using UITextView and UITextField objects for text editing, simply assign custom views to the inputView and inputAccessoryView properties. 要在应用程序使用UITextView和UITextField对象进行文本编辑时获得此功能,只需将自定义视图分配给inputView和inputAccessoryView属性即可。 Those custom views are shown when the text object becomes first responder... 当文本对象成为第一响应者时,将显示这些自定义视图...

Actually i don't need to mention all this mess to you, but there is an interesting reason for mentioning this, from the first sentence i am mentioning view-view-view, but you are making the input view in a separate view controller and you are trying to assign it as an input view of your textfield and init shouldn't be creating the view, loadView does that. 实际上我不需要提到所有这些混乱,但有一个有趣的原因提到这一点,从第一句我提到视图 - 视图,但你是在一个单独的视图控制器和输入视图您试图将其指定为文本字段的输入视图,而init不应该创建视图,loadView会这样做。 Calling the view getter (v.view) when view is nil will cause loadView to be invoked.Thats why it is crashing with EXC_BAD_ACCESS. 当视图为nil时调用视图getter(v.view)将导致调用loadView。说明它与EXC_BAD_ACCESS崩溃的原因。

Source : Text, Web, and Editing Programming Guide for iOS 来源: 适用于iOS的文本,Web和编辑编程指南

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

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