Swift: Show UIView when button with segue is clicked - swift

I try to show a UIView when a button is clicked, but it seems that the UI can't get updated, because the button also activates a segue. When I delete the segue, the UI is updated and my UIView is drawn.
Can anyone help me on how to solve this problem?
override func shouldPerformSegueWithIdentifier(identifier: String, sender: AnyObject!) -> Bool {
if identifier == "mySegue" {
if(myLocation == nil) {
/* Rest of code */
return false
}
else {
// These properties need to be set
box.hidden = false
actInd.startAnimating()
return true
}
}
return true
}
#IBAction func startComputing(sender: UIButton) {
//When I put the properties, nothing happens,
//but something like print("Button pressed") does happen.
}
Thanks in advance!

From what I understand, the view is within the view controller window hierarchy, so I'd do this without using segues:
#IBAction func startComputing(sender: UIButton) {
UIView.transitionWithView(self.yourView, duration: 0.5, options:UIViewAnimationOptions.TransitionFlipFromLeft, animations: {
}, completion: nil)
}
This will present your view when the button is clicked. Inside the block you can do the rest like updating UI etc.

Related

how do I refresh a childViewControler object when an action occurs on the parent viewControler

I have embedded a child ViewControler into my mainViewControler and it displays a graph.
the main view controller includes an input field and a save button.
I wish to have the graph in the childViewControler reload its data when the save button is taped.
the childViewControler includes a function
func refresh() {
fetchTransactionsAndCalculateTotals()
generateChartData()
setChart()}
that I would like to call. I have setup a segue from the main controller to the child controller,
enum Segues {
static let toInOutBar = "toInOutBars"
}
override func prepare(for segue: UIStoryboardSegue, sender: UIButton?) {
if segue.identifier == Segues.toInOutBar {
let destVC = segue.destination as! InOutBarViewController
destVC.refresh()
}
}
I am struggling to get further than this, I have tried to call this function from myUIButton action without success.
Thanks for any support
You should link an IBAction on the touchUpInside action of the button and get your child VC using the children property.
#IBAction func refreshClicked(_ sender: UIButton)
{
guard let vcChild = self.children.first as? InOutBarViewController else { return; }
vcChild.refresh()
}

Swift, core data, add item from modally presented view

I am updating an app into Swift and have a tableView to which items can be added from a modally presented view. This works fine when the Save button is pressed, but when cancel is pressed the table view is presented with a blank line at the top, showing that an empty item was added. What am I missing?
The AddViewController protocol is:
protocol NewGaugeDelegate {
func didFinish(viewController:AddGaugeViewController, didSave:Bool)
}
and the didSave function is:
extension AddGaugeViewController {
#IBAction func cancelButtonWasTapped(_ sender: AnyObject) {
delegate?.didFinish(viewController: self, didSave: false)
}
#IBAction func saveButtonWasTapped(_ sender: AnyObject) {
addGauge()
delegate?.didFinish(viewController: self, didSave: true)
}
}
in the tableView controller, the protocol is accessed like this:
extension GaugeTableViewController: NewGaugeDelegate {
func didFinish(viewController: AddGaugeViewController, didSave: Bool) {
guard didSave,
let context = viewController.context,
context.hasChanges else {
dismiss(animated: true)
return
}
context.perform {
do {
try context.save()
} catch let error as NSError {
fatalError("Error: \(error.localizedDescription)")
}
self.coreDataStack.saveContext()
}
dismiss(animated: true)
}
}
Suggestions as to why the cancel function is not working properly would be appreciated.

How to click a button programmatically?

I have 2 view controllers which should be swapped according to userinput. So, I want to switch the views programatically based on the input I get from a text file.
Algorithm :
if(input == 1)
{
Go to View Controller 1
}
else if(input ==2)
{
Go to View Controller 2
}
Any help on how to click the button programmatically or load that particular viewcontroller with input?
To fire an event programmatically you need to call sendActionsForControlEvent
button.sendActionsForControlEvents(.TouchUpInside)
--
Swift 3
button.sendActions(for: .touchUpInside)
Or you can just put all the logic that you perform when a button gets clicked in a separate method, and call that method from your button's selector method.
#IBAction func someButtonPressed(button: UIButton) {
pushViewControllerOne()
}
#IBAction func someButtonPressed(button: UIButton) {
pushViewControllerTwo()
}
func pushViewControllerOne() {
let viewController = ViewControllerOne(nibName: "ViewControllerOne", bundle: nil)
pushViewController(viewController)
}
func pushViewControllerTwo() {
let viewController = ViewControllerOne(nibName: "ViewControllerTwo", bundle: nil)
pushViewController(viewController)
}
func pushViewController(viewController: UIViewController) {
navigationController?.pushViewController(viewController, animated: true)
}
Then instead of invoking programatically invoking a button press, just call the method pushViewControllerOne() or pushViewControllerTwo()

How to dismiss view in swift? dismissViewControllerAnimated is not doing anything?

I have an issue where dismissViewController method is not actually doing anything. My code is as follows:
import UIKit
class CameraController: UIViewController, UIImagePickerControllerDelegate, UINavigationControllerDelegate {
....
#IBAction func dismissPhotoPicker(sender: UIButton!) {
print("in the function")
self.dismissViewControllerAnimated(true, completion: nil)
}
}
No errors are thrown when running, click on the button does trigger the method (I put a print statement in the function)
There are 2 buttons "Back", which calls that method, and another one called "Open Image Picker", which opens the Image Picker.
My storyboard starts with a TabBarViewController, I set up a segue such that when clicking on the "Camera" button, it switches to the "CameraViewController", which is associated with the custom class CameraController (which I've specified in my Storyboard.xib file)
Can you do something like this?
yourView:UIView {
// do something
}
yourMainController:UIViewController {
let view = yourView()
func showView() {
self.view.addSubview(view)
}
func hideView() {
view.removeFromSuperView()
}
}
Try this code:
if self.isBeingPresented() {
// being presented
print("being present, going to dismiss..")
self.dismissViewControllerAnimated(true, completion: nil)
} else if self.isMovingToParentViewController() {
// being pushed
print("being pushed, going to pop..")
if let navController = self.navigationController {
navController.popViewControllerAnimated(true)
}
} else {
// simply showing again because another VC was dismissed
}

Swift: Function Called when Back is pressed

I would like to perform some actions when a user presses the "Back" button on my Navigation Controller. Is there a Swift function that is called when this happens?
Try this (copied and pasted from manecosta)
Replacing the button to a custom one as suggested on another answer is possibly not a great idea as you will lose the default behavior and style.
One other option you have is to implement the viewWillDisappear method on the View Controller and check for a property named isMovingFromParentViewController. If that property is true, it means the View Controller is disappearing because it's being removed (popped).
Should look something like:
override func viewWillDisappear(animated : Bool) {
super.viewWillDisappear(animated)
if (self.isMovingFromParentViewController()){
// Your code...
}
}
Here is the link to the other question
override func viewWillDisappear(animated: Bool) {
// Do Your Lines of Code ...
}
Everytime when back button or Done is pressed or a view is popped out this function is called.. you need to override this..
Try to call this
override func viewDidDisappear(_ animated: Bool) {
super.viewDidDisappear(animated)
if self.isMovingFromParent {
print("isMovingFromParent()") // dismiss for push viewController
}
if self.isBeingDismissed {
print("isBeingDismissed()") // dismiss for modal that doesn't has navigationController
}
if self.navigationController?.isBeingDismissed ?? false {
print("navigationController?.isBeingDismissed()") // dismiss for modal that has navigationController
}
}
Try this:
override func willMoveToParentViewController(parent: UIViewController?) {
if parent == nil {
// Back button Event handler
}
}
Swift 5
"viewWillDisappear" call Every time for forward or backward.
But if you add this check it's call only when back from controller.
override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
if self.isMovingFromParent {
print("Only back action")
}
}