Swift: Trouble getting rid of "dim" - ios

I have been following this tutorial: http://www.totem.training/swift-ios-tips-tricks-tutorials-blog/ux-chops-dim-the-lights
However I have edited it slightly so that I can specify different Segues and also do it programmatically.
The problem occurs when I close the popped up view. When I close it, the background dim stays there:
What I did to the project files:
files: (https://github.com/TotemTraining/DimBackground.git)
1) Deleted the Segue that was there
2) Created an IBAction for the button named clickedButton
3) Created new Segue from first VC to second Named the Segue testSegue
4) Added this code for the IBAction:
#IBAction func clickedButton(sender: AnyObject) {
performSegueWithIdentifier("testSegue", sender: self)
}
5) Changed the prepareForSegue to:
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if (segue.identifier == "testSegue") {
dim(.In, alpha: dimLevel, speed: dimSpeed)
}
}
Now, When I run it, it shows the popup the desired way however when I click the close it removed it but leaves the "Dim" there. Can anyone see why?
The view is going to have several segues from it and I only want a few of them to have this "dim" effect.
Edit:
If I take out the if (segue.identifier == "testSegue") so its now:
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
//if (segue.identifier == "testSegue") {
dim(.In, alpha: dimLevel, speed: dimSpeed)
//}
}
it works as desired, so is it something to do with that?

It sounds like your unwind isn't getting called in order to dim out. Have you set a breakpoint to make sure? Also, if you are using Xcode 8, there was a weird bug with unwind segue names. They automatically got appended with "WithSegue:" at the end of them, so double check that it is labeled correctly in Interface Builder.

Related

Can't perform Auto Segue

I'm new to iOS dev, I'm trying to switch between storyboards.
I followed the answer provided here 'Receiver (<ViewController>) has no segue with identifier 'addSegue'.
I did every step and put the below code in my viewDidLoad, because I want it to automatically go to a new storyboard, depending no a condition. Am I doing something wrong?
self.performSegue(withIdentifier: "identifierName", sender: self)
your code is write in prepareForSegue method
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if(segue.identifier == "yourIdentifierInStoryboard")
{
self.performSegue(withIdentifier: "identifierName", sender: self)
}
}
Like Vacawama said, it's not possible to do it in the ViewDidLoad, so I've put it in the ViewDidAppear and it works just fine.

Swift - Navigation Bar Not Appearing From Segue From Custom Cell

Here is a picture of my storyboard:
I assume the problem is because the segue is coming from a custom table view cell. The button will send information based on the cell selected. When I press the button, I just perform the segue with identifier. Unfortunately, the navigation bar does not appear, and I can't imagine why its not appearing.
Any help is greatly appreciated!
EDIT: Here is the storyboard segue. It is push:
EDIT2: Here is the code I am using to perform this segue:
func buttonAction(sender: UIButton!) {
segueIndex = sender.tag
performSegue(withIdentifier: "segue_show_job", sender: self)
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "segue_show_job" {
let nextVC = segue.destination as! PlaceBidTableViewController
nextVC.job_info.job_info = self.job_arr_arr.job_info_arr_arr[segueIndex]
}
}
ANSWER:
Maximo and XCoder, thank you for your help. Even though my segue was push, something must have been corrupted. I deleted the segue and re-created it and it worked! Thanks!
Make sure use Push in your view. This is what suppose to be.
This make your navigation bar not show up.
Maximo and XCoder, thank you for your help. Even though my segue was push, something must have been corrupted. I deleted the segue and re-created it and it worked! Thanks!

What is the correct way to use getters/setters

Salutations,
This is a follow up from my previous post (Incorrect data passed on first click of button). It was answered, but the updated edit of course occurred.
My inquiry is as follows, at the moment I have two IBActions funcs which empty, lack any sort of code, and their only purpose for existence is that they are connected to my other view controller, and as such if I remove them I have no way to differentiate between which segue should be used (My understanding is that although we can create segues between two view controllers, I do not think anything more than one would make sense (as in, how to decide which one to go with). I digress.
I tried using the following in my code before:
self.performSegue(withIdentifier: "slideTwo", sender: self);
This works well, however, it does cause double segueing. As such, I settled for the following:
override func prepare(for segue: UIStoryboardSegue, sender: Any?)
{
let passedPic = segue.destination as! ViewControllerTwo;
if(segue.identifier == "slideOne") {
passedPic.picsChosen = sLS1;
} else {
passedPic.picsChosen = sLS2;
}
}
This works well, does what I need, but I do then have the problem of two empty IBActions:
#IBAction func slideShowOne() {
}
#IBAction func slideShowTwo() {
//self.performSegue(withIdentifier: "slideTwo", sender: self);
}
This is where I thought perhaps I could try to create a getter/setter (which swift makes far more complicated than it honestly should be). This is where I am lost.
The code looks as follows:
var picsChosen : [String] {
set(newData) {
for index in 0..<newData.count {
print("Index is: ", newData[index]);
}
self._picsChosen = newData;
} get {
return self._picsChosen;
}
}
Now, my issue is two fold. First, what is the correct way to actually access and pass values into my variable. I tried doing: myClass: ViewControllerTwo! = ViewControllerTwo();
and then use myClass.picsChosen = myArray; However, although it does appear that the data was passed successfully into my VC2, since when I loop through (inside the setter) I do see the values successfully being displayed, when I try to access picsChosen outside of that I get an index out of bounds error, in other words, the value was never associated with picsChosen. The current way I have it works well, but I want to know what is the correct way to use getters/setters, and why what I have did not work.
Cheers!
This works well, does what I need, but I do then have the problem of
two empty IBActions:
You don't need these empty #IBActions. They aren't doing anything for you. The segues are wired from the buttons in the Storyboard, so you don't need the #IBActions. If you delete the code, you also need to remove the connection from the Storyboard or your app will crash when it tries to call the removed #IBActions. To remove, control-click on the buttons in the Storyboard, and then click on the little x next to Touch Up Inside to remove the connection. Then you can delete #IBAction in your code.
if I remove them I have no way to differentiate between which segue
should be used
Not true. You have two different segues wired from two different buttons. The segue's identifiers are how you differentiate between the two segues.
I tried doing: myClass: ViewControllerTwo! = ViewControllerTwo();
When using segues, the segue creates the destination viewController for you.
This doesn't work because you are creating an entirely new ViewControllerTwo here. You are passing the values to this new instance, but this isn't the ViewControllerTwo that the Storyboard segue created for you.
When using segues, you should pass the data in prepare(for:sender:), just like you showed above. Get the destination ViewController and fill in the data you want to pass.
And now for the question you didn't ask:
How could I have done this using the #IBActions to trigger the
segue?
Remove the segues that are wired from your buttons.
Wire the segue from viewController1 to viewController2. Click on the segue arrow in the Storyboard and assign the identifier slideShow.
Assign unique tags to your two buttons. You can set this in Interface Builder. Give the first button tag 1, and the second button tag 2.
Your buttons can share the same #IBAction:
#IBAction func goToSlideShow(button: UIButton) {
self.performSegue(withIdentifier: "slideShow", sender: button)
}
In prepare(for:sender:):
override func prepare(for segue: UIStoryboardSegue, sender: Any?)
{
let passedPic = segue.destination as! ViewControllerTwo
if let button = sender as? UIButton {
if button.tag == 1 {
passedPic.picsChosen = sLS1
} else if button.tag == 2 {
passedPic.picsChosen = sLS2
}
}
}

iOS Segue not working as expected when passing value

I have a button with the following code:
#IBAction func buttonPress(sender: AnyObject) {
performSegueWithIdentifier("newAccount", sender: sender)
}
When the button is pressed it performs a segue. I want to pass a value to the new view controller so I added the following code:
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "newAccount" {
if let dvc = segue.destinationViewController as? NewAccountViewController {
dvc.testValue = "This is my test value I want to pass"
}
}
}
The NewAccountViewController doesn't receive the value (I don't get any error).
The code I have in my NewAccountViewController is:
var testValue:String?
When I print(testValue) I don't get anything.
Why isn't this working as expected?
You need to check if the class is set correctly
Like the ViewController in below image
There are maybe two reasons: 1. identifier is not set as "newAccount" in the storyboard; 2. you have put a navigator view controller into the NewAccountViewController, which would ask you to transfer the destination as UINavigationController instead of NewAccountViewController. Hope this helps.

How to specify which view controller you are preparing for with Swift's prepareForSegue function?

So I have multiple buttons on one view segue'ing to other views. I have the function prepareForSegue() at the bottom of my code preparing one segue, however the app crashes when I use another segue on the view, even if it doesn't need any preparing.I think that the problem is that all of the segues use the prepareForSegue() function, however the storyboard ID is different than the view it is transitioning to.Is there any way to specify a prepareForSegue() function for each separate segue on the same view?Code:
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
let theVC: ViewController = segue.destinationViewController as! ViewController
theVC.receivedString = "true"
}
Thanks
In your prepareForSegue you should use a condition to check segue identifier like:
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if(segue.identifier == "yourSegueToAnyController")
{
//do code for specific viewController
}
}

Resources