Not running textFieldShouldReturn method - objective-c

I just started reading up on Objective-C yesterday, and i can't quite figure why my
textFieldShouldReturn
method isn't being run.
This is the actual method:
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
self.itemTxt.text = #"Return pressed";
return YES;
}
This is the interface line in my header file:
#interface ArrViewController : UIViewController <UITextFieldDelegate>

You need to actively set the delegate for the UITextField:
For example, in viewDidLoad you could write:
myTextField.delegate = self;
You can also hook this up in Interface Builder if desired.

Use setDelegate method of your textfield to self

Related

textFieldShouldReturn is not called

I want to make the keyboard disappear when the user clicks the "return" button, I was told to use
-(BOOL)textFieldShouldReturn:(UITextField *)textField {
[tf resignFirstResponder];
return YES;
}
But nothing happens when I click the "return" button, the method isn't even being called. I am doing this in
#interface gameOverMenu : UIView
not in the ViewController. I also don't use interface builder. What should I do?
You need to make sure you implement the UITextFieldDelegate and set your UITextField delegate to self. In your .h file:
#interface gameOverMenu : UIView <UITextFieldDelegate>
And somewhere in your .m file (viewDidLoad: maybe):
self.yourTextField.delegate = self;
Now your -(BOOL)textFieldShouldReturn:(UITextField *)textField method should be called.
Make sure that you have set the parent class (whatever it is) as a UITextFieldDelegate

Set App delegate as first responder

I'm trying to implement a shake recognition that works throughout my application. To do this I'm adding the following code to my xxxAppDelegate.m:
-(BOOL)canBecomeFirstResponder {
return YES;
}
- (void)motionBegan:(UIEventSubtype)motion withEvent:(UIEvent *)event {
if (event.type == UIEventSubtypeMotionShake) {
NSLog(#"Shaken, not stirred.");
}
}
But because in the .h file the delegate is defined as a
#interface xxxAppDelegate : NSObject <UIApplicationDelegate, UITabBarControllerDelegate>
I cannot use the
[self becomeFirstResponder];
in the .m to make the app delegate the first responder. Therefore of course it doesn't work.
What would be the best way to get it working?
What happens if you change the app delegate to a subclass of UIResponder?
Edit
You can read about the responder chain here in the documentation.

In which method should I set the delegate of a UITextField?

Is it good practice to be setting the delegate of a UITextField in viewDidLoad or in an init method?
I tried setting the delegate as self in an init method, but it wasn't calling the corresponding delegate methods, when I moved the code into viewDidLoad, it registered as setting self as the delegate?
It seems that I should be able to set it in either method, if someone can help clear this up for me it would be much appreciated.
-(id) init {
self = [super init];
if (self)
textField.delegate = self; //this text field is an IBOutlet
//some other code here as well
return self;
}
OR
-(void)viewDidLoad {
[super viewDidLoad];
textField.delegate = self;
}
If your text field is an IBOutlet then until viewDidLoad method is called your text field will be nil (hence you set delegate to nil object). When viewDidLoad gets called it literally means that view was loaded and all IBOutlets and IBActions were connected and are at your disposal.
Assuming that your class is a UIViewController and is loaded from a NIB (since you have an IBOutlet), the proper init method to override is initWithCoder:(NSCoder*)decoder. What's happening right now:
iOS loads your NIB file and creates your UIViewController
The UIViewController is created by calling its initWithCoder:(NSCoder*)decoder method.
The first thing that initWithCoder does is to call init and therefore your code, before it has even decoded the NIB.
Because it hasn't decoded the NIB yet, your textField IBOutlet hasn't been set yet (if you debug it you should be able to see that its value is nil inside your init). And therefore setting the delegate doesn't do anything.
The easiest way to proceed is indeed to set your delegate in the viewDidLoad method; it can't be done in init. However it can be done by overriding initWithCoder:
- (id)initWithCoder:(NSCoder*)decoder {
self = [super initWithCoder:decoder];
if (self)
textField.delegate = self;
return self;
}
This time you wait until UIViewController's implementation of initWithCoder has finished decoding the NIB and so all your outlets have been set.

keydown in objective-C

[UPDATED AFTER FIRST AWNSER]
I have tried to find a way to use and implement the keyDown option in Objective C. But when I try it, it always fails...
Can anyone give me an example of how this is done. I understand Objective C good and a full explaination is not needed.
I deleted the method in -(void) keyDown because it wasn't working.
This is my code now:
#import <Cocoa/Cocoa.h>
#interface ViewController : NSView {
IBOutlet id pressLabel;
}
#end
#import "ViewController.h"
#implementation ViewController
-(BOOL) acceptsFirstResponder
{
return YES;
}
-(BOOL) becomeFirstResponder
{
return YES;
}
-(BOOL) resignFirstResponder
{
return YES;
}
-(void)keyDown:(NSEvent *)theEvent
{
NSString *theUpArrow = [NSString stringWithFormat:#"%c",NSUpArrowFunctionKey];
if( [[theEvent characters] isEqualToString:theUpArrow]){
[pressLabel setStringValue:#"Pressed"];
} else {
[super keyDown:theEvent];
}
}
#end
keyDown: is an NSResponder method, typically implemented in views. This class is named Controller, which would suggest it isn't a view, and thus won't receive key down events. You probably want to put it in a view instead.
[self keyDown:theEvent];
This doesn't solve your problem, but I think for the line above, you want to use super and not self. If you use self it will invoke the same method again, and it will keep invoking the same method over and over, eventually crashing your application once there is no more stack space.

Show new window from status menu [duplicate]

OK, what am I doing wrong?
1. Created cocoa app and appDelegate named: window2AppDelegate
2. window2AppDelegate.h
#import "PrefWindowController.h"
#interface window2AppDelegate : NSObject <NSApplicationDelegate> {
NSWindow *window;
PrefWindowController * ctrl;
}
#property (assign) IBOutlet NSWindow *window;
- (IBAction) buttonClick:(id)sender;
- (IBAction) buttonCloseClick:(id)sender;
#end
3. in xib editor, window connected to window controller - set to appdelegate, buttonclick actions to buttons
4, created
#import <Cocoa/Cocoa.h>
#interface PrefWindowController : NSWindowController {
#private
}
#end
#import "PrefWindowController.h"
#implementation PrefWindowController
- (id)init {
self = [super initWithWindowNibName: #"PrefWindow"];
return self;
}
- (void)dealloc {
// Clean-up code here.
[super dealloc];
}
- (void)windowDidLoad {
[super windowDidLoad];
// Implement this method to handle any initialization after your window controller's window has been loaded from its nib file.
}
#end
5. created new xib file named PrefWindow window IBOutlet connected to window from its controller (also controller set to PrefWindowController) Option "Visible At Launch" UNCHECKED! i want to see this window on buttonclick.
6. implemented window2AppDelegate
#import "window2AppDelegate.h"
#implementation window2AppDelegate
#synthesize window;
- (id) init {
if ((self = [super init])) {
ctrl = [[PrefWindowController alloc] init];
if ([ctrl window] == nil)
NSLog(#"Seems the window is nil!\n");
}
return self;
}
- (BOOL)applicationShouldTerminateAfterLastWindowClosed:(NSApplication *)sender {
return YES;
}
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
// Insert code here to initialize your application
}
- (IBAction) buttonClick:(id)sender {
// [[ctrl window] makeKeyAndOrderFront:self]; this doesn't work too :(
NSLog(#"it is here");
[ctrl showWindow:sender];
}
- (IBAction) buttonCloseClick:(id)sender {
[window close];
}
#end
7. After I build and run app: closebutton closes the app but buttonclick - won't show me PrefWindow!? Why and what am i doing wrong? Don't dell me that to show other window in cocoa objective-c is more difficult than in "stupid" Java or C#?
Finally i've managed the problem! In the nib editor for PrefWindow I had to do: Set File's owner class to: NSWindowController then connect window IBOutlet from File's owner to my (preferneces) window. After 6 days of unsuccessful attempts, google works.
Anyway, thanks for all your responses and time!
I'd suggest you move the creation of the PrefWindowController to applicationDidFinishLaunching:
I am not sure the application delegate's init method is called. Probably only initWithCoder: gets called when unarchiving the object from the NIB.
- (id) init {
if ((self = [super init])) {
ctrl = [[PrefWindowController alloc] init];
if ([ctrl window] == nil)
NSLog(#"Seems the window is nil!\n");
}
return self;
}
init is way too early in the scheme of things to be trying to test IBOutlets. They will still be nil yet. Not until later on in the object creation process will the nib outlets be "hooked up". The standard method where you can know that everything in the nib file has been hooked up is:
- (void)awakeFromNib {
}
At that point, all of your IBOutlets should be valid (provided they're not purposely referencing an object in a separate, yet-unloaded nib).
If PrefWindowController is a class that will only be used after the user chooses Preferences from the app menu, I would have to disagree with the others and say that I would not create the instance of the PrefsWindowController at all during the initial load. (Your main controller should be able to function independently from the prefs window). If you have a method that is meant to load the preferences window, then when that method is called, you should check to see if the PrefsWindowController instance is nil, and if it is, create it, then proceed to show the prefs window.