how to inactivate segue in Swift - ios

Hi guys i made an app and what i want to do is
when you click "select" button in navigation bar item, i want to make it to click multiple items and the code for that is just to enable collectionView.allowsMultipleSelection = true which works.
click to view an app image
but at the same time i want to make if the user didn't click the select button but the item, i want to show next view controller.
so now, whenever i clicked the image, it directly shows the next view controller since i connected them in storyboard. i want to inactivate it when the "select" button is clicked.
how? do i have to use the prepare func? and not use the storyboard? please tell me the simplest way to do it
thanks

One simple way of solving this:
Connect the entire view controller that contains the images with the other view controller through a segue.
Add an identifier to this segue, which you will use later.
Create a function that segues to that view controller when an image is pressed. - Up to here is just another way of implementing the code that you have implemented
Create a boolean variable, which is set to false when the select button is on ( and the opposite )
Add an if check to your function in number 3. that only segues when the boolean variable is true.
This ensures that if the select button is pressed, you don't segue when images are clicked.

Related

Xcode / Swift: How I implement a back button?

I just started with Xcode and Swift.
I try to build my first little App for iOS. But now I have the problem, that I don't know how to implement a the back button, so that i come back to the view before.
My Storyboard look like this:
When I open the A-Z view, I want to display the Back Arrow, which turn me back to the Item 2 view.
To open the A - Z view I connect the button "Medikamente A - Z" with the Navigation Controller.
When using storyboards the back button is usually implemented with unwind segue.
I usually like to follow raywenderlich toturials on UI related topics, like this - http://www.raywenderlich.com/113394/storyboards-tutorial-in-ios-9-part-2
It include a detailed example of how to implement back button in storyboards. Quoting from it -
Storyboards provide the ability to ‘go back’ with something called an unwind segue, which you’ll implement next.
There are three main steps:
1. Create an object for the user to select, usually a button.
2. Create an unwind method in the controller that you want to return to.
3. Hook up the method and the object in the storyboard.
When using UINavigationController, whenever you push to a new ViewController the back button will automatically appear, so you can jump back to the previous View Controller.
So it's works like:
UIViewController -> UIViewController -> UIViewController
A back button will appear on the last 2 so you can pop back the the previous ViewController.
You don't have to do any additional coding for the back button to appear, it'll do it on its own. I hope this clears it up. Let me know if you have any questions.
To implement a back button, your root view controller has to be a Navigation Controller.
The first view controller becomes the navigation root of the navigation controller.
If you want to present another view controller, you select a "Show Detail" relationship as the action for the button which should show the view controller. To do this, Ctrl-click and drag from the button to the destination view controller and select "Show Detail".
I had the same problem, even when on the storyboard the back button was visible at design time.
I deleted the segue, and recreated it with "Show" instead of "Show detail". Changing the segue to "Show" had no effect. I think this is a bug, so if you miss that back button delete and recreate the segue.

How to connect two view controllers to one button in storyboard?

Up to now, there is a button in a view controller (let's call it Main View Controller). What I want is: if it satisfies a certain condition, when I press the button, the segue would lead to View Controller A; if not, when I press the button, it would lead to View Controller B.
But it seems that one button can only have one segue from it, so I wonder whether what I want is possible to be achieved in storyboard???
Thanks in advance!!!
If this question is too basic for you, I am so sorry. I am still a very starter in iOS programming.
You need to create a segue from the viewController itself to the destination. You can do this for each viewController and give them appropriate identifiers.
Then you hook up your button with an IBAction and decide which segue to perform

Unwind segue function with Back button

I'm trying to call function when i'm pressing default Back button (e.g. < Settings) in Navigation Controller.
At other situations i'm creating custom button, after i'm Ctrl+Dragging to red Exit button in XCode and pointing it to Selection segue.
Now i need to use default back button of navigation controller, but i can't set assign it to segue action because it's no back button in Storyboard view (but it exists in Simulator).
Simulator:
XCode:
I can create custom button in navigation contoller and assign it so custom unwind segue, but that button will be ugly without "< " symbol.
I've found some answers at stackoverflow, but they all are written in Objective-C :(.
You can use a delegate method to accomplish what you are doing.
Here is a really thought out swift tutorial that deal with delegates and segues:
http://makeapppie.com/2014/07/05/using-delegates-and-segues-part-2-the-pizza-demo-app/
What you are asking, ability to catch the back button's action, isn't possible the way you describe it.
Most people would recommend creating a custom button, then use an unwind segue.
There are lots of tutorials on custom back buttons just in case you're new to swift, and the ugly part of the custom back button can be easily fixed by putting your own arrow there.
Also I found this previous asked question for future reference: Unwind segue from navigation back button in Swift
Actually I just assigned the unwind segue to the entire view controller (right click from the yellow view controller outlet and drag to the exit outlet then select the function). And it works great so far. No need custom button, no delegate.
In fact, in my project the child is supposed to call the different parent's function according to current caller view controller and simply using the same function name made my dreams come true.
This page helped me a lot: http://spin.atomicobject.com/2014/12/01/program-ios-unwind-segue/

Call segue with Disclousure indicator won't work

My question is very similar to this:
Disclosure Indicator doesn't call seque while Detail Disclosure does
But is a bit different.
The same is happening to me. I have a dynamic table view which loads some rows at viewLoad.
So I set the segue from the Cell to a new View which will show some details of the item. I also set the Segue to Push.
When I do that, the Cell automatically sets its Accessory to Detail Indicator (So the blue info button appears) This works fine, I click on the info button and it loads the new view. But I do not want that, I want the Cell to have a Disclousure indicator (the arrow) and whenever you click on the entire row, just Push the new view. I did this on an Static Table View and it worked fine, but I can't make it work with a Dynamic Table. Nothing happens when I click, just the animation of the tap and nothing else.
No, it's not ok to change an accessory action segue to a selection one by setting the accessory to none -- that doesn't change the segue type. The fact that you got the blue button when you made the segue means you chose an "Accessory Action" segue. When you drag from the cell to the next controller and let go, you get a menu of choices that's divided into two sections. The top ones are Selection segues, and the bottom set are Accessory Action segues. You need to delete your segue and remake it, being sure to choose from the Selection segue choices.

Want to simulate More Navigation Item behavior in viewcontroller

I have a view that can either be shown via another view or via the More tableview list of more controllers.
In the first case, I added a More button on the Navigation bar to take them back to the More view list, but what is the code that I put on the More button to take the user back to the More list?
Thanks
If you want to customize your backBarButtonItem check this SO question.

Resources