Subclassing NSTextField to create a number dialing text field

746 views Asked by At

I am trying to create a number dialing text field, that is, a text field that works in conjunction with an NSStepper sitting next to it.

The text field should display floats and allow the user to use the scroll wheel to adjust the number it is displaying as well as the up and down arrow keys to the same effect.

It should also switch to different increments depending on what modifier keys are pressed. Later on I might implement a behaviour where when the user drags the mouse over the text field left or right while pressing alt it will determine where (at what digit of the number) the insertion caret was placed and only increment that number so that the user can quickly update the ones, tens, hundreds, etc. parts.

While the modifier keys and mouse dragging details are rather just that, details, my question is more concerned with the fundamentals of achieving this in a most unobtrusive manner, so that bindings and other features still continue to work.

So far I got a subclass of NSTextField (with a number formatter attached to its cell in IB) and a subclass of NSTextView for being the number dialing text field's field editor. This is so that I can override keyDown: to make the arrow keys updates happening.

The problem is that returning this field editor for the number dialing text field from windowWillReturnFieldEditor:toObject: of the window's delegate seems to break bindings and generally feels like I shouldn't be doing this.

Am I going about this the wrong way?

PS.: You can find just the subclass code at http://gist.github.com/361265 I wasn't sure if I should post it all here.

1

There are 1 answers

0
LJ Wilson On

I have used this in the past. First format the number as with the tel: URL format like so:

NSString *numberString = [@"tel://" stringByAppendingString:self.phoneLabel.text];

Then what I did (since my implementation involved a static UITableView, I checked the row that the user touched and did this:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString *numberString = [@"tel://" stringByAppendingString:self.phoneLabel.text];

    switch (indexPath.row) {
        case 3:
            // Phone Number Field Pressed
            [[UIApplication sharedApplication] openURL:[NSURL URLWithString:numberString]];
            break;
        case 4:
            // Send email using MailComposer
            if ([MFMailComposeViewController canSendMail]) {

                MFMailComposeViewController *mailViewController = [[MFMailComposeViewController alloc] init];
                mailViewController.mailComposeDelegate = self;
                [mailViewController setToRecipients:[NSArray arrayWithObject:self.emailLabel.text]];
                [mailViewController setSubject:@""];
                [mailViewController setMessageBody:@"" isHTML:NO];

                [self presentModalViewController:mailViewController animated:YES];

            }
            break;
        default:
            break;
    }

}

You can implement this several ways, but using the tel: format and then calling the OpenUrl method worked really well for me.