How do I close the first window when the next one opens? - swift

Xcode 9.2, macOS 10.12.6, Swift 4. I don't really know what I'm doing, so please explain what to do in detail.
I am trying to make it so that the first window is closed when the second one is opened. The buttonCONTINUE makes the second window open, via show segue.
I followed the Control+Click and drag as explained here, and tried to make the CONTINUE button close the first window when closed in two different ways; with self.view.window?.close() and with setting the key equivalent to CMD+W.
I've tried the solution suggested here, but that did not solve my problem.
Edit:
I have two windows, one each goes to the other one. Here is the code:
override func prepare(for segue: NSStoryboardSegue, sender: Any?) {
super.prepare(for: segue, sender: sender)
if segue.identifier!.rawValue == "SegueToWIR" {
view.window?.close()
}
//neither if or else if make this work
if segue.identifier!.rawValue == "SegueToWarning" {
view.window?.close()
}
}
The second if statement doesn't cause an error, but doesn't do anything.

Unlike UIKit's UIControl, AppKit only permits a single target-action per NSControl instance. You are attempting to use two:
The show segue.
The action method connection you created using the control + click & drag method.
The segue takes precedence. You can verify this by setting a breakpoint at CONTINUE(_:). You'll find that the action method never gets called!
So scrap the action method &, alternatively, use:
📌 Note: The below implementation is valid for Swift 4.2+.
override func prepare(for segue: NSStoryboardSegue, sender: Any?) {
super.prepare(for: segue, sender: sender)
// Note: For Swift 4, replace `segue.identifier` with `segue.identifier?.rawValue`.
if segue.identifier == "my-segue-identifier" {
view.window?.close()
}
}

Related

How can I get access to the systemItem information of the sender parameter of the function prepare for segue?

By unwinding a segue I use the function prepare(for:sender:) to identify which button was pressed to run some code based on the outcome;
the information should be available within the parameter sender but I'm not able to read out the needed information although I can see the needed information would be there (systemItem=Save) when checking the debugger.
The debugger shows me the content of "sender":
(lldb) po sender
â–¿ Optional
- some : target=0x600002e6aec0 action=perform: systemItem=Save
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
// Get the new view controller using segue.destination.
// Pass the selected object to the new view controller.
print("prepare called")
}
What I would expect is that I can check if the button clicked was "Save" and perform some code if true; How do I get a handle on the information needed?
You cannot get button info from prepareForSugue... func because this is not target. If you want to get pressed button info you may add target to your button with passing sender parameter like :
button.addTarget(self, #selector(handlePress(_:)))
#obj private func handlePress(_ sender: UIButton) {
// Do your logic here with button then
self.perform(segueWithIdentifier: String)
}
PrepareForSegue method will call automatically after you calling preform(segue...) func

Slow performSegue in tableview didSelectRowAtIndexPath - iOS12

Has anyone experienced this? I'm, not 100% certain that this is iOS12-related but calling performSegue inside didSelectRowAtIndexPath has a delay of like 1-2 secs.
I already tried different things that I found elsewhere like bringing it to the main thread but nothing works. Not sure if this is a bug or not but I haven't seen anyone talking about it online.
Try your code inside main thread:
DispatchQueue.main.async{
self.performSegue(withIdentifier: "YourSegueName", sender: self)
}
This worked for me, As sometimes we can not get e main thread which is important if you are working with some UI stuff.
Are you using the prepare Method? if so, what are you doing before the Segue?
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "ExampleSegue" {
let ChangeVC = segue.destination as! ExampleViewController
...
}
}
Have you tried to hand over the index path of your selected Row to a different ViewController? And decide there what to do?

Perform UnwindSegue correctly

I am trying to create an Unwind segue that brings from the DetailViewcontroller to the SecondViewcontroller.
I have created an IBAction like this one that can be intercepted by Segues :
#IBAction func segueToSecondVC(sender: UIStoryboardSegue) {
print("segue intercepted by IBAction segueToSecondVC")
}
An placed it in the receiver ViewController (secondViewController).
Then I moved to the Detailviewcontroller and clicked to the third figure on the top of the viewcontroller (exit),clicked to the segueToSecondVC IBAction that appeared in the list of events and dragged it into the button "TURN BACK"(placed in the Detailviewcontroller) in order to associate the button to the event.
In this way the button should be connected with the event and should turn back to the secondVC with an automatic Hidden Segue.
But it doesn't work, the simulator crash and I am redirected to xCode editor with an error in the Appdelegate that says "Thread 1: signal SIGABRT"
I don't know why it doesn't work, anyone could help me?
you can use Unwind like this:
#IBAction func prepareForUnwind(segue: UIStoryboardSegue){}
#IBAction func closePressed(_ sender: Any) {
performSegue(withIdentifier: "unwindToAnotherVC", sender: nil)
}
good rule, is to add all your segues names to constants file:
Create swift file
import Foundation
// Segues
let TO_LOGIN = "toLogin"
let TO_CREATE_ACCOUNT = "toCreateAccount"
let UNWIND = "unwindToChannel"
and then use UNWIND in
#IBAction func closePressed(_ sender: Any) {
performSegue(withIdentifier: UNWIND, sender: nil)
}

calling performSegueWithIdentifier doesn't work

i have this function in a class, that posts NSNotification, once the user has been identified and the match is prepared to begin. then i have an observer in the mainViewController that triggers this segue:
func startMP(){
performSegueWithIdentifier("MPVC", sender: nil)
}
but it doesn't work. what am i doing wrong?
pd. the authenticationViewController is dismissed once the match is prepared to start.

No Segue with Identifier Error in Swift?

I am trying to use the performSegueWithIndentifier function to to create a segue from one ViewController to the next. But, when I press the button with the UITApGestureRecognizer connected to it, the View shifts to the debugger panel.
Here is the error it is displaying:
ContaminateTargetViewController: has no segue with identifier 'showMasterChemistryViewController'
(I cut out the personal Info)
Here is the ViewControllers Class:
import Foundation
import UIKit
class ContaminateTargetViewController: UIViewController {
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "showMasterChemistryViewController" {
let chemistryMasterViewController = segue.destinationViewController as! ChemistryMasterViewController
}
}
#IBAction func showPlaylistDetail(sender: AnyObject) {
performSegueWithIdentifier("showMasterChemistryViewController", sender: sender)
}
}
I also previously had to manual segues from the 2 buttons I have on the ViewController and recently deleted them to switch over to the UITapGestureRecognizer for the convenience. I am wondering if I have an error in my code that I do not see or if previously deleting the manual segues from the View is causing this error. If the problem is rooting from the previously deleted manual segues please tell me how to fix this in your error. If the problem is rooting form the code, please leave the code I should add, delete, or substitute.
Any suggestions or input is greatly appreciated.
Thanks in advance.
This error is called when you do not have the requested segue connected to your view controller. Are you sure that you have the two view controllers connected via a segue named "showMasterChemistryViewController" on your storyboard?