textFieldShouldReturn not firing in iPad app - objective-c

I have an iPad app created using XCode 4 with Storyboard. I have a UITableViewController with the interface defined as such:
#interface CustomerViewController : UITableViewController <UITextFieldDelegate>
In the .m file, I have a code snippet as:
- (BOOL) textFieldShouldReturn:(UITextField *)textField {
if(textField == businessName) {
[email becomeFirstResponder];
return true;
}
The method 'textFieldShouldReturn' does NOT get executed. What else needs to be done? (BTW... I have never been able to get this to work in a iPad app, but always successful in a iPhone app)

In the textFieldShouldReturn method, you're testing if the textField is equal to businessName. I'm assuming that's a UITextField object, but you have to set the delegate to receive callbacks. Just a simple self.businessName.delegate = self; when you create the TextField.
Another tip, I'd recommend calling it something like businessNameTextField. It's easier to read throughout your code.

You have to set the view controller as the delegate of the text field, just declaring that you conform to the protocol isn't enough. If it works in the iPhone but not iPad, then the chances are you haven't linked the delegate outlet in the iPad storyboard, but you have in the iPhone.

Related

How do I make the AppDelegate a NSWindowDelegate?

I'm finally trying out storyboards on OSX and have become confused by the structure of a generated project. Basically, I'd like to know how windowWillReturnUndoManager is supposed to work as part of the application delegate.
I create a Cocoa application with Storyboards, Core Data, and no
documents.
The generated AppDelegate contains an implementation of -
(NSUndoManager *)windowWillReturnUndoManager:(NSWindow *)window.
The AppDelegate is not defined as a NSWindowDelegate, however.
The above method is never called.
Trying to set the AppDelegate as the window delegate gives me some problems.
The Application Scene has the AppDelegate object and the Window
Controller Scene has the window, so IB connections don't work.
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification is called before the application's mainWindow is available.
I can make it work by defining a mainWindow ivar and using code like...
- (void)applicationDidUpdate:(NSNotification *)aNotification {
if (!mainWindow) {
NSWindow *appWindow = [[NSApplication sharedApplication] mainWindow];
if (appWindow) {
mainWindow = appWindow;
[mainWindow setDelegate:self];
}
}
}
...and it sets the delegate the fourth time it's called. To me, that seems like a ridiculous hack.
What's the expected strategy here?

cocoa windowDidEnterFullScreen not being called

I'm writing a Cocoa application which should work in fullscreen mode. I would like to detect the user setting the application window to fullscreen mode.
In Xcode,
in the Attributes Inspector, Full Screen value is "Primary Window"
in the File Inspector, Use Auto Layout is unchecked
the view of the main window has Autoresizes Subviews unchecked
I'm making the AppDelegate an NSWindowDelegate like this in AppDelegate.h
#interface AppDelegate : NSObject <NSApplicationDelegate, NSWindowDelegate>
and have a method like this in AppDelegate.m
-(void) windowDidEnterFullScreen:(NSNotification *)notification
{
vuMain.view.frame = NSMakeRect(0, 0, window.frame.size.width,
window.frame.size.height);
NSLog(#"AppDelegate - windowDidEnterFullScreen");
}
But this method is not getting called as there is no NSLog output. A watch set in the method does not get reached.
What am I doing wrong? What am I missing?
Please help! I'm rather new to Cocoa and am struggling for one full day with this.
Thanks
You need to actually make the AppDelegate the window's delegate. You either have to connect the window's delegate outlet to the AppDelegate in the NIB, or you need to set the window's delegate property programmatically.

Undeclared identifer error in prepareForSegue : using Storyboards in iOS6

I am trying to build a 2 scene application using Storyboards in iOS6.
I am taking the users name via text input in the first scene and passing it using a push segue to the second scene; where it is displayed in a label.
The first scene's UIViewController is called ViewController and the second scenes UIViewcontroller is DrawViewController.
I have imported the the DrawViewController.h in my ViewController.m file where I have defined the prepareForSegue as below:
-(void) prepareForSegue:(UIStoryboardSegue *) segue sender:(id) sender{
if ([segue.identifier isEqualToString:#"ColorPickerControllerSegue"]){
DrawViewController *dvc =[segue destinationViewController];
dvc.userName= self.userName;
}
}
Here userName is a NSString defined in DrawViewController.
I am getting "use of undeclared identifer:DrawViewController".
I am quite new to iOS programming, so is there something I am missing here?
I have set the second view controller's custom class to DrawViewController.
I was able to solve this issue by removing the existing DrawViewController.h and DrawViewController.m files and then creating and adding them back to the the project.
It seemed to do the trick.

Populate text in one textfield from text in another

Working on an experiment on the iPad. Tried some variations on how to do this, but I can't seem to get it to work correctly...
I tap a UIButton on my MainViewController and a TextEntryModule is added to the view. TextEntryModule is its own class (for multiple instantiation) and it contains a UITextView called TextEntry (this all works at the moment).
I tap on the TextEntry UITextView and it brings up the keyboard and another view (located in MainViewController) with a UITextView called TextPreview. (this also works at the moment).
The part I'm having trouble with is synching the two UITextViews. The idea being that when I type into TextEntry, the text in TextPreview will also be updated.
Outlets are linked properly for the text fields, but I think I'm missing something "obvious":
TextEntryModule *tm = (AnnotationModule *)currentModule;
TextPreview.text = tm.TextEntry.text
Thanks in advance!
UITextView: delegate.
- (void)textViewDidChange:(UITextView *)textView
Then assign it the value of the other textview in this method.
Edit
#interface MainViewController <UITextViewDelegate> {
...
}
...
#end
Then you implement this method in the implementation file of MainViewController
#implementation MainViewController
//More code
- (void)textViewDidChange:(UITextView *)textView {
TextEntryModule *tm = (AnnotationModule *)currentModule;
TextPreview.text = tm.TextEntry.text
}
#end
Then you will have to set the TextEntryModule object's delegate to self since the controller now conform to the protocol and can "act" upon this notification.
You need to become a UITextFieldDelegate and monitor when text changes in the one field and then update the other field. Take a look at the documentation on it.

ipad detect when UIPopoverControllers are dismissed

I have several uiPopoverControllers in my universal iPad app. I now have a requirement to trigger a function once a certain popover has been dismissed. I can do this easily if the user clicks "close" inside the popover, but if they touch the screen to hide the popover, I cannot trigger my function.
I've been googling for some time and cannot seem to find any delegate methods which I might be able to use in my main view controller to capture them. I would love something like didDismissPopoverController - but my guess is it's not available.
IF not, I guess the only thing to do would be to detect the touches and trigger then? Basically I am highlighting a UITableView row and loading the popover. I need to deselect the row - so want to simply call [table reloaddata].
Thanks for any help on this one!
You need to assign a delegate to the UIPopoverController and then implement the - (void)popoverControllerDidDismissPopover:(UIPopoverController *)popoverController method. For example:
#interface FooController : UIViewController <UIPopoverControllerDelegate> {
// ...
}
// ...
#end
When you instantiate the UIPopoverController (say, for this example, in FooController)...
UIPopoverController *popover = // ...
popover.delegate = self;
Then, you would implement the method:
- (void)popoverControllerDidDismissPopover:(UIPopoverController *)popoverController {
// do something now that it's been dismissed
}
Granted, I haven't tested this but it seems like it should work...
Hope this helps!
You can use the popoverControllerDidDismissPopover delegate method after the following assignment:
self.popoverController.delegate = self;
Note that popoverControllerDidDismissPopover delegate method does not get called if you programmatically call [self.popoverController dismissPopoverAnimated:YES].