How to get rid of or change a storyboard created segue programatically - swift

I have a segue from one view controller to another, which was created in the storyboard of my project. I'm wondering if there is a way to change or get rid of that segue through code. Thanks, using Xcode 9 Swift 4.

Apparently it's not possible... look at the answer on this post. And the documentation says this about creating segues:
"You do not create segue objects directly. Instead, the storyboard runtime creates them when it must perform a segue between two view controllers."
Although it's not about changing or deleting the segue, I think it's the same logic.
If you just want to avoid the segue to happen you can do that inside "shouldPerformSegue" (like vacawama said in the comments):
override func shouldPerformSegue(withIdentifier identifier: String, sender: Any?) -> Bool {
if identifier == "aSegueIWantToDisable" {
return false
}
return true
}

Related

How to override Copy and Paste NSMenuItems for one View Controller Swift macOS

I am writing a macOS application with multiple view controllers, using Storyboards.
In my main View Controller I would like to be able to copy and paste data to the NSPasteboard. The data is related to buttons displayed to the user, and the exact data to be copied varies depending on which button has most recently been pressed/selected.
I would like to be able to override the standard behaviour of the Copy and Paste NSMenuItems when my main View Controller is the front most (key) window, but revert to back to standard behaviour when other windows are in the foreground, as they all contain NSTextFields which can be copied/pasted into.
I have done a lot of googling, and overriding this behaviour is not very well documented. I can achieve it globally by adding an IBAction into the App Delegate, which I could use to call a function in whichever View Controller is key, but this doesn't feel like a very elegant solution.
Currently my IBAction in the App Delegate looks like this:
#IBAction func copy(_ sender: Any) {
if let window = NSApplication.shared.keyWindow {
if let splitView = window.contentViewController as? SplitViewController {
if let controlVC = splitView.controlItem.viewController as? ControlViewController {
controlVC.copyAction(self)
}
}
}
}
Am I missing a neater solution?
Thanks,
Dan

Swift - TabBarController - Decide segue at runtime

i have the following situation:
In my TabBarController i have multiple tabs and it all works fine, but:
The destination of one specific tab is dynamic.
If a certain condition matches, this specific tab should open a NavigationViewController.
If an other condition matches, this specific tab should open a ViewController.
Are there any built in solutions?
How can i modify the destination of a tab at runtime?
Greetings and thanks
Long long ago, in a galaxy far far away...
Segues didn't even exist. So yeah
The UITabBarController has a setter in which you can pass the new view controllers you want it to handle. You won't be able to decide what shows at the point of the tapping this way, but you'll be able to change the controllers as the condition changes.
open func setViewControllers(_ viewControllers: [UIViewController]?, animated: Bool)
If you however DO need to decide as the tab is tapped... You could have your tab controller direct to a controller that's essentially empty, and use view controller containment for that controller to have the 2 options on it, and have one of them hidden. Pretty doable from the storyboard with very little supporting code. I think I prefer this option.
If you would rather continue to use Segues, then you can perform a specific Segue depending on the state of some condition variable like this:
func presentAppropriateView() {
if condition {
performSegue(withIdentifier: "ToNavBar", sender: self)
} else {
performSegue(withIdentifier: "ToVC", sender: self)
}
}
write a delegate method which you want to perform on certain condition on runtime and call
[self.tabBarController setSelectedIndex:0];
where index could be of your choice

How do I remove a view controller from the memory?

I am using segues to present a new view controller. I have a 1 button that goes to VC 2. I am using this code:
func jumpToVC() {
dispatch_async(dispatch_get_main_queue()) {
[unowned self] in
self.performSegueWithIdentifier("whatVC", sender: self)
}
}
However the VC will not remove itself from the memory. How can I achieve this? I am presenting my views modally.
I think that you got a little bit lost here. What you want is a segue that will cause you to change UIViewController object to another one. You don't need to delete the first UIViewController and it's against practice in Swift.
From what I guess, you're just starting your journey with Swift and I know how confusing it might be. Therefore, I recommend you try this tutorial: https://itunes.apple.com/us/course/developing-ios-9-apps-swift/id1104579961
I've recently learnt from that and it's the best thing I've found.
Let me know if you have any more questions and whether I can help you somehow.

Xcode 7.2.1 Making a Reset Button using a Button with Swift

I am trying to make a reset button for my app that will reset the UI to the original state. I have made a UIButton and linked it to the ViewController, but I have no idea where to go from here. I tried using the following code:
#IBAction func resetToOriginalState(sender: UIButton) {
self.resetToOriginalState (sender: UIButton)
}
It gave me the following error:
Editor placeholder in source file
Sorry if there may be an obvious answer, but I am very new to Swift and Xcode.
Is there any other way to create a reset button?
The error:
Editor placeholder in source file
Is because you are calling a function with the UIButton Class name instead of the actual button.
#IBAction func resetToOriginalState(sender: UIButton {
// this line is wrong, you shouldn't have UIButton in here
self.resetToOriginalState (sender: UIButton)
// the line should read
self.resetToOriginalState (sender: sender)
}
This way, you are passing the actual button into the function that was passed to resetToOriginalState
Seems you have too IBAction for the same button, check how many time you have #IBAction func resetToOriginalState(sender: UIButton) in your code and remove the references from the references Interface list to clean it, should there be only one :
It depends what is in the scene and what do you need to reload. As far is I know you can't really segue a ViewController to itself, but here are few options:
Try to add loadView() when the button is pressed
Duplicate the view controller, and segue between the two. (might be risky and create more work)
Reset your variables to their initial state when the button is pressed
You should give us more detail because this is implementation specific.
Nevertheless, it's not very clean, but depending on the architecture of your code, you might be able to generate a new instance of your view controller, destroy the current one, and present the new one.

Come back to the tabBarController, swift

Currently on my viewController : Upload, my button send the data to my database only if all the information are filled out, and I come back to the preview view (table View) with :
self.navigationController?.popViewControllerAnimated(true)
I would like, if it is possible, to come back to my main view on the tabBarController. I tried many things, like directly on the storyboard with Present modally segue to "TabBar controller", but I come back to the TabBar without sending my data to the database and without checking in the information are filled out..
How can I do it?
Thanks!
UITabBarController has a property selectedIndex with which you can switch the selected tab. So on completion after dismissing the UploadViewController you can run:
self.tabBarController?.selectedIndex = 0 // Index to select
It would probably be best to create a delegate for your UploadViewController to fire a function to do all the work in your previewVC on API call completion.
(Super late response...in case someone has similar questions, presumably in later version of Swift, such as mine which is Swift 5, iOS 13.2). Steps:
Be sure to set an id for your UITabBarController storyboard, e.g. "TabBarViewController"
Next, add the following to an action that has already been connected to a button:
let ID_TABBAR = "TabBarViewCOntroller"
#IBAction func returnToTabbar(_ sender: Any) {
let tabBarController = self.storyboard?.instantiateViewController(identifier:ID_TABBAR) as! UITabBarController
self.navigationController?.pushViewController(tabBarController, animated:true)
}
Referenced from one of the responses from this post.
Update: In case your Tab Bar View Controller also happens to be the root view controller, the two lines of the code in the returnToTabbar method above can be:
self.dismiss(animated:true, completion:nil);
self.navigationController?.popViewController(animated:true);
(ref.: See answer here, for Swift4 but works just fine in Swift5)