• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
[iPhone] Why can't a UITextField be its own delegate?
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[iPhone] Why can't a UITextField be its own delegate?


  • Subject: [iPhone] Why can't a UITextField be its own delegate?
  • From: WT <email@hidden>
  • Date: Sat, 25 Jul 2009 21:06:00 +0200

Hello all,

I need to create a subclass of UITextField for a text field that performs some control over its own editing. The obvious idea is to make the subclass instance set itself up as its own delegate, but that freezes the app as soon as the delegate method - textFieldDidBeginEditing returns.

Looking at Activity Monitor, the CPU usage of the app goes up to nearly 100%. I've since created the most trivial app with a UITextField subclass that conforms to the UITextFieldDelegate protocol and which sets itself up as its own delegate and, sure enough, the app freezes.

Here's the subclass. Set up a project then add a text field to a view and make its class be CustomTextField. Compile and run, and watch it freeze when you attempt to edit the text in the field.

So... why can't the field be its own delegate?

Thanks in advance.
Wagner


// CustomTextField.h:

#import <UIKit/UIKit.h>

@interface CustomTextField: UITextField <UITextFieldDelegate>
{
}

@end

// CustomTextField.m:

#import "CustomTextField.h"

@implementation CustomTextField

- (void) awakeFromNib
{
    super.delegate = self;
    super.text = @"Hello world";

    NSLog(@"awakeFromNib called - delegate set to self");
}

// = = = = ===================================================================== //

- (BOOL) textField: (UITextField*) text_field
shouldChangeCharactersInRange: (NSRange) range
replacementString: (NSString*) string
{
NSLog(@"-textField: shouldChangeCharactersInRange: replacementString:");
return YES;
}


// = = = = ===================================================================== //

- (BOOL) textFieldShouldBeginEditing: (UITextField*) text_field
{
    NSLog(@"-textFieldShouldBeginEditing:");
    return YES;
}

// = = = = ===================================================================== //

- (void) textFieldDidBeginEditing: (UITextField*) text_field
{
    NSLog(@"-textFieldDidBeginEditing:");
}

// = = = = ===================================================================== //

- (BOOL) textFieldShouldEndEditing: (UITextField*) text_field
{
    NSLog(@"-textFieldShouldEndEditing:");
    return YES;
}

// = = = = ===================================================================== //

- (void) textFieldDidEndEditing: (UITextField*) text_field
{
    NSLog(@"-textFieldDidEndEditing:");
}

// = = = = ===================================================================== //

- (BOOL) textFieldShouldClear: (UITextField*) text_field
{
    NSLog(@"-textFieldShouldClear:");
    return YES;
}

// = = = = ===================================================================== //

- (BOOL) textFieldShouldReturn: (UITextField*) text_field
{
    NSLog(@"-textFieldShouldReturn:");
    [self resignFirstResponder];
    return YES;
}

// = = = = ===================================================================== //

@end

_______________________________________________

Cocoa-dev mailing list (email@hidden)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden


  • Follow-Ups:
    • Re: [iPhone] Why can't a UITextField be its own delegate?
      • From: Peter Blazejewicz <email@hidden>
    • Re: [iPhone] Why can't a UITextField be its own delegate?
      • From: Brian Slick <email@hidden>
  • Prev by Date: Re: I need a milder application badge (solution)
  • Next by Date: Re: Unit testing question
  • Previous by thread: Re: I need a milder application badge (solution #2)
  • Next by thread: Re: [iPhone] Why can't a UITextField be its own delegate?
  • Index(es):
    • Date
    • Thread