How to dismiss keyboard from childViewController which is in containerView in parentViewController? - ios

I need to dismiss keyboard which show itself when I click UITextField from childView. I need to do this in method (scrollViewWillBeginEditing) which is in parentViewController. How can I do it?
EDIT:
I have method in childViewController:
- (void)dismissKeyboard {
[self.textField resignFirstResponder];
NSLog(#"%#", self.textField.text);
}
and .h of childViewController:
#protocol VVInformationTableViewControllerDelegate<NSObject, UIScrollViewDelegate>
-(void)dismissKeyboard;
#end
#interface VVInformationTableViewController : UITableViewController <UITextFieldDelegate, UITableViewDelegate, UIScrollViewDelegate, VVInformationTableViewControllerDelegate>
#property (weak, nonatomic) id<VVInformationTableViewControllerDelegate> delegate;
and I try to call it in:
-(void)scrollViewWillBeginDragging:(UIScrollView *)scrollView{
[self.infoTableController dismissKeyboard];
}
When it is call then NSLog print (null) and keyboard didn't dismiss, but when I call dismissKeyboard from childViewController then it print true value and keyboard dismiss.
Any help?

You can use the following code
[self.childView endEditing:Yes];

Related

Hiding UIImageView from UISwitch action in popover (iPad)

I need to hide an UIImageView, from an action triggered by a UISwitch which is inside a popover.
I'm using this piece of code however it does nothing when tapping the UISwitch, probably because the UISwitch its inside a popover view.
This code works perfectly on iPhone, however on iPad does not work and the UIImageView does not hide. Why?
- (IBAction)toggleImage:(id)sender {
if ([sender isOn]){
self.myImage.hidden = NO;
} else {
self.myImage.hidden = YES;
}
}
UIImageView is connected to an outlet and UISwitch is connected to an outlet and action.
Please help, thank you.
Since the switch being interacted with is on the popover and the image view that we want to change is on the underlying (presenting) VC, the proper approach is to make the underlying VC a delegate of the popover.
// MyPopoverVC.h
#protocol PopoverDelegate <NSObject>
- (void)popover:(MyPopoverVC *)vc changedSwitchTo:(BOOL)on;
#end
#interface MyPopoverVC : UIViewController
#property (nonatomic, weak) id<PopoverDelegate>delegate;
// ...
#end
In the Popover VC's implementation (IMPORTANT: the switch's IBAction should be wired to the popover vc)...
- (IBAction)toggleImage:(UISwitch *)sender {
[self.delegate popover:self changedSwitchTo:sender.on];
}
In the presenting vc, declare it as conforming to that <PopoverDelegate> protocol. Before presenting the popover, initialize the delegate...
MyPopoverVC *myPopoverVC = [[MyPopoverVC alloc] init...
myPopoverVC.delegate = self;
Also in the presenting vc, implement the delegate protocol...
- (void)popover:(MyPopoverVC *) changedSwitchTo:(BOOL)on {
self.myImage.hidden = !on;
}

Xcode - Using Delegate and Segue to Unlock button in first ViewController

I have this simple project where, when a button on a second ViewController (SecondViewController) is pressed a Delegate function send a string to the first ViewController and Unhide a new button which is not visible.
I have no problem now doing this however, if I add a third view between these to windows the function stops working.
Here what I mean:
In order to do this I use
In ViewController.h:
#interface ViewController : UIViewController <SecondViewControllerDelegate>
In ViewController.m I implemented a Segue function which detected an Identifier and send the value to second view :
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
if ([segue.identifier isEqualToString:#"to2"]) {
SecondViewController *SecondView = segue.destinationViewController ;
SecondView.delegate = self;
}
}
-(void)done:(UIButton*)name{
[self dismissViewControllerAnimated:YES completion:nil];
NSLog(#"back to first view, name=%# ", name);
level2But.hidden = NO;
}
To return to the first window i have created a returnButton in SecondViewController.h:
#protocol SecondViewControllerDelegate <NSObject>
-(void) done:(UIButton*)returnButton; //variable passed to ViewController
#end
#interface SecondViewController : UIViewController{
IBOutlet UIButton *returnButton;
id delegate; //declare delegate as an object
}
#property (nonatomic, strong) id <SecondViewControllerDelegate> delegate;
-(IBAction)returnButtonPressed:(id)sender;
In SeconViewController.m I have:
-(IBAction)returnButtonPressed:(id)sender{
[self.delegate done:returnButton];
}
This code is working just fine However if I had a third View It stops Working.
This is not working anymore.
AnySuggestion? Any Help?
This is the project file:
http://goo.gl/3rJOje
I did checked your code and I could see the self.delegate is nil and hence you are not able to move in SecondViewController.m.
-(IBAction)returnButtonPressed:(id)sender{
[self.delegate done:returnButton];
}
so make sure you pass the delegate.
Also if you want to know how to pass the data(delegate) then go through the below tutorial :
http://www.appcoda.com/storyboards-ios-tutorial-pass-data-between-view-controller-with-segue/

How to get custom UIView from Storyboard

In FatEditViewController, has a custom UIView.
FatEditViewController.h
#import <UIKit/UIKit.h>
#interface FatEditViewController
#property (weak, nonatomic) IBOutlet UIView *myview;
#end
In another UIViewController, I want to get the myview object.
I do like this:
FatEditViewController *vc = [self.storyboard instantiateViewControllerWithIdentifier:#"FatEdit"];
UIView *view = [vc myview];
NSLog(#"%#, %#", vc,view);
And the myview is null.
How to get the myview?
The problem here is that your myview gets instantiated only when your FatEditViewController's method viewDidLoad is called, which of course is called only when the view controller is presented on screen. One possible way to achieve what you need is to build myView programmatically (it wouldn't be an IBOutlet anymore), possibly by lazy initialising it in the getter. You would have to add the view to your FatEditViewController in the viewDidLoad method programmatically as well.
-(UIView *)myview
{
if(!_myview){
_myview = [[UIView alloc] initWithFrame:whateverFrameYouNeed];
//build the view
}
return _myview;
}
The property would have to be strong as well.

iOS modal views in stack dissmiss

i have the following problem:
I have a storyboard containing three views with UIViewController classes. I do not use seques and apart from initial view (call it View A), all other views are created programmatically with presentViewController method. The problem i have is that when A opens B with presentViewController and then B opens C with presentViewController, when i dissmiss C with dismissViewControllerAnimated (in C there is a Button with Outlet to C UIViewController calling dismissViewControllerAnimated on self), C View disapears, and B appers but only for a 0.1 sec, and then i get C View showing again, and after that the close button wont work anymore.
Any idea what the reason might be?
Best Regards Edin
//delegate definition used between controller A/B and B/C
#protocol ParentControllerDelegate <NSObject>
//called as delegate method from B on A and from C on B to dismiss B from A and C from B
- (void)dismissView:(UIViewController*)controller;
#end
// MainMenuViewController.h which is controller A
#interface MainMenuViewController : UIViewController <ParentControllerDelegate>
//Controller B property
#property (strong, nonatomic) ChooseLevelViewController *chooseLevelViewController;
//button to open controller B
#property (weak, nonatomic) IBOutlet UIButton *chooseLevelBtn;
#end
//MainMenuViewController.m - Controller A
#implementation MainMenuViewController
//called to present chooseLevelViewController which is controller B
- (IBAction)chooseLevelPressed:(id)sender
{
if(self.chooseLevelViewController == nil)
{
self.chooseLevelViewController = [self.storyboard instantiateViewControllerWithIdentifier:#"ChooseLevelView"];
self.chooseLevelViewController.parentControllerDelegate = self;
}
[self presentViewController:self.chooseLevelViewController animated:YES completion:nil];
}
//called from controller B over delegate mechanism to dismiss B
- (void)dismissView
{
[self.chooseLevelViewController dismissViewControllerAnimated:YES completion:nil];
}
#end
// ChooseLevelViewController.h which is controller B
#interface ChooseLevelViewController : UIViewController <ParentControllerDelegate>
//Controller A as delegate
#property (assign, nonatomic) id <ParentControllerDelegate> parentControllerDelegate;
//Controller C property
#property (strong, nonatomic) ChoosePlayerViewController *choosePlayerViewController;
//button to dismiss B over delegate A
#property (weak, nonatomic) IBOutlet UIButton *backMainBtn;
//button to open C
#property (weak, nonatomic) IBOutlet UIButton *choosePlayerBtn;
#end
//MainMenuViewController.m - controller B
#implementation ChooseLevelViewController
//calling controller A as delegate to dismiss B
- (IBAction)backMainBtnPressed:(id)sender
{
[self.parentControllerDelegate dismissView];
}
//presenting choosePlayerViewController which is controller C
- (IBAction)choosePlayerBtnPressed:(id)sender
{
if(self.choosePlayerViewController == nil)
{
self.choosePlayerViewController = [self.storyboard instantiateViewControllerWithIdentifier:#"ChoosePlayerView"];
self.choosePlayerViewController.parentControllerDelegate = self;
}
[self presentViewController:self.choosePlayerViewController animated:YES completion:nil];
}
//called from controller C over delegate mechanism to dismiss C
- (void)dismissView:(UIViewController*)controller
{
[self.choosePlayerViewController dismissViewControllerAnimated:YES completion:nil];
}
#end
//ChoosePlayerViewController.h which is controller C
#interface ChoosePlayerViewController : UIViewController
//Controller B as delegate
#property (assign, nonatomic) id <ParentControllerDelegate> parentControllerDelegate;
//button to dismiss C over delegate B
#property (weak, nonatomic) IBOutlet UIButton *closeBtn;
#end
#implementation ChoosePlayerViewController
//calling controller B as delegate to dismiss C
- (IBAction)closeBtnPressed:(id)sender
{
[self.parentControllerDelegate dismissView];
}
#end
You should NEVER have a view dismissing itself. You should always have the parent view dismiss the child.
B should dismiss C
A should dismiss B
Check http://developer.apple.com/library/ios/featuredarticles/ViewControllerPGforiPhoneOS/ModalViewControllers/ModalViewControllers.html#//apple_ref/doc/uid/TP40007457-CH111-SW14
for more information.

iPad 'dismiss keyboard' button doesn't dismiss keyboard

I have a UITextField in a Landscape view, and when I press the 'dismiss keyboard' button in the lower right of the UIKeyboard view, the keyboard does NOT disappear. Is there a way to programmatically listen for when this key was pressed? Or is there a connection I am not seeing that will make this keyboard go away?
This is iOS 4 and XCode 4. Thanks.
I had same problem today and I wondered, wy it works in Apple's KeyboardAccessory Sample Code.
So I did reverse engineering. The ViewController was not the mistake I made in my case.
In the implementation of UIApplicationDelegate there is the entry point of application, where root viewcontroller and window will be setup - (void) applicationDidFinishLaunching:(UIApplication *)application. If you forgot to add root viewcontrollers view to window as subview, the dismiss-keyboard-button wouldn't work in any view of your app.
#class ViewController;
#interface KeyboardAccessoryAppDelegate : NSObject <UIApplicationDelegate> {
UIWindow *window;
ViewController *viewController;
}
#property (nonatomic, retain) IBOutlet UIWindow *window;
#property (nonatomic, retain) IBOutlet ViewController *viewController;
#end
...
- (void)applicationDidFinishLaunching:(UIApplication *)application {
[window addSubview:viewController.view];
[window makeKeyAndVisible];
}
Please don't forget to setup the outlets in the main xib file.
I dont know why this is related to keyboards behavior. But my theory is, that the responder chain is not linked to window, but it needs.
To dismiss the keyboard using the dismiss keyboard button you need to implement the delegate method
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
//Check if your text field is first responder, and if it is, have it resign
}
Alternatively, if you want to dismiss the keyboard by tapping outside of it, use
-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
for (UIView* view in self.view.subviews)
{
if ([view isKindOfClass:[UITextField class]]) {
[view resignFirstResponder];
}
if ([view isKindOfClass:[UITextView class]]) {
[view resignFirstResponder];
}
}
}
you need to tell the text field that is accepting the keyboard input to no longer be the first responder.
[UITextField resignFirstRepsonder];

Resources