2

I'm working through the IOS HelloWorld example and I have a question regarding setting the delegate for a TextField. In the example it was as easy as control-dragging from the TextField to the ViewController. But now say I wanted to create a custom class to act as my delegate as so:

#import <Foundation/Foundation.h>
@interface SweetAssDelegate : NSObject <UITextFieldDelegate>
- (BOOL)textFieldShouldReturn:(UITextField *)theTextField;
@end

#import "SweetAssDelegate.h"

@implementation SweetAssDelegate
- (BOOL)textFieldShouldReturn:(UITextField *)theTextField
{
    NSLog(@"Calling Delegate");
    [theTextField resignFirstResponder];
    return YES;
}
@end

How can I set this class to be the delegate of the TextField? As far as I can tell there is not way to accomplish this through the GUI. I tried manually setting the delegation after window load with no success:

#import "ViewController.h"
#import "SweetAssDelegate.h"

@interface ViewController ()
@property (weak, nonatomic) IBOutlet UITextField *inputField;
@end

- (void)viewDidLoad
{
    [super viewDidLoad];

    SweetAssDelegate *foo = [[SweetAssDelegate alloc] init];
    [self.inputField setDelegate:foo];

    NSLog(@"Delegate: %@", self.inputField.delegate);
}

I actually receive some sort of memory exception when bringing up the keyboard? Any ideas? Thanks.

As a side question, where should I always use viewDidLoad to initialize any variables? I noticed that init was not being called???

2
  • What kind of memory exception? Oct 4, 2012 at 17:28
  • 1
    There should be no need to declare - (BOOL)textFieldShouldReturn:(UITextField *)theTextField; in the interface of SweetAssDelegate as it is already declared in UITextFieldDelegate, which your class implements.
    – FD_
    Oct 4, 2012 at 17:28

2 Answers 2

2

Your delegate object, foo, is allowed to fall out of scope and is released at the end of viewDidLoad and by the time the keyboard comes up, it doesn't exist anymore. Make it an ivar (or property) of your view controller, or otherwise make sure that foo doesn't fall out of scope at the end of viewDidLoad.

Thus, it could be something like:

@interface ViewController ()
@property (weak, nonatomic) IBOutlet UITextField *inputField;
@property (strong, nonatomic) SweetAssDelegate *foo;
@end

- (void)viewDidLoad
{
    [super viewDidLoad];

    self.foo = [[SweetAssDelegate alloc] init];
    [self.inputField setDelegate:self.foo];

    NSLog(@"Delegate: %@", self.inputField.delegate);
}
7
  • He's better off with a property. Oct 4, 2012 at 17:36
  • @SimonGermain I don't particularly care if he defines the ivar himself, or let's a property synthesize it for him. The key is that he just needs something with a scope of the view controller, not of viewDidLoad.
    – Rob
    Oct 4, 2012 at 17:51
  • ivars aren't "cool" anymore, but +1 cuz you get the point! :P Oct 4, 2012 at 17:52
  • Before ARC I would need to use retain correct? Now that I am using ARC property's are the way to go? Oct 4, 2012 at 18:22
  • @user1437801 I have a slightly different spin on this: The key issue is that it shouldn't be a local variable of viewDidLoad. And if MRC, if the local variable was retained (and no retain is needed because it already would have a +1 retainCount), you'll fix the crash, but you'll leak. In terms of whether this should be a instance variable (ivar) or a property in this particular situation, I'll leave to others to debate. Just make sure it shares the scope of the view controller, not of viewDidLoad. But ARC is definitely the way to go!
    – Rob
    Oct 4, 2012 at 19:24
0

Your textfield delegate must have the implemented to be your textfield delegate I guess.

A delegate manages the communication between objects, which means your custom delegate must allow communication between objects and must provide methods, the textfield can work with...

Another example is a tableView:

You can make a custom delegate which implements the delegates and then calls some tableview related Methods...

Here this code might be interesting for you:

@interface myCustomDelegateForTextFields <UITextFieldDelegate>


@end

@implementation myCustomDelegateForTextFields 

- (BOOL)textFieldShouldReturn:(UITextField *)theTextField {

return TRUE;

}

@end

@implementation ViewController 


myCustomDelegateForTextFields *txtfielddelegate = [[myCustomDelegateForTextFields alloc] init];

UITextField *whatever;
whatever.delegate = txtfielddelegate;

//your textfield now listens to the BOOL method in your custom delegate

@end

Is it that what u were looking for? :)

you can ofc pack the myCustomDelegateForTextField delegate in another class and call the class

1
  • 1
    This doesn't solve his problem, because you've defined your delegate object within the implementation, too, and txtfielddelegate will fall out of scope. The delegate object needs to be part of the @interface of the view controller, (either the main interface or a private class extension) not the @implementation. I'm sure you were just trying to use shorthand here and you probably meant it to be an ivar/property of the view controller, but it seems it's an important point given that this is why his program is crashing.
    – Rob
    Oct 4, 2012 at 18:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.