Unable to programmatically add a UITableView - ios

I recently converted my .xib file to Storyboards and decided to programmatically add a UITableView to my ViewController in the storyboard.
I used this code to accomplish this:
// ViewController.h
#import <UIKit/UIKit.h>
#interface ViewController : UIViewController <UITableViewDelegate, UITableViewDataSource> {
UITableView *table;
}
// ViewController.m
#import "ViewController.h"
#interface ViewController (PrivateStuff)
// unimportant code...
#end
#implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
table = [[UITableView alloc] initWithFrame:self.view.bounds];
table.delegate = self;
table.dataSource = self;
[self.view addSubview:table];
}
Unfortunately, whenever I try to run it, Xcode is giving me this message...
Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: '-[UITableViewController loadView] loaded the "ViewController" nib but didn't get a UITableView.'
... which doesn't make any sense. I have everything that needs to be hooked up on its correct order and all of my other views with UITableViews have no problem, in spite that the code is the same.
What could I do to solve this problem? Thanks in advance.

I agree with #rdelmar-Xcode is saying that a nib was loaded during a [tableviewcontroller loadView] call-so that means there is a tableviewcontroller somewhere in your code...but it also means that somehow the nib you created is still linked to your view controller. You may need to look at the connections inspector under utilities in Xcode and see if your nib still has any persistent links present and delete them. Otherwise, as long as you are providing a TableViewCell, either custom or generic, in the cellForRowAtIndexPath method, the above code should work

Related

This class is not key value coding-compliant for the key unitControl SegmentedControl

I am trying to make a Segmented Control that can change between Metric and US units, but the app crashes with NSUnknownKeyException every time the class is accessed. I am tried many solutions here on SO, but none works. I checked my outlet lists, uninstall and reinstall my app on the simulator, clean the project but nothing work out.
This is my .h file:
#import <UIKit/UIKit.h>
#interface SettingsController : UIViewController{
IBOutlet UISegmentedControl *unitControl;
}
- (IBAction)unit:(id)sender;
#end
and my .m file:
#import "SettingsController.h"
#import "ViewController.h"
#interface SettingsController ()
#end
#implementation SettingsController
-(IBAction)unit:(id)sender{
ViewController *viewController = [[ViewController alloc]initWithNibName:#"ViewController" bundle:nil];
if([sender selectedSegmentIndex] == 0){
viewController.metric = YES;
}
if([sender selectedSegmentIndex] == 1){
viewController.metric = NO;
}
[viewController setUnits];
}
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
I have tried commenting away all the code in unit:sender but the app still crashes with the same error. Also, when I remove the IBOutlet reference, the app crashes with another exception NSInvalidArgumentException unrecognized selector sent to instance 0x7ff04bf39510
These errors are common when there are missing connections, I just did a single view project, copy-pasted your code (you're missing #end at the end of implementation but I think you just forgot to paste it here) connected IBOutlets and IBActions (commented the inside of the unit code) and it worked flawlessy.
Some times Xcode keeps cache of Storyboard and Xibs in DerivedData.
Clear DerivedData
Restart your Xcode
Select Your project and click on Delete button to remove DerviedData.

Loaded nib but the view outlet was not set - Swift edition

I have a project that is all in Objective C, except for my view controller, which is in Swift.
When I run it, i get the error
Terminating app due to uncaught exception
'NSInternalInconsistencyException', reason: '-[UIViewController
_loadViewFromNibNamed:bundle:] loaded the "..." nib but the view outlet was not set.'
So I opened up my nib file, look at "File's Owner", and I see that the view does not even show up as an outlet at all.
For my old view controller (objective c), the view outlet does show up.
In my Swift view controller, I tried overriding the "view" variable from UIViewController in order to force it to be an #IBOutlet, but it complained about the "view" variable being of type UIView, complained about UIView?, and complained about UIView!.
Here are simplified versions of
my AppDelegate.h
#import <UIKit/UIKit.h>
#class MyViewController;
#class MyViewControllerSwift;
#interface MSAppDelegate : UIResponder <UIApplicationDelegate>
{
}
#property (strong, nonatomic) UIWindow *window;
#property (strong, nonatomic) UIViewController *viewController;
#end
AppDelegate.m
#import "MyAppDelegate.h"
#import "MyViewController.h"
#import "MySwift-Swift.h"
#import <UIKit/UIKit.h>
#implementation MyAppDelegate
static BOOL USE_SWIFT_VIEW_CONTROLLER = YES;
- (void)dealloc
{
[_window release];
[_viewController release];
[super dealloc];
}
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
// Override point for customization after application launch.
id viewControllerPtr = nil;
if(USE_SWIFT_VIEW_CONTROLLER)
{
viewControllerPtr = [MyViewControllerSwift alloc];
}
else
{
viewControllerPtr = [MyViewController alloc];
}
UIViewController* vController = nil;
if(USE_SWIFT_VIEW_CONTROLLER)
{
vController = [[viewControllerPtr initWithNibName:#"MyViewControllerSwift" bundle:nil] autorelease];
}
else
{
vController = [[viewControllerPtr initWithNibName:#"MyViewController" bundle:nil] autorelease];
}
self.viewController = vController;
self.window.rootViewController = self.viewController;
[self.window makeKeyAndVisible];
return YES;
}
ViewController.swift
import Foundation
import AVFoundation
#objc class MyViewControllerSwift : UIKit.UIViewController {
var player : AVFoundation.AVAudioPlayer?;
#IBOutlet weak var myTextView : UITextView!;
required init(coder aDecoder : NSCoder) {
super.init(coder:aDecoder);
}
override init(nibName nibNameOrNil: String?, bundle nibBundleOrNil: NSBundle?) {
super.init(nibName:nibNameOrNil, bundle:nibBundleOrNil);
}
override func viewDidLoad() {
super.viewDidLoad();
println("Using MyViewControllerSwift");
}
deinit {
//TODO
}
}
What do I need to do to get my view to display?
Thanks.
(Yes, this is a similar question to Loaded nib but the view outlet was not set - new to InterfaceBuilder but the view outlet does not show up. )
First - set custom class in your nib file
(File's Owner -> third icon -> custom class : YourViewController )
Second - the last icon in file's owner -> link (drag) the "view" property to the interface view
Init your ViewController like so:
YourViewController(nibName: "YourViewName", bundle: nil)
It will work. Don't do any manipulations with View.
If you have tried everything and you still get this error, try re-creating the class file from scratch but remember to select the "Also create XIB file" check box. This will auto link up a few items that are not linked when creating these files separately. After this is created, you can likely cut and paste everything onto the new XIB and it should work fine.
I am finding this issue specifically with creating files separately in Swift.
I had the same problem , my 2nd view on my view controller couldn't load . I changed the view's name in property(as I had previously given "View"). Your existing view controller already had references to the view on top of it with the same name(View). Hence the error . Try changing the outlet names . It worked perfect in my case .
Originally I ran into this same issue when I had an empty .xib and dropped in a UIViewController.
My fix was to remove the UIViewController and just add a UIView instead. I made the File's Owner (under Placeholders) Custom Class -> Class the name of the UIViewController class that would contain all the controller logic. I connected the UIView in the .xib file to the view Placeholder -> File's owner -> Outlets.
Maybe Apple/Xcode is trying to enforce a more strict MVC design by removing the UIViewController from everything but storyboard? I tried to follow the advice of many other posts I saw in trying to connect my UIViewController's view (of the UIViewController I originally dropped into the .xib) but it wouldn't connect no matter whether I made the Class type in File's Owner or for the UIViewController the same as my custom class.
This is the only thing that worked for me in Xcode 11.0
I found on chinese forum that it could be happen when you have similar names for ViewController and view from XIB.
Example SpecialOfferViewController and SpecialOfferView
I renamed
SpecialOfferViewController and OfferView and error has gone!!!
I had a xib file named FooView and a view controller class named FooViewController.
FooVew was not intended to the be the root view of FooViewController, only a subview. Yet, UIViewController's default behavior is to automatically attempt exactly that. And thus, my app would crash with this error at the first point that my controller's view was accessed.
This behavior can be avoided using one of:
Rename your controller class or the xib file to dissociate them
override var nibName: String? { nil }
override func loadView() and assign to view yourself
I went with 2 as it seemed the least likely sneak back to bite me later.

Error when set UICollectionViewController - nib but didn't get a UICollectionView

I have a trouble when set custom cell into UICollectionViewController.
This is my code.
KidsDetailViewController.h
#import <UIKit/UIKit.h>
#interface KidsDetailViewController : UICollectionViewController <UICollectionViewDataSource>
#property NSNumber *idCampana;
#property (weak, nonatomic) IBOutlet UICollectionView *grid;
#end
KidsDetailViewController.m
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
return [grid_kid count];
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
GridCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:#"MY_CELL" forIndexPath:indexPath];
cell.imageView.image = [UIImage imageNamed:self.truckImages[0]];
cell.prd_img.image = [UIImage imageNamed:#"ic_launcher_58x58"];
return cell;
}
GridCell.h
#import <UIKit/UIKit.h>
#interface GridCell : UICollectionViewCell
#property (weak, nonatomic) IBOutlet UIImageView *prd_img;
#end
GridCel.m
#import "GridCell.h"
#implementation GridCell
- (instancetype)initWithFrame:(CGRect)frameRect {
self = [super initWithFrame:frameRect];
if (self) {
// Initialization code
}
return self;
}
#end
This is the Error.
*** Assertion failure in -[KidsDetailViewController loadView], /SourceCache/UIKit_Sim/UIKit-3283.1/UICollectionViewController.m:166
2014-07-28 02:25:18.874 Geelbe[7673:231598] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: '-[UICollectionViewController loadView] loaded the "jXd-Ir-mr4-view-0IK-5Q-NzC" nib but didn't get a UICollectionView.'
*** First throw call stack:
I've tried everything and still gives error.
Thanks.
I had this issue when adding a UICollectionViewController as an embedded container view controller on a storyboard.
I switched the container view controller's custom class to be a Collection View Controller. But the view property inside that view controller was still the Xcode boilerplate UIView.
Delete the default UIView within your controller on the storyboard and replace it with a UICollectionView. The outlets should be reconnected automatically.
I think the problem may be because you inherit from UICollectionViewController, you do not have to specify anymore , because it already implements it. Try top delete it, see if it would fix your error.
Also in the Identity Inspector make sure you set the class KidsDetailViewController for the corresponding View Controller.
Jeff Kelley's answer here worked for me for same error.
When you’re using a UICollectionViewController, the view outlet needs
to be connected to a UICollectionView in your storyboard; if it’s a
UIView of another class, it’ll crash.
So, I used UIViewController with UICollectionView as its property and hooked up with nib.
How to setup a collection view controller with a nib (.xib) file.
Add a new file by selecting the "Empty" template from the "User Interface" section. Drag a UICollectionView object from the object library over.
1) Associate the nib which collection view controller class.
In the Document Outline (left hand section), under "Placeholders", select "File's Owner". Show the Identity Inspector and set the class dropdown to your subclass of UICollectionViewController.
2) Associate the nib with the UICollectionView.
Again, in the Document Outline (left hand section), under "Placeholders", select "File's Owner". Right click it to see the "Outlets" and "Referencing Outlets" display. Two of the selections under "Outlets" are "view" and "collectionView". Its counter-intuitive, but drag from the "view" selection to the UICollectionView object in the Document Outline.
** If you drag from the "collectionView" selection, you will get the 'loaded the "your_collection_view_controller" nib but didn't get a UICollectionView' error.
When you build and run, the code should now "load" the nib and also "get" the UICollectionView.

app crashes on device but not in simulator with:[UITableViewController loadView] loaded the "XXXViewController" nib but didn't get a UITableView.'

When running my app on the IOS simulator it just works fine, but if I run it on my device I get the next error:
Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: '-[UITableViewController loadView] loaded the "XXXViewController" nib but didn't get a UITableView.'
Could this be a bug, as it is working in iOS simulator.
Does anyone know how to fix this?
I have setup my view controller as
#interface XXXViewController : UITableViewController <UITableViewDataSource, UITableViewDelegate>
I have created the view programmatically, so there is no xib file (and no storyboard).
Adding
- (id)initWithStyle:(UITableViewStyle)style
{
return [self init];
}
solved the issue. as I was calling it in
-(id)init
{
// Call the superclass's designated initializer
self = [super initWithStyle:UITableViewStyleGrouped];
if (self) {
...
}
return self;
}
Instead of:
#interface XXXViewController : UITableViewController <UITableViewDataSource, UITableViewDelegate>
try:
#interface XXXViewController : UIViewController <UITableViewDataSource, UITableViewDelegate>
replace UITableViewController with UIViewController. Chances are that you have a UITableView and not a UITableViewController hence why you are explicitly setting up the delegate protocols in your .h file.
If you get other errors, please do let me know

App crashes when webViews try to load in a Tab Bar Controller

So the premise of my app is that it has multiple tabs and each tabViewController has a webView property that loads up a different webpage.
I'm not sure what exactly is wrong, and why it crashes.
*** Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[<UIViewController 0x6a7c5b0> setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key webView.'
The way I structured the app was I made separate ViewController classes for each tab. I included IBOutlets to the WebViews and linked them to webViews I included in the storyboard. I also set the CustomClass to the ViewController class.
Here is an example of my code (it's pretty much synonymous across the ViewControllers):
AmericaViewController.h
#import <UIKit/UIKit.h>
#interface AmericaViewController : UIViewController
#property (nonatomic, weak) IBOutlet UIWebView *webView;
#end
AmericaViewController.m
#import "AmericaViewController.h"
#implementation AmericaViewController
#synthesize webView = _webView;
-(void)viewDidLoad
{
[super viewDidLoad];
// allow user to pinch-zoom page
self.webView.scalesPageToFit = YES;
self.webView.multipleTouchEnabled = YES;
//load webView
[self.webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:#"http://baylornotes.org/articles/America"]]];
}
-(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
return YES;
}
#end
According to the exception, one of your UIViewController has no property webView.
Maybe one of your classes is not well written, or you forgot to set the right class in a xib...

Resources