Swift - UIView.transitionFromView - - swift

What happens to your “fromView” after you use UIView.transitionFromView? How do you get it back? (re-initialize it?).
Here’s a simple example I made and get “fatal error: unexpectedly found nil while unwrapping an Optional value” when I try to get back to the “fromView” or reference a label on it, etc.
Let’s say I’ve set these views in my storyboard - I added a subview to the root called “cardView” and then 2 views under cardView called “backView” and “frontView"
Linked these up to the view controller and linked the Flip button to an action that looks like this (along with the variables):
#IBOutlet weak var backView: UIView!
#IBOutlet weak var frontView: UIView!
#IBOutlet var cardView: UIView!
var front = true
#IBAction func flipIt(sender: AnyObject) {
if front {
UIView.transitionFromView(self.frontView, toView: self.backView, duration: 1, options: UIViewAnimationOptions.TransitionFlipFromRight, completion: nil)
} else {
UIView.transitionFromView(self.backView, toView: self.frontView, duration: 1, options: UIViewAnimationOptions.TransitionFlipFromLeft, completion: nil)
}
front = !front
}
So when I run this - the first tap of the “Flip” button does fine, it flips over to the backView, but then when I tap it again I get “fatal error: unexpectedly found nil while unwrapping an Optional value” on the 2nd transitionFromView line (the one under “else”).
Obviously there’s some effect of transitionFromView that I don’t understand. I've tried a lot of different things, including a tutorial that uses this transition and was able to get it working but it doesn’t use the storyboard so I’m not sure how to apply how that tut does it to my storyboard views, apparently...
thanks for any help.

You've declared your view properties as weak, so they're getting released as soon as it appears they're no longer needed. Just change them to strong (by removing "weak") and you should be golden:
#IBOutlet var backView: UIView!
#IBOutlet var frontView: UIView!

Related

Unrecognized selector sent to instance for unhiding button

import UIKit
class ViewController: UIViewController {
#IBOutlet var continueLabel: UILabel!
#IBOutlet var yesButton: UIButton!
#IBOutlet var noButton: UIButton!
override func viewDidLoad() {
super.viewDidLoad()
continueLabel.isHidden=true
yesButton.isHidden=true
noButton.isHidden=true // Do any additional setup after loading the view.
}
#IBAction func continueButton(_sender: UIButton) {
continueLabel.isHidden=false
yesButton.isHidden=false
noButton.isHidden=false
}
}
I'm a bit new to IOS development and was practicing how to hide/unhide a button. On the view controller, I can get the button hidden but when I press the button to unhide - The following error is being thrown reason: '-[WelcomeApp.ViewController ContinueButton:]: unrecognized selector sent to instance 0x12b2061d0.
It was basically a button which displayed two options - Yes or No. The Yes button is linked to the next view controller modally.
Function names, as variables, are case sensitive. You seem to have linked to ContinueButton() function, and then to have renamed it to continueButton(). Remove the link and recreate it, and then it should work.
Check the connection inspector as shown in the pic:
You can see a yellow warning, you have to remove this connection first and then again make the connection.

Fatal error: Unexpectedly found nil while changing Image and Label

I am trying to change a picture and a label programmatically.
Here's the current code setup:
#IBOutlet weak var image: UIImageView!
#IBOutlet weak var label: UILabel!
override func viewDidLoad(){
super.viewDidLoad()
setlabel()
setImage()
}
func setImage(){
self.image.image = UIImage(named: name)
}
func setlabel(){
self.label.text = string
}
But unfortunately, I'm receiving nil for both UI elements, the label and the image. I've doublechecked the connection between my storyboard and the variables- they are set correctly, imho. They are available, since I am calling them outside the viewDidLoad function.
Anyone any suggestion?
Regards
I found the solution. I had 2 views- let's call them A and B.
A contained a button- and the button had 2 functions: Change the view and set the labels/images. Those functions were defined in the Controller of view A.
Since I wanted to change the labels and images of view B, I got the exception.
I had to include another Controller and transfer the functions for setting the images and labels to Controller B. Voila, I could change the images and labels.
Summarized: I had the right connections and the right functions, but in the wrong place.
Thank you all!

IBOutlet is nil

I have created a standard outlet for a view that will hold different information based on the button selected on the previous screen.
#IBOutlet weak var labelView: UIView!
It shows it is connected in both the story board view and on the code itself, however, every time I get to any reference to the labelView such as:
if detail.description == "About"
{
labelView.backgroundColor = UIColor.red
}
Then the app crashes out with:
fatal error: unexpectedly found nil while unwrapping an Optional value
I have tried everything I can think of or read on the internet:
Removed and replaced the connection
Deleted the derived data folder like one post suggested
Created a reference to self.view to force it to load
Moved it to viewDidAppear
Moved it to viewWillAppear
Moved it to viewDidLoad (which is where it is currently being
called)
I am sure at this point that the answer is rather simple and I am just completely missing it.
To see where the outlet is being set to nil, try this:
#IBOutlet weak var labelView: UIView? {
didSet {
print("labelView: \(labelView)")
}
}
You should see it set to an initial value when the view is loaded. If it then gets set to nil, put a breakpoint on the print and your should be able to see from the backtrace where it's happening.
Views are lazy initialized. In case you are calling the affected line of code before viewDidLoad() in the views life cycle, try to access viewin advance:
if detail.description == "About" {
_ = self.view
labelView.backgroundColor = UIColor.red
}

EXC_BAD_INSTRUCTION error

I know that there are a lot of questions about this error, but I haven't found solution for my problem, even if I read most of them. In view controller I'm trying to make my text view displaying top of text, instead of bottom of it. When there is only one textView, then everything works perfectly, but when I try do it with two of the, , then I get EXC_BAD_INSTRUCTION error.
import UIKit
import Social
class ViewController: UIViewController {
#IBOutlet weak var textViewA: UITextView!
#IBOutlet weak var textViewB: UITextView!
override func viewDidLoad() {
super.viewDidLoad()
}
override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
textViewA.setContentOffset(CGPointZero, animated: false)
textViewB.setContentOffset(CGPointZero, animated: false)
}
If your error is like this
Then it is because you have not hooked up your outlets, to do this go into the storyboard / nib and hook it up like so:
Make sure they are valid by seeing the circles become solid:
This code works for me. I tested it on my own. This is mostly an outlet error, if they aren't connected correctly. It could also be that you have removed an outlet from your code and still have it connected inside your storyboard.

How to add a custom uiview to a view controller programmatically SWIFT

I am trying to add a custom view programmatically to my view controller. I used this snippet of code with no success of it appearing in front of my main view controller.
var DynamicView = CustomFilter()
DynamicView.setTranslatesAutoresizingMaskIntoConstraints(false)
self.view.addSubview(DynamicView)
CustomFilter Class:
import UIKit
class CustomFilter:
UIView {
#IBOutlet weak var party: UIButton!
#IBOutlet weak var outdoors: UIButton!
#IBOutlet weak var sports: UIButton!
#IBOutlet weak var diner: UIButton!
#IBOutlet weak var music: UIButton!
#IBOutlet weak var gaming: UIButton!
}
The custom filter is connected to a xib file.
Xib File:
Is there a possibility that the custom view maybe out of placed? I'm not sure what I'm doing wrong.
In order to use a view we designed in a xib, we most load from the xib.
if
let bundle = NSBundle(forClass: CustomFilter.self),
let nib = bundle.loadNibNamed("<#Xib File Name#>", owner: self, options: nil),
let dynamicView = nib.first as? CustomFilter {
self.view.addSubview(dynamicView)
}
An alternative approach would be to write your CustomFilter's init to load the view from the xib itself.
More clearly, the problem you're having is that none of your CustomFilter's initializers are going to care about the xib file you made unless you write them and tell them to care about it. Your current code is returning a 0x0 view with probably a white or clear background. If you modified your current code to set the CustomFilter's frame to something other than 0x0 size and set the background color to something like UIColor.greenColor(), you'd see it clear as day.
Also, you could use Xcode's visual debugger to find it.
It's probably zero height and zero width, and may be off-screen also.
You need to give it height and width constraints and x and y position constraints.
You should probably also use CustomFilter(frame: someFrameRect), since as I recall initWithFrame is the designated initializer for UIView.
As an aside, variable names should start with a lower-case letter, so DynamicView should be dynamicView