Custom Views aren't dismissing keyboards? - ios

Have tried [[self jotNotes] resignFirstResponder]; , have tried [self endEditing:YES];
So I have my NoteViewController, which inherits UIViewController, and tries to implement the delegate like so
#interface NOTEController : UIViewController <UITextViewDelegate>
#end
#implementation NOTEController
-(id)init {
self = [super init];
if (self) {
// self.delegate = self; //doesnt let me set this, so i assume i do not do that here
NOTEControllerView * mainView = [[NOTEControllerView alloc] initWithFrame:[UIScreen mainScreen].bounds];
self.view = mainView; //just a plain custom uiview subclass its boring and not special
}
return self;
}
#end
and then in the mainView, i have a bunch of sub-views that are basically a square with a UITextView inside.
the squares class is like this, and they're the ones im trying to dismiss the keyboard from, heres where i set the delegate, the dismissKB method, and the UITextView code. Currently, it will log my keyboard method when pressing the done button, but the keyboard is still present. Would really appreciate if anyone could help me understand why
#interface NOTESubview : UIView <UITextFieldDelegate>
#property (nonatomic, weak) id<UITextFieldDelegate> delegate;
-(UITextView *)jotNotes;
#end
#implementation NOTESubview
-(id)initWithFrame:(CGRect)arg1 {
self = [super initWithFrame:arg1];
if (self) {
self.delegate = self;
[self addSubview:[self jotNotes]];
}
return self;
}
-(UITextView *)jotNotes {
UITextView * jotNotes = [[UITextView alloc] initWithFrame:CGRectMake(0, self.frame.size.height/5.7, self.frame.size.width, self.frame.size.height - self.frame.size.height/5.7)];
UIToolbar* keyboardTextViewBar = [[UIToolbar alloc] init];
keyboardTextViewBar.barStyle = UIBarStyleDefault;
[keyboardTextViewBar sizeToFit];
UIBarButtonItem *flexSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self action:nil];
UIBarButtonItem* doneButton = [[UIBarButtonItem alloc] initWithTitle:#"Done"
style:UIBarButtonItemStylePlain target:self
action:#selector(dismissKB:)];
[keyboardTextViewBar setItems:[NSArray arrayWithObjects:flexSpace, doneButton, nil]];
jotNotes.inputAccessoryView = keyboardTextViewBar;
jotNotes.delegate = self.delegate;
return jotNotes;
}
-(void)dismissKB:(UIBarButtonItem *)sender {
//this will log, so im not sure why it wont resign the board no matter what i try
NSLog(#"keyboard attempted to dismiss by %#", sender);
[[self jotNotes] resignFirstResponder];
}

I suspect that when the dismissKB method is called, its actually not the one who is currently the first responder.
However, there's a trick where you can just "dismiss the keyboard" from anywhere in your app. You might wanna give it a try:
[[[[UIApplication sharedApplication] delegate] window] endEditing:YES];

Add the following line where you are trying to dismiss the keyboard:
[self endEditing:YES];

Related

Dismiss keyboard when default ios mail app opens

In my app I display the MFMailComposeViewController to display default mail app. How to dismiss the keyboard which opens up in the controller?
UIWindow* keyWindow = [[UIApplication sharedApplication] keyWindow];
UIView* firstResponder = [keyWindow performSelector:#selector(firstResponder)];
[firstResponder resignFirstResponder];
I hope This Will Help You.. :)
Unfortunately it look's like even if we try to force close the keyboard using endEditing; that function takes no effect. On my device it doesn't seem like the keyboard can be dismissed.
Below would have been the answer that should have worked:
Display the MFMailComposeViewControllerwith a completion and call endEditing: on the MFMailComposeViewController's view.
MFMailComposeViewController *mailVC = [[MFMailComposeViewController alloc] init];
[self presentViewController:mailVC animated:YES completion:^{
[mailVC.view endEditing:YES];
}];
UPDATE:
Wouldn't suggest doing this but, here is a hackish way that works:
Objective-C:
MFMailComposeViewController *mailVC = [[MFMailComposeViewController alloc] init];
[self presentViewController:mailVC animated:YES completion:^{
UITextField *a = [[UITextField alloc] init];
[mailVC.view addSubview:a];
[a becomeFirstResponder];
[mailVC.view endEditing:YES];
}];
Swift:
let mailVC = MFMessageComposeViewController()
self.presentViewController(mailVC, animated: true) { () -> Void in
let a = UITextField()
mailVC.view.addSubview(a)
a.becomeFirstResponder()
mailVC.view.endEditing(true)
}
In your viewcontroller.h add:
#property (nonatomic) UITapGestureRecognizer *tapRecognizer;
Now in the .m file, add this to your ViewDidLoad function:
- (void)viewDidLoad
{
[super viewDidLoad];
//Keyboard stuff
tapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(handleSingleTap:)];
tapRecognizer.cancelsTouchesInView = NO;
[self.view addGestureRecognizer:tapRecognizer];
}*
Also, add this function in the .m file:
- (void)handleSingleTap:(UITapGestureRecognizer *) sender
{
[self.view endEditing:YES];
}

ABPeoplePickerNavigationController transparent top bar

I use standard ABPeoplePickerNavigationController and when I had dragged table with contacts I saw that top bar is transparent. I can't solve it. It looks awful.
I tried to set [UIColor whiteColor] to everything I can reach: navigationBar, all subviews of ABPeoplePickerNavigationController and all subviews of it's topViewController. I tried to set different bar styles to navigation bar. Nothing help.
Here is code
#interface MNFindClientVC () <ABPeoplePickerNavigationControllerDelegate>
#property (nonatomic, strong) ABPeoplePickerNavigationController *addressBookController;
-(void)openPhoneBook;
#end
#implementation MNFindClientVC
-(void)viewDidLoad
{
[super viewDidLoad];
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:#"phonebook"] style:UIBarButtonItemStyleBordered target:self action:#selector(openPhoneBook)];
self.addressBookController = [[ABPeoplePickerNavigationController alloc] init];
self.addressBookController.peoplePickerDelegate = self;
}
-(void)openPhoneBook
{
[self presentViewController:self.addressBookController animated:YES completion:nil];
}
#end
Sorry for my english. Thank you:)
I had the same problem when using a UINavigationBar that was translucent. You can solve this problem by disabling translucency of the UINavigationBar when in a ABPeoplePickerNavigationContrller with the following code (Tested in iOS 8):
_addressBookController = [[ABPeoplePickerNavigationController alloc] init];
[_addressBookController setPeoplePickerDelegate:self];
[[UINavigationBar appearanceWhenContainedIn:[ABPeoplePickerNavigationController class], nil] setTranslucent:NO];
[self presentViewController:_addressBookController animated:YES completion:nil];

Right Button won't show in custom Navigation Controller

My app is a tab bar app with many navigation controllers.
I want these navigation controllers to handle login/logout actions with a custom right bar button. So I've set up my tab bar in AppDelegate this way :
MyFirstViewController *firstViewController = [[MyFirstViewController alloc] init];
UIViewController *firstNavigationController = [[CustomNavigationController alloc]
initWithRootViewController:firstViewController];
MySecondViewController *secondViewController = [[MySecondViewController alloc] init];
UIViewController *secondNavigationController = [[CustomNavigationController alloc]
secondViewController]
initWithRootViewController:secondViewController];
....
[tabBarController setViewControllers:#[firstNavigationController, secondNavigationController]];
Then CustomNavigationController :
#implementation ConnectionNavigationController
- (void) viewDidLoad
{
[self displayConnectionButton];
}
- (void) displayConnectionButton
{
UIImage *portraitImage, *landscapeImage;
portraitImage = [UIImage imageNamed:#"conn.png"];
landscapeImage = [UIImage imageNamed:#"connLandscape.png"];
UIBarButtonItem *connButton = [[UIBarButtonItem alloc]
initWithImage:[portraitImage
imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]
landscapeImagePhone:[landscapeImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]
style:UIBarButtonItemStylePlain target:self action:#selector(showConnectionPopup)];
self.navigationItem.rightBarButtonItem = connButton;
}
#end
I tried with viewDidLoad, viewDidAppear, viewWillAppear but nothing works, I can't get this button to show in the navigation bar. I also tried to add reloadInputViews or setNeedsDisplay. What should I do?
Thanks for your help
Edit : interface of CustomNavigationController
#interface ConnectionNavigationController : UINavigationController
- (void) displayConnectionButton;
#end
From what I understand you should add the displayConnectionButton method to the relevant UIViewController subclass and not to your UINavigationController subclass as this only contains the stack of view controllers.
E.g.
#implementation MyFirstViewController
- (void) viewDidLoad
{
[self displayConnectionButton];
}
- (void) displayConnectionButton
{
UIImage *portraitImage, *landscapeImage;
portraitImage = [UIImage imageNamed:#"conn.png"];
landscapeImage = [UIImage imageNamed:#"connLandscape.png"];
UIBarButtonItem *connButton = [[UIBarButtonItem alloc]
initWithImage:[portraitImage
imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]
landscapeImagePhone:[landscapeImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]
style:UIBarButtonItemStylePlain target:self action:#selector(showConnectionPopup)];
self.navigationItem.rightBarButtonItem = connButton;
}
#end
As a note if you put a breakpoint on your displayConnectionButton in your CustomNavigationController and po self.viewControllers I think you will get an empty array (i.e. no view controllers)

ViewController add UITableView use scrollViewDidScroll popViewControllerAnimated crash

Here is all my code :
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
UIBarButtonItem *leftItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:#selector(pushTableViewController:)];
self.navigationItem.leftBarButtonItem = leftItem;
}
- (void)pushTableViewController:(id)sender {
TableViewController *tableViewController = [[TableViewController alloc]init];
[self.navigationController pushViewController:tableViewController animated:YES];
}
The TableViewController code is:
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
UIBarButtonItem *myMessageButton = [[UIBarButtonItem alloc] initWithTitle:#"back" style:UIBarButtonItemStylePlain target:self action:#selector(myMessageButtonClicked:)];
self.navigationItem.leftBarButtonItem = myMessageButton;
UITableView *scrollTableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height) style:UITableViewStylePlain];
scrollTableView.delegate = self;
scrollTableView.dataSource = self;
[self.view addSubview:scrollTableView];
}
- (void)myMessageButtonClicked:(id)sender
{
[self.navigationController popViewControllerAnimated:YES];
}
- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
}
Just use the most simplest way to use UITableView,and implement scrollViewDidScroll
when I scroll the UITableView to the bottom and then popViewControllerAnimated i got crash.
is this BUG of IOS? or where did i go wrong?
The problem is,tableview try to access scrollViewDidScroll delegate method during the time of animating viewController. so after reaching scroll at bottom, set datasource and delegate as Nil before popViewController.
try like this
ExampleviewController *example=[[ExampleviewController alloc]]init];
[self.navigationController pushViewController:example animated:YES];
check UIScrollView Delegate is declared are not

Create UIViewController programmatically with navbar along with done button and UITextView for ModalView Presentation

I am trying to create UIViewController programmatically for ModalView presentation on the main window of the application and in this UIViewController on top i have is Navigation bar with done button to dismiss the view and below the navigation bar it has UITextView.
So my question is that the UITextview which i want to create below the navigation bar will come in the ViewdidLoad Method or i m doing right to show separate medthod to setup textview .
In the Infoviewcontroller.h file have following code:
#import <UIKit/UIKit.h>
#interface Infoviewcontroller : UIViewController <UITextViewDelegate>{
UITextView *textView;
}
#property (nonatomic, retain) UITextView *textView;
#property (nonatomic, assign) UINavigationBar *navBar;
#end
Then in the infoviewcontroller.m file have the following code:
#import "Infoviewcontroller.h"
#implementation Infoviewcontroller
#synthesize textView;
#synthesize navBar;
-(void)dealloc{
[textView release];
[navBar release];
[super dealloc];
}
-(void)setupTextView {
self.textView = [[[UITextView alloc] initWithFrame:self.view.frame] autorelease];
self.textView.textColor = [UIColor redColor];
self.textView.font = [UIFont fontWithName:#"System Bold" size:13];
self.textView.delegate = self;
self.textView.backgroundColor = [UIColor whiteColor];
self.textView.textAlignment = UITextAlignmentCenter;
self.textView.text = #"This is UITextView\nThis is UITextView\nThis is UITextView\nThis is UITextView";
[self.view addSubview: self.textView];
}
- (void)viewDidLoad {
[super viewDidLoad];
navBar = [[UINavigationBar alloc] init];
UINavigationItem *navItem = [[[UINavigationItem alloc] initWithTitle:#"ModalViewControllerTest"] autorelease];
UIBarButtonItem *done = [[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:#selector(dismissView:)] autorelease];
navItem.rightBarButtonItem = done;
navBar.items = [NSArray arrayWithObject:navItem];
[self.view addSubview:navBar];
}
Regarding creation of UINavigationBar programmatically, I found following blog post helpful http://cps.liridesce.com/?p=100. Example overrides loadView rather than viewDidLoad method, but I think in your case using viewDidLoad should have the same effect.
I would also add viewDidUnload where I would set to nil navBar and textView properties.
I'm not quite sure why you need separate setupTextView method, unless you are calling it from different places of your UIViewController. I would just keep everything together in viewDidUnload.

Resources