How to open the keyboard automatically on UITextField? - iphone

I have a very simple table and when tocuh a cell it opens a new view with one UITextfield. All I want is that the keyboard will automatically opens, without the user have to touch the UITextfield.
Its all done in Interface Builder, so I am not sure how I do this. I guess I need to set the focus at some point ?
Thanks

To cause the keyboard to show up immediately you'll need to set the text field as the first responder using the following line:
[textField becomeFirstResponder];
You may want to place this in the viewDidAppear: method.

Swift 3 & 4:
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
textField.becomeFirstResponder()
}

override func viewDidLoad() {
super.viewDidLoad()
textField.becomeFirstResponder()
}

Prefer adding the first responder on the main thread -
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
self.textField.becomeFirstResponder()
}
This will come in handy when the view controller view is added as a subview.

Related

Swift: Make UIViewController print off the subclass type upon hitting the viewWillAppear or viewWillDisappear functions?

The title basically asks my question for me. I have a number of view controllers which I'm loading in or dismissing as a user goes through the app. I'm having some asynchrony issues with the dismiss calls being called potentially too much. It's been a bit of a doozy going through the code base and finding what is being called where. I'd like to just print off whenever a new UIViewController hits the points in it's lifecycle functions viewWillAppear() or viewWillDisappear(). Is there a way for me to extent the UIViewController in such a way that all of its subclasses will naturally do this? Or would I have to go through each subclass to add in that code?
Thank you!
You could create a new UIViewController base class with the following:
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
print("\(type(of: self)) will appear")
}
override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
print("\(type(of: self)) will disappear")
}
You'll have to inherit from this class on any view controller whos events you want to log

viewDidDisappear not showing ad

Im using startApp to display ads but when the view disappears it doesn't show the ad. I have startAppAd = STAStartAppAd() in viewDidLoad() I'm not quite sure what is going wrong.
override func viewDidAppear(_ animated: Bool) {
startAppAd?.load()
}
override func viewDidDisappear(_ animated: Bool) {
super.viewDidDisappear(animated)
startAppAd?.show()
}
viewDidDisappear is called when the vc is about to dimiss or popped , so any property is in it's way to deallocate if that vc is not strongly linked , so move the show method also inside viewDidAppear or in the ad load finish callback if any

How to Stop a VCs functions from operating when VC is not in view? [Swift 3.0 Xcode]

I have a view controller which contains functions wish I need to disable once I leave the view controller. The functions wont start until I navigate to the VC, which is what I want, but I also what these functions to stop once I leave and navigate to other view controllers. Does anyone know any tricks to this?
there are multiple ways you can do this.
One like others have commented is invalidate timers or location stuff in either one of these methods.
let someTimer = Timer()
override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
someTimer.invalidate()
}
override func viewDidDisappear(_ animated: Bool) {
super.viewDidDisappear(animated)
someTimer.invalidate()
}

Dismiss keyboard for textfield in uitableviewcell in tableviewcontroller - Swift

I have a textfield and textview in my custom tableviewcell.
I have 4 different prototype cell with 4 different class created. there is a textfield in 1 prototype cell and a textview in the other.
I am not sure how I can do it and I dont understand the obj-c answers out there.
I've tried
override func viewDidLoad() {
super.viewDidLoad()
let tap: UIGestureRecognizer = UIGestureRecognizer(target: self, action: #selector(UIInputViewController.dismissKeyboard))
view.addGestureRecognizer(tap) }
and
func dismissKeyboard() {
self.view.endEditing(true)
}
I wanted to try
UITextFieldDelegate and touchesbegan and textfieldshouldreturn method, but there are no textfields to call in my tableviewcontroller.
Go to attributes inspector in storyboard and click tableView and set keyboard to dismiss interactively.
Or
Set textField delegate and implement
override func viewDidLoad() {
super.viewDidLoad()
textField.delegate = self
}
func textFieldShouldReturn(textField: UITextField) -> Bool // called when 'return' key pressed. return false to ignore.
{
textField.resignFirstResponder()
return true
}
I had same problem, you can solve this with IQKeyboardManager, you don't need to complicate things with the custom cell classes, just install it on your project, when you're all set with installation, in the appDelegate inside the didFinishLaunchingWithOptions method, add this line of code:
IQKeyboardManager.shared.shouldResignOnTouchOutside = true
Get IQKeyboardManager from here.

How can i identify self.editButtonItem button's clicked event?

I set my left bar button of UINavigationController as edit button using the code
leftBarButton = self.editButtonItem;
I want to change some disable/enable properties of other buttons with respect to the edit button's click action.
How can i find whether the Edit button is pressed or not?
The edit button's action sends your view controller the setEditing:animated message. Override this in your subclass to perform other actions when entering or leaving edit mode.
Be sure to call the super implementation at the end to manage the rest of the transition to editing view.
So finally i got the solution...
-(void)setEditing:(BOOL)editing animated:(BOOL)animated {
[super setEditing:editing animated:animated];
if(editing) {
//Do something for edit mode
}
else {
//Do something for non-edit mode
}
}
This method will be called with out changing the original behavior of self.editButtonItem button.
In Swift:
#IBOutlet weak var tableView: UITableView!
override func viewDidLoad() {
super.viewDidLoad()
....
self.navigationItem.leftBarButtonItem = self.editButtonItem()
}
override func setEditing(editing: Bool, animated: Bool) {
// Toggles the edit button state
super.setEditing(editing, animated: animated)
// Toggles the actual editing actions appearing on a table view
tableView.setEditing(editing, animated: true)
}
In Swift you can follow the below methods:
#IBOutlet weak var tableView: UITableView!
override func viewDidLoad() {
super.viewDidLoad()
navigationItem.rightBarButtonItem = editButtonItem()
}
override func setEditing(editing: Bool, animated: Bool){
super.setEditing(editing, animated: animated)
tableView.setEditing(editing, animated: true)
}
UIBarButtonItem *barBut=[[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemEdit target:self action:#selector(doSomething)];
self.navigationItem.leftBarButtonItem=barBut;
[barBut release];
.h
-(void)doSomething;
.m
-(void)doSomething{
NSLog(#"dooooooooooooo");
//ur stuff
}
updated:
- (void)setEditing:(BOOL)editing animated:(BOOL)animated
will be called
editButtonItem
Returns a bar button item that toggles its title and associated state between Edit and Done.
- (UIBarButtonItem *)editButtonItem
Discussion
If one of the custom views of the navigationItem property is set to the returned object, the associated navigation bar displays an Edit button if editing is NO and a Done button if editing is YES. The default button action invokes the setEditing:animated: method.
Availability
Available in iOS 2.0 and later.
See Also
#property editing
– setEditing:animated:
Declared In
UIViewController.h
http://developer.apple.com/library/ios/#DOCUMENTATION/UIKit/Reference/UIViewController_Class/Reference/Reference.html