dismissViewController with button in swift - swift

So I am wondering what func to use for closing a detailed ViewController opened from a tableView. Currently I have it working to close the view, but the touches event occurs when you touch anywhere on the screen, not with a specific button. I have tried just using the button on storyboards but no luck. Below is what I have working so far (again not button specific)...
//DetailedViewController.swift
override func touchesBegan(touches: NSSet, withEvent event: UIEvent) {
self.dismissViewControllerAnimated(true, completion: nil)
}
Any and all feedback or direction is greatly appreciated!

#IBAction func buttonAction(sender: AnyObject) {
self.dismissViewControllerAnimated(true, completion: nil)
}
Put the button in the storyboard view controller and right-click-drag from the little circle for this action to the button. Alternatively, create your button programmatically and call addTarget(self, action:"buttonAction:") on the button.

Related

How to dismiss all presented view controllers when user KILL the app?

I am making a twitter-like app for assignment and have to make the app stay logged in until user log out.
I have "Login View Controller" which has a segue to "Home Table View Controller".
In my loginviewcontroller.swift, I have following code
override func viewDidAppear(_ animated: Bool) {
if UserDefaults.standard.bool(forKey: "userLoggenIn") == true{
self.performSegue(withIdentifier: "loginToHome", sender: self)
}
}
After I login the app (making UserDefaults.standard.bool to be true and go to Home table view), I kill the app and re-open the app, but the viewDidAppear function is not called. I guess this is because the Home table view is not dismissed? How do I fix it?
I will suggest you to Use scene delegate method like
func sceneDidDisconnect(_ scene: UIScene) { }
or
func sceneDidEnterBackground(_ scene: UIScene) { }
like so you can save some data. I usually save Context for CoreData.

How to hide the cursor in Swift?

I'm trying to make a first person game where moving the mouse changes the camera angle, similar to in Roblox in first person mode or in Minecraft. How to do that? I'm asking how to do basic functionality so there is no code to provide. I just need to know how to hide and lock the cursor position in Swift or more specifically Metal or Cocoa.
You can subclass NSView, add a tracking area for mouse entered and exited, override NSView mouseEntered and mouseExited methods and just hide and unhide the cursor there:
import Cocoa
class HiddenCursorView: NSView {
override func awakeFromNib() {
addTrackingArea(NSTrackingArea(rect: bounds, options: [.activeAlways, .mouseEnteredAndExited], owner: self, userInfo: nil))
}
override func mouseEntered(with event: NSEvent) {
NSCursor.hide()
}
#objc override func mouseExited(with event: NSEvent) {
NSCursor.unhide()
}
}

why is my custom UIButton subclass not triggering touch up IBAction?

this probably has something to do with the fact that I am overriding touchesBegan in my custom subclass:
class myCustomButton: UIButton {
override open func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
super.touchesBegan(touches, with: event)
print("TOUCHES BEGAN OVERRIDE CALLED")
originalColor = self.backgroundColor ?? .red
self.backgroundColor = .white
self.animateScale()
setNeedsDisplay()
}
//...
}
touchesEnded is similarly overridden, and both of these functions are called and print debug statements to console on button press. however, the code in the button pressed IBAction from my view controller is never run
#IBAction func nextButtonPressed(_ sender: Any) {
print("this is never executed")
//...
}
I have double checked that the IBOutlet is not broken and changed the (_ sender: Any) to (_ sender: AnyObject) both to no avail. any insights would be appreciated
ah ok, figured it out. in my custom button initializer I added a subview which, by default, had userInteractionEnabled set to true. setting userInteractionEnabled = true on my custom button object and userInteractionEnabled = false on my view before adding it as a subview allows my custom button to capture the touch and handle it properly rather than the view capturing the touch
Instead of overriding touchesBegan, try overriding the sendAction function of your UIButton:
override func sendAction(_ action: Selector, to target: Any?, for event: UIEvent?) {
super.sendAction(action, to: target, for: event)
//Insert your code here
}

why touchesBegan event in 1stVC triggered by touch from 2ndVC?

I met a very strange thing:
in storyboard has to ViewControllers : 1stVC and 2ndVC,
in 1stVC (Initial VC in storyboard) only have 2 functions:
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
//when touch view in 2ndVC,it will exec here!!!
super.touchesBegan(touches, with: event)
NSLog("xxx")
}
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
//switch to 2ndVC
performSegue(withIdentifier: "2ndVC", sender: nil)
}
Because app starts from 1stVC,but at viewDidAppear func it's soon to go to exec performSegue() func to switch to 2ndVC;now app should be in 2ndVC,but when I touch the screen (at 2ndVC's view), it will jump to the 1stVC's touchsBegin func (exec NSLog("xxx")).
How could that happen!? please help me,thanks!!!

How to open the keyboard automatically on UITextField?

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.