I have two viewControllers in the storyboard, a ViewController (root) and a TableViewController, that are linked by a push segue.
The TableViewController acts as a setting page of the app and multiple UIControls such as UISegmentedControl and UISwitch are put in it. These UIControls are linked to the TableViewController via IBOutlets.
#IBOutlet weak var mySegmentedControl: UISegmentedControl!
#IBOutlet weak var mySwitch: UISwitch!
However, when I call these UIControls in the ViewController by:
let tableView: TableViewController = TableViewController.init(nibName: nil, bundle: nil)
if tableView.mySwitch.isOn {
//perform actions
}
Error pops up at the if-statment:
fatal error: unexpectedly found nil while unwrapping an Optional value
I thought it was because the TableViewController has not been loaded when the UIControls are being called, thus I have tried calling the viewDidLoad() of tableView first, but the error still remains.
What should I do? Appreciate any suggestions.
You do not directly call viewDidLoad, this is done by UIKit after the view is being loaded. And the view is loaded when the view needs to be displayed.
If you need to access the outlets before, you can force loading by
tableView.loadViewIfNeeded()
But remember:
Maybe it's better to do the outlet stuff inside viewDidLoad of the involved controller, and not from outside.
You should not name the controller variable tableView. That name suggest to be a view and not a controller. Better name it tableViewCtrl or so.
Related
UPDATE It turns out that my renaming of my project causes these issues to happen :. I followed all these steps (How do I completely rename an Xcode project (i.e. inclusive of folders)?). I removed all my Outlets and I'm still getting the error so I still don't know what exactly is causing this to happen. For now I'm just going to revert to my previously named project and go from there. If you have any other rec's I'd appreciate them!
I'm simply trying to run my app but I get an "NSInternalInconsistencyException" exception saying that my view controller didn't get a UITableView.
I checked that my IBOutlet from my UITableView is set correctly but I'm still receiving the error..
This is how I'm declaring my IBOutlet...
class EntryViewController: SwipeTableViewController, UITextFieldDelegate {
//MARK - IBOUTLETS
#IBOutlet weak var entryTableView: UITableView!
#IBOutlet weak var entryTextField: UITextField!
#IBOutlet weak var saveButton: UIButton!
#IBOutlet weak var heightConstraint: NSLayoutConstraint!
#IBOutlet weak var searchBar: UISearchBar!
This is the error I'm getting:
Screenshot
* Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: **'-[UITableViewController
loadView] instantiated view controller with identifier "showEntries"
from storyboard "Main", but didn't get a UITableView.'
libc++abi.dylib: terminating with uncaught exception of type
NSException
I imagine this would link my UITableView to my ViewController.
Also, I'm subclassing a view named SwipeTableViewController from my EntryViewController...
class SwipeTableViewController: UITableViewController, SwipeTableViewCellDelegate {
override func viewDidLoad() {
super.viewDidLoad()
tableView.rowHeight = 80
}
Wouldn't my EntryViewController be automatically subclassed to the UIViewController from there?
Looks like you have UITableView outlet declared in UITableViewController. Make sure your class is a subclass of UIVIewController instead of UITableViewController.
In your storyboard, look for the view controller with identifier "showEntries" , delete it, and drag the UIViewController element instead of UITableViewController element.
Then drop the UITableView into the UIViewController and connect it to the tableview outlet property in your view controller.
Do below steps.
1.Delete all outlets from storyboard.
2.Create a class of type UIViewController.
3.Drag and drop a new tableview to the storyboard.
4.Connect all the outlets with respective UI Elements.
Or you need to make your class as UITableViewController type.
Now It will not crash.
I am currently trying to build a demo app in Xcode 8.2.1 using swift to learn how to use UIPageViewController. I added three outlets to my subclass of UIPageViewController which I intended to connect to the three views that the page view controller would control.
class MyPageViewController: UIPageViewController, UIPageViewControllerDataSource
{
#IBOutlet weak var redController : MyColorViewController!
#IBOutlet weak var greenController : MyColorViewController!
#IBOutlet weak var blueController : MyColorViewController!
//...
}
In my main storyboard, I created one instance of MyPageViewController and three instances of MyColorViewController.
But, for the life of me, I cannot figure out how to get the storyboard to connect the three outlets above to any of the color view controller instances.
I have tried both ctrl-dragging from the page view controller itself to each color view controller and from the page view controller's outlets in the inspector. Nothing works.
Suggestions?
You should do this by code. And not with IBOutlets.
First: set a storyboard ID to each of the UIViewControllers.
Second:
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let viewController = storyboard.instantiateViewController(withIdentifier :"blueViewController") as! UIViewController
And connect it to a property.
You do this for all three UIViewControllers.
I have a problem in my apps that its memory increase constantly. So here is my app structure. I am using SWrevealViewController for slidemenu.
Slidemenu
- View Controller A
- View Controller B
- View Controller C
My View Controller A consist of UICollectionView and ScrollView.
When I perform segue from View Controller A to View Controller B, the memory is doing fine. But if I am going to View Controller A with slide menu, the memory increases.
Here is my didSelectRowAtIndexPath on SWRevealViewController's table delegate. I am using performSegueWithIdentifier
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
if isLoggedIn {
self.performSegueWithIdentifier(idSegueLoggedIn[indexPath.row], sender: self)
}
}
Then I am going to View Controller B via slide menu and then going to View Controller A again, on that point memory increases constantly.
It looks like my View Controller A that consist of UICollectionView doesn't release its object on memory. But if I remove my UICollectionView, my memory issue was dissapear and it runs well.
So I think it was about my reference to UICollectionView. I already declared that with weak property but it does nothing.
#IBOutlet weak var othersCollectionView: UICollectionView!
I try to called deinit() in my View Controlle A but it doesn't called.
Here is my memory usage graph, on each "black arrow", I move to View Controller A with slide menu.
Pls let me know if I am doing something wrong. Thank you so much.
I am answering a bit late but I had a similar problem. I the end my problem was linked to the declaration of delegates in some UITableViewCell. I fixed the problem by declaring the delegate variable as weak.
Here is an example :
class MyTableViewCell: UITableViewCell {
#IBOutlet weak var myNameTextField: UITextField!
weak var delegate: CellDbErrorDelegate?
I have a UIViewController with some IBOutlets. I also have a UIWindow that needs to access those IBOutlets in the first UIViewController. However, whenever I try to access it from my UIWindow, the variables are nil. Here's some of my code.
class ViewController: UIViewController {
#IBOutlet weak var playPauseLabel: UILabel!
#IBOutlet weak var playButton: UIButton!
#IBOutlet weak var pauseButton: UIButton!
func functionA() {
println(playPauseLabel)
println(playButton)
println(pauseButton)
}
}
class WindowClass: UIWindow {
func resetPlayPause() {
var vc = ViewController()
vc.functionA()
}
}
Now when I call func resetPlayPause() from WindowClass, all three IBOutlets are nil. I've read around on the other SO threads on this issue but haven't found any solutions. How can I access and modify those IBOutlets in ViewController from WindowClass?
The simple answer is that resetPlayPause() is constructing a new instance of ViewController each time it's called, and unless your ViewController has an initializer that loads itself from a storyboard or xib, none of your outlets will be connected. If your ViewController does have an initializer that does this, it won't connect its outlets until its view has been loaded. You can do this simply by calling vc.view in resetPlayPause() before you toggle the buttons.
The harder answer is that having your window access a view controller's ivars directly seems like a really bad idea. Either move your resetPlayPause() method somewhere other than in a window subclass, or have it call a method on ViewController that will toggle the buttons internally.
The answer is to use NSNotificationCenter. Works like a charm.
I have a UIViewController with an outlet for a tableView
#IBOutlet weak var tableView: UITableView!
When I try to access the tableView variable in viewDidload it's throwing an error saying tableView is nil.
Everything seems to be set up in the interface builder. The class for File's Owner is set to my custom class and the tableView outlet is set as well. What could be going wrong here, why is tableView variable still nil even inside viewDidLoad?
Did you type out that outlet code without creating a link?
If you did that would be your problem.
Go back to your Storyboard, switch to assistant editor mode and CTRL + Drag from the UITableView to #IBOutlet weak var tableView: UITableView!
Ensure you're got a FILLED IN circle to the left of your var similar to this.