I have this code in my viewdidload:
[_txtName setDelegate:self];
[_txtName becomeFirstResponder];
_txtName.enabled = YES;
_txtName.text = #"";
But when my view loads, the keyboard does not show, any idea why?
_txtName is the UITextField
As pointed out in a comment by #lukya you should place the call to becomeFirstResponder in either viewWillAppear: or viewDidAppear:. If you place the call in viewWillAppear: the keyboard will be shown on screen when the view is shown. If you would like the keyboard to animate in from the bottom when the view appears you should put the call in viewDidAppear:.
Related
I have a UIViewController with UISearchBar (and SearchDisplayController) along with a UITableView. When navigating to this view controller, I want to auto-focus on the UISearchBar (bring up the keyboard with focus on the text field in the search bar). Everything says to use
[searchBar becomeFirstResponder]
(assuming searchBar is an outlet to the UISearchBar)
I put this at the end of viewDidLoad:
- (void)viewDidLoad
{
[super viewDidLoad];
BOOL did = [searchBar becomeFirstResponder];
[searchBar setText:#"donkey"];
}
Variable did is 0 (and focus doesn't happen), but the search bar's text is successfully changed to donkey.
What am I doing wrong?
I'm using iOS 5 with ARC and latest Xcode (4.3.2).
Got it working. Just had to put it in viewDidAppear instead of viewDidLoad or viewWillAppear.
add your code to -(void)viewDidAppear;
This worked for me:
[window makeKeyAndVisible]
we must ensure the METHOD becomeFirstResponder be performed on mainThread
so make it like :
[xxx performSelectorOnMainThread:#selector(becomeFirstResponder) withObject:nil waitUntilDone:NO];
I don't want to dismiss popover controller when i touch any view. Is it possible?
I am using following code for displaying popover controller.
UIPopoverController* popoverCamera;
popoverCamera= [[UIPopoverController alloc] initWithContentViewController:videoRecorder];
[popoverCamera presentPopoverFromRect:CGRectMake(cropRectangleButton.frame.origin.x,cropRectangleButton.frame.origin.y,0,0) inView:innerview permittedArrowDirections:UIPopoverArrowDirectionUp animated:YES];
NSLog(#"start recording-->%d",[videoRecorder startVideoCapture]);
[videoRecorder startVideoCapture];
[self presentModalViewController:videoRecorder animated:YES];
[videoRecorder release];
Yes it is possible. Implement the following delegate method:
- (BOOL)popoverControllerShouldDismissPopover:(UIPopoverController *)popoverController
{
return NO;
}
Hope this might help you.
From the Link disable dismissal of uipopoverview controller
When displayed, taps outside of the popover window cause the popover to be dismissed automatically. To allow the user to interact with the specified views and not dismiss the popover, you can assign one or more views to the passthroughViews property. Taps inside the popover window do not automatically cause the popover to be dismissed. Your view and view controller code must handle actions and events inside the popover explicitly and call the dismissPopoverAnimated: method as needed.
implement the pop over delegate
/* Called on the delegate when the popover controller will dismiss the popover. Return NO to prevent the dismissal of the view.
*/
- (BOOL)popoverControllerShouldDismissPopover:(UIPopoverController *)popoverController;
func popoverShouldClose(_ popover: NSPopover) -> Bool {
return false
}
Try this where you dont want to dismiss the popUp:-
[popOverController dismissPopoverAnimated:NO];
Hope this helps :)
I added a modalView to my App, everything working fine, but on closing the modal, the whole modalView jumps about 1-2 centimeters to left while it disappears.
I did not find any reason for it yet, so here is the code regarding modal:
AppController:
- (void) showNameModal:(Player *)player
{
namesModal = [[PlayerModalView alloc] init];
namesModal.delegate = self;
namesModal.player = player;
UINavigationController *navCon = [[UINavigationController alloc] initWithRootViewController:namesModal];
navCon.modalPresentationStyle = UIModalPresentationFormSheet;
[self presentModalViewController:navCon animated:YES];
[navCon release];
[namesModal release];
}
- (void)didDismissModalView
{
[self dismissModalViewControllerAnimated:YES];
}
ModalView:
- (void)dismissView:(id)sender
{
[delegate didDismissModalView];
}
called via navigation buttons as well ass via keyboard by
[self dismissView:nil];
As you can see, there is nothing special in it, could be taken from a manual actually.
What happens in detail:
Modal appears in center of screen, slides in from the bottom. centered all time.
i can handle some actions in the modalView, it stays centered.
now, dismissing the view makes it jumping to the left, than slides out.
Since it's a forced landscape-right app (currently), I was only able to notify the left-jump.
Any ideas how to get this jumping away?
Thanks
Try this,
- (void)didmissView:(id)sender
{
[self.navigationController didmissModelViewControllerAnimated:YES];
}
You are not modally presenting an instance of PlayerModalView but rather a UINavigationController. The left jerk you see is most likely the default animation of the navigation controller attempting a slide transform to the (non-existant) previous view.
It doesn't sound like you need a navigation controller for the PlayerModalView. Instead, you should create an ordinary view controller for it.
This solution seems to work well: Modal View Controller with keyboard on landscape iPad changes location when dismissed
To simplify resigning the first responder (if finding it is difficult), you can just call
[self.view endEditing:YES];
[self dismissModalViewControllerAnimated:YES];
The problem is that the UIViewController you're showing modally doesn't allow the orientation you're presenting it in, so when it disappears, it will do that in a direction that it considers "allowed".
Add this to the UIViewController for you modal view:
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return YES;
}
I have an iPhone app that is based on a navigation controller.
I have a main view controller that displays a list of articles, and a detail view, where you can see one article in a UIWebView. For the detail view, I have the navigation bar on the top, and a UIToolbar on the bottom.
I'd like to auto-hide them with a slide animation (to top and bottom) and restore them when tapping the screen. I thought this would be a standard function, but couldn't find how to do it.
As a reference, this is what Stanza or the NYT app do.
Set up a method that runs this on a tap event:
if (![navigationController isNavigationBarHidden])
[navigationController setNavigationBarHidden:YES animated:YES]; // hides
else
[navigationController setNavigationBarHidden:NO animated:YES]; // shows
As for the UIToolbar, it is a UIView subclass, so you should be able to pretty easily set up a custom animation for sliding this in and out of sight.
There is also quite a useful method for UIVIewController.
- (BOOL) hidesBottomBarWhenPushed {
//hide a toolbar or whatever
return NO;
}
Try this:
BOOL hide = ![self.navigationController isNavigationBarHidden];
[self.navigationController setNavigationBarHidden:hide animated:YES];
I guess recently you can use self.navigationController.hidesBarsOnTap = true;
When my iPhone app starts up, the main screen has a keyboard. Currently the keyboard rises as soon as the rest of the interface is displayed and this is visually distracting.
How can I have the view display with the keyboard already up?
Since I am already faking some of the rest of the screen during startup so that the user sees what they last were doing, I thought that I could fake the keyboard as well. But if the motion is there when the real keyboard appears, I've lost the effect. The keyboard is, as far as I know, on a separate window, not just a separate view, so I can't overlay it with my own image.
Is there a way to either overlay the keyboard withy my own image as it appears, or not show the keyboard until it is all the way up, or make its animation instant?
My original answer has the keyboard animate in along with the view controller if it's an animated transition (i.e. pushing a view controller or presenting a modal controller with animated: YES). However, the keyboard still animates in if the new view controller is displayed without an animated transition, so it doesn't solve your problem.
Here's another approach that worked in my testing. Try disabling animations while you're displaying the controller + keyboard.
[UIWindow beginAnimations: nil context: NULL];
[UIWindow setAnimationsEnabled: NO];
RestoredController *controller = [[[RestoredController alloc] init] autorelease];
[self.navigationController pushViewController: controller animated: NO];
[UIWindow commitAnimations];
You'll still need to make field the first responder in viewWillAppear: or viewDidAppear:
In viewWillAppear:, ensure the view is loaded (via self.view) and set the first responder to the correct field. This will display the keyboard fully when the view is actually displayed instead of animating it in.
For example:
- (void) viewWillAppear: (BOOL) animated {
[super viewWillAppear:animated];
NSString *storedID = [[NSUserDefaults standardUserDefaults] stringForKey:#"storedID"];
if ([storedID length] > 0) {
idField.text = storedEmail;
[passwordField becomeFirstResponder];
} else {
[idField becomeFirstResponder];
}
}
Not an answer, but here is some code.
I set the font for my UITextView in viewDidLoad. The view is instantiated by the NIB and the outlet is correctly set up.
-(void)viewWillAppear:(BOOL)animated;
{
[super viewWillAppear:animated];
// Start with the text from the currently edited message
NSString *startString = self.messageManager.editingMessage.text;
// start string processing omitted
self.editingMessageEditingView.text = startString;
if([self showingMessageList]) {
[self.editingMessageEditingView resignFirstResponder];
} else {
#if DEFAULT_SCREEN==1
[[self.tools.items objectAtIndex:kPVToolBarItemDelete] setEnabled:NO];
[[self.tools.items objectAtIndex:kPVToolBarItemSendLater] setEnabled:NO];
#else
[self.editingMessageEditingView becomeFirstResponder];
#endif
}