Ios adds custom inputView

I am stuck in an iOS concept that I cannot understand, no matter how much I read about it. I am trying to override the standard iOS number pad with a special design. When a user touches a UITextField, I want the user inputView to appear instead of the standard numeric keypad.

I created a separate ViewHontroller.h / .m / .xib class for my custom View input called "customInputViewController". Right now itโ€™s just a dark background and one button that hides about half the screen when you touch UITextField (similarly on the numeric keypad, but it looks just different). My implementation fails when I click one button in my custom inputView - iOS throws an EXC_BAD_ACCESS error.

This is how I load the .xib file at runtime and attach a custom inputView to a UITextField object:

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

In the user input .Xib file for the View, I set the File Owner as "customInputViewController" and I created a method (IBAction) and bound it to UIButton. When this button is pressed, (IBAction) is configured to send an NSLog message (@ "Clicked"). Nothing special. This is just a simple template implementation that continues to throw an error.

Perhaps I am doing this completely wrong. Can anyone provide a simple example?

+4
source share
2 answers

The v.view view is preserved because the inputView property is defined as (readwrite, keep). However, if you release your customInputViewController v somewhere before pressing the enter button, you will fail (EXC_BAD_ACCESS)

You can try this on 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 ...

+2
source

First of all, look this

The UIKit framework includes support for custom input representations and input accessories. Your application can replace its own input representation for the system keyboard when users edit text or other forms of data into a view. For example, an application may use a custom input view to enter characters from the runic alphabet. You can also attach an input auxiliary view to the system keyboard or user input view; this accessory opens at the top of the main input view and may contain, for example, controls that in some way affect the text or labels that display some information about the text.

To get this function, if your application uses UITextView and UITextField Objects for text editing, they simply assign custom views to the inputView and inputAccessoryView properties. These custom views are displayed when the text object becomes the first responder ...

Actually, I donโ€™t need to mention all these disturbances, but there is an interesting reason to mention this, from the first sentence I mention view-view-view, but you make the input view separate and you try to designate it as the input representation of your text field, and init should not create a view, loadView does this. Calling the getter (v.view) function when view is nil will call loadView. For this reason, it crashes with EXC_BAD_ACCESS.

Source: Text, Web, and Editing Programming Guide for iOS

+1
source

Source: https://habr.com/ru/post/1445298/


All Articles