Swift: why does "performSegueWithIdentifier" happen twice? - swift

In my app I have the following storyboard fragment:
and from the pink button I have added a show segue called "toFishPic" to the UIViewController at the rigth. This is the swift code linked to the pink button:
#IBAction func toFishPhoto(sender: UIButton) {
dispatch_async(dispatch_get_main_queue()) {
self.performSegueWithIdentifier("toFishPic", sender: self)
}
}
I don't know why, but when the pink button is tapped the UIViewConroller at the right (the one with the big UIImageView) is loaded twice as if there was one identical after it. Why does this happen?

I think you are linked pink button to PhotoViewController directly instead ActionFishViewController to PhotoViewController. If you gonna use performSegueWithIdentifier, don't link directly and reconnect between controllers.
Try to dont call self.performSegueWithIdentifier("toFishPic", sender: self) (//) and check if you can push to PhotoViewController. If it`s works means you linked button directly to PhotoViewController.

Related

How to detect a click from an NSToolbarItem inside an NSWindowController?

I have a toolbar on my macOS app, developed in Swift. The toolbarItem is dragable onto the NSWindowController, and I can setup an IABAction function, I just have a print in the function at the moment. And when I click on the button nothing happen the click does not seem to be recognised as an action ?
I had a few more line of code in the function but deleted it and now have just the print("test") line.
#IBAction func exportCsvClicked(_ sender: NSToolbarItem) {
print("test") }
No output observed, so I'd love to get "test" in the console when I click on this button.
Here is a list of the connections associated with the toolbarItem.
I found a way to get around the fact that the IBAction from an NSToolbarItem does not recognise the click on this item:
1/I made a customSegue from the item to the main window controller (it can go anywhere)
2/The prepare for segue function posts a notification to the notification saying that the item has been clicked.
3/The main view controller observes the notification and presents, either has a popup or a sheet (I got two buttons), the view that I have setup in the storyboard (referencing with the storyboardID). I found that you need to pass on all the necessary variable to setup the view from the main view Controller, and that there was issue of code in the viewDidLoad function of the sheet/popup view not running, I am suspecting that they might be different instances.

Button ignores action when segued to a previous viewcontroller

So here is my current issue.
I want to unwind to a previous viewcontroller lets call it viewcontroller1.
This works fine, except that the button action
that is hooked up with the segue, also has a lot of other code written in it. However, when pressing the button xcode ignores the code thats already written.
So this is my question how do I make the button perform the written code and then engage the segue??
For the unwind segue I've created an empty action in ViewController1.
#IBAction func unwindtoViewcontroller1(segue: UIStoryboardSegue){}
And hooked ViewController2 up with this segue action (using ViewController2:s exit button).
Ive also set the segue to "revealviewcontrollerpushviewcontroller"
Thanks in advance for all the help I can get I am really stuck.
Best regards Albin
You should be able to do it this way:
Open Assistant Editor, then control drag from the button that performs the unwind segue in ViewController 2 to create an IBAction. That's where all the code for the action you would like your button to do should go.
This action gets performed before the unwind segue function is called.
So your button will do both: action and unwind.
I have made a simple project to illustrate and printed the output to the console. The button in yellowVC performs an action before unwind.
So as you said, in the first ViewController (in my example the green one you have an IBAction for unwind segue
#IBAction func goBackToGreenVC(segue: UIStoryboardSegue) {
print("go back")
}
Then in the second ViewController (in my case the yellow one), control drag from the button to Exit to set unwind segue and then also control drag from the button to Assistant Editor for an IBAction, something like:
#IBAction func goBackToGreenVCButtonTapped(_ sender: UIButton) {
print("performing an action before unwind")
}

Unwind Segue Navigation Bar Button Xcode 8.0 Swift 3.0

I want to create an app in Xcode 8.0 that lets the user press a bar button item in the navigation bar. Once this button has been clicked an unwind segue will take place whilst also printing, "helloWorld" to the output section at the bottom of Xcode. I have successfully created an unwind segue using:
#IBAction func unwindToViewController (sender: UIStoryboardSegue){
}
I then linked this to the bar button item on the navigation bar so that when I tap the bar button item on the navigation bar the view disappears downwards.
I am trying to simply print("helloWorld") onto the output section at the bottom of Xcode when I press the done button but nothing appears on the output section. So far I have:
#IBAction func add(_ sender: AnyObject) {
print("helloWorld")
}
in the View Controller Swift File for that View Controller.
For unwind We need to remember only two points
1) We need to put below method into Destination ViewController.It is a place where we want to navigate to. Here method name can be different but arguments matter a lot.
#IBAction func unwindToDestinationViewController (sender: UIStoryboardSegue){
}
2) Go to the Source viewController from where you want to go back. Now click on unWind button on top right and Control-Click and set outlet to the Back button(Example)
Kindly check this post on stack overflow. This is a duplicate of below post.
You can find the answer in the given below link.
https://stackoverflow.com/a/39916477/2894335
This Blog gives a very nice introduction to unwind segues. article link here.
Try the following tutorial will fix your problem and help you to understand more about how to perform unwind segue...
https://www.andrewcbancroft.com/2015/12/18/working-with-unwind-segues-programmatically-in-swift/#identifier
http://ashishkakkad.com/tag/uistoryboardsegue/
Hope this helps...

How to access a Storyboard's segue through code?

There are couple for segues in the storyboard, all with identifier. Is it possible to access them through code and change its anchor?
Something like this:
mySegue1.anchor = someOtherView
try this
self.performSegueWithIdentifier("YourSegueIdentifier", sender: self)
I think you may chnage anchor's view controller with the following codes, as I wrote in above comments, I believe you can create a Segue programmatically and the following code will create a new Segue for you not just change an old already drawn Segue's anchor programmatically.
Most of the good programmers think that you can not create a Segue by code, thats why changing a Segue anchor is not believed to be a performable job. But it is
It is an 'Objective C' version but you may easily convert it to 'Swift' if you like.
UIViewController *toViewController = [self.storyboard instantiateViewControllerWithIdentifier:#"OtherViewControllerId"];
MySegue *segue = [[MySegue alloc] initWithIdentifier:#"" source:self destination:toViewController];
[self prepareForSegue:segue sender:sender];
[segue perform];
However if you need to create a popover menu using a gesture recognizer, then just create a gesture recognizer and activate your pop over in gesture's action func ( handleTap() )
take a look at the following link, you may create your popover in storyboard and then write the rest of the code or completely do it by code, both are fully covered here.
http://gracefullycoded.com/display-a-popover-in-swift/
as I understood which I hope I get it right, you are about to create a pop over on your view controller which has a tab gesture defined on it. and you succeed to do the job but the pop over menu is mislocated. if this is the case You dont need to use a segue or a storyboard, all parts can be done by code easily.
What you can do is making an invisible anchor frame (ie a label ) and using that as anchor for the segue. I had som buttons and use this approach and it works fine. If you replace my buttons with you dynamically created objects.
#IBAction func rearHubActionUIButton(sender: UIButton) {
invisibleAncherLabel.frame = sender.frame
performSegueWithIdentifier("PopSelection", sender: sender)
}
The popover will then appear with your object as anchor.

How to return from "partial curl" type segue

I am attempting to use a partial curl segue to reveal an options view underneath my main view. This is identical to touching the "page curl" button in the bottom right corner of the Maps app.
I can successfully execute the segue to the option view, my problem is returning to the main view. I'm hoping to replicate the reverse animation effect of the Maps app as the edge curls back flat and the main view is shown.
It doesn't seem logical to create another segue to return back to the main view (what I would do if I were not using the partial curl segue) because the return segue is associated with the original segue.
Thanks in advance for any help!
try
-(IBAction) buttonPressed{
[self dismissModalViewControllerAnimated:YES];
}
This should work!
Or i believe you can press the back side of the turned page and it will return automatically with out adding any code! Hope that helps!
In Swift dismissModalViewControllerAnimated has been renamed:
#IBAction func handleAddButton(_ sender: Any) {
self.dismiss(animated:true, completion: {})
}