I have this two controller liked by segue:
My problem is that:
when I tap on selected table view'cell in general I have to follow the segue and so go to other controller , but if a specific condition is true I have to show ad alert view and so I don't follow the segue and so don't go to those controller.
In this way when I tap on selected cell I go always on the other controller.
How do I solve this problem?
if you override this function segue wont continue if you return false, this gives you the oportunity to show a warning under certain conditions, after that you can performSegueWithIdentifier("segueidentifier", sender: self), and your good to go.
override func shouldPerformSegueWithIdentifier(identifier: String!, sender: AnyObject?) -> Bool {
if identifier == "segueidentifier" {
//show warning and perform segue with this identifier on the accept button listener.
return false
}
}
return true
}
You can add a segue from one controller to another without specifying exact view which triggers the segue. Then just check your condition in didSelectRow method and call performSegue method if you need to show new view controller:
if condition {
performSegue(withIdentifier: "MySegueIdentifier", sender: nil)
}
else {
//show ad here
}
To add a segue between controllers just drag with right mouse button from one controller to another and pick "Show".
Then select segue and add a string identifier for it:
Make sure you use the same string in the code when you call performSegue method.
Related
I have more than 20+ buttons in my application. For these buttons if I click twice it performSegue twice and opens the viewcontroller twice, throughout my application in swift iOS?
self.performSegue(withIdentifier: "toViewController", sender: nil)
You can attach a tag to each button and define an array of segues
self.performSegue(withIdentifier:segues[sender.tag], sender: nil)
or simply make the button the source of the segue , if you're not willing to override prepareForSegue
You could declare a property
var isSegueEnabled = true
Then implement the method to control whether the segue should be performed
func shouldPerformSegue(withIdentifier identifier: String, sender: Any?) -> Bool {
return isSegueEnabled
}
In prepare(for segue set the property to false
isSegueEnabled = false
At some point after the presented view controller has been dismissed set isSegueEnabled back to true
I sometimes manually invoke segue using
self.performSegue(withIdentifier: "SegueID", sender: self)
The problem is sometimes the function containing above line is invoked twice in succession. This causes segue animation to be aborted in the middle and restart, which looks weird. How do I check if segue animation is already in progress?
The method shouldPerformSegue(withIdentifier:sender:) is called automatically to check if the segue you try to execute should really be performed:
func shouldPerformSegue(withIdentifier identifier: String, sender: Any?) -> Bool
Determines whether the segue with the specified identifier should be performed.
The Bool return value determines if the segue will be performed:
true if the segue should be performed or false if it should be ignored.
Try to incorporate one of the following checks inside shouldPerformSegue(withIdentifier:sender:) and return true or false depending on the outcome:
Check if the segue's destination view controller is already loaded into memory with isViewLoaded?
override func shouldPerformSegue(withIdentifier identifier: String, sender: Any?) -> Bool {
// check if the destination view controller is not yet loaded into memory
if !target.isViewLoaded {
return true // perform the segue
}
return false // do not perform the segue
}
Check if the segue's destination view controller is already presented with isBeingPresented?
override func shouldPerformSegue(withIdentifier identifier: String, sender: Any?) -> Bool {
// check if the destination view controller is not presented yet
if !target.isBeingPresented {
return true // perform the segue
}
return false // do not perform the segue
}
You need to get the target ViewController depending on how your app is set up. You can identify it with a identifier or by checking the array of presented or child view controllers on your current view controller for an already existing view controller of the target class type. I would require more information to give a more specific solution.
Here's what I'm trying to do:
I have 2 views, Settings and Main Screen.
I've coded a segue to occur when a button in Settings is pressed, taking the view to the Main Screen. The identifier is "reset".
I'm trying to have the Main Screen perform a series of actions if this segue is triggered, but I can't find the function or way to do this.
Any help on how to implement this? I'd like it to trigger when the segue occurs.
You can pass arguments to the main screen in the prepareForSegue function in the settings page. Then in your main screen you can put in checks in your viewWillAppear function to handle them as you see fit.
Example:
In Settings:
override func prepareForSegue(segue: UIStoryboardSegue!, sender: AnyObject!) {
if (segue.identifier == "reset") {
// pass data to next view
let viewController:ViewController = segue!.destinationViewController as MainViewController
viewController.settings = settings // where settings is what you want to pass
}
}
In Main Screen:
override func viewWillAppear(animated: Bool) {
super.viewWillAppear(animated)
if self.settings { // Do something }
}
If you're wanting something to happen when the segued view controller is triggered you can do it in a couple of places.
If you want the event to happen at the time the segue is fired, perform your code inside of prepareForSegue. There you can check the UIStoryboardSegue object's identifier field for "reset" and if true, call your logic.
If you want it to happen when the destination view controller loads or appears, do it inside of its viewDidLoad or viewDidAppear methods. In your case those methods would exist on the "Main Screen" view controller class.
Currently developing a small ios app in swift, I have populated a collection view cell with data from a .plist, each cell has a title and button, what i'm wanting is to segue to multiple view controllers in the storyboard depending on the segue identity once the button is pressed. For example if the segue has the id set as food, i want it to navigate to the view called food?
or if it is easier for the segue to pull the title from the cell then navigate to the view with the same title as the cell?
ill try and explain in code:
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?)
{
if segue.identifier == "food"
{
//then navigate to the view controller called food
}
else if segue.identifier == "drink"{
{
//navigate to the view called drink
}
}
If food, drink or any other items have their own unique view in storyboard this is achievable.
You can assign a string to each button in UICollectionView and check which item in the collection was tapped and do a performSegueWithIdentifier: with button string.
performSegueWithIdentifier("toComments", sender: self)
Then prepareForSegue: method to pass data. Do another if for drink.
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "toFood" {
let ExchangeViewData = segue.destinationViewController as! FoodViewController
ExchangeViewData.foodMenuToShow = foodMenuID //This can be anything that you get your food items.
}
}
You can't do what you are asking for. A segue identifier is what determines which view controller gets invoked by the segue. If you want to go to a different view controller, invoke a segue with a different identifier. By the time you get to prepareForSegue it's too late. The segue to a specific view controller is already in progress.
You should explain what it is you're trying to do and we can help you solve your problem rather than trying to fight the APIs and do something that's neither possible nor appropriate. (Your question is an example of an "XY problem".)
I have a registration view controller. In it I process the input provided by the user soon as he clicks on the register button. This button in the view is connected to an IBAction where I validate the input provided.
How is t possible to trigger the segue and pass the data within the IBAction (not override prepareforsegue) to the next view controller? If the validation fails it shouldn't attempt to execute segue but rather stay on same view to display validation errors.
Thanks for support. The question has been asked in different ways before surely but I wasn't able to find a good combine of segue in ibaction and passing data too.
You can create a segue from one view controller to another and can put a check in your button's IBAction, like this:
if (validationPasses)
{
self.performSegueWithIdentifier("segueIdentifier", sender: self)
}
And if you don't won't to override your prepareforsegue, you can pass the data using delegates.
However, by overriding prepareforsegue, you can pass data to your next view controller like this:
override func prepareForSegue(segue: UIStoryboardSegue!, sender: AnyObject!) {
if (segue.identifier == "segueIdentifier") {
// pass data to next view controller
let vc : NextViewController = segue!.destinationViewController as NextViewController
vc.someVariable = someValue
}
}
Here NextViewController is your next viewcontroller, change its name to your next view controller and in this view controller define variable to whom you want to pass value, in this case it is someVariable