NSViewController initWithCoder - bundle has not been loaded - objective-c

I have a viewController in my storyboard that I have set up like below:
I am using the following code to load it programmatically.
-(AttributeViewController*)AttributeVC{
if(!_AttributeVC)
{
_AttributeVC = (AttributeViewController*)[self.storyboard
instantiateControllerWithIdentifier:#"MyAttribute"];
}
return _AttributeVC;
}
my app crashes with:
-[NSViewController initWithCoder:] could not instantiate an NSViewController for a
nib in the "Attribute" bundle because the bundle has not been loaded.
Why is the view controller now trying to load the "Attribute" bundle that I don't have.
Thanks.

Try to include the storyboardWithName reference in there as well... This worked for me
-(AttributeViewController*)AttributeVC{
if(!_AttributeVC)
{
_AttributeVC *attributeVC = [[UIStoryboard storyboardWithName:#"MainStoryboard" bundle:nil] instantiateViewControllerWithIdentifier:#"MyAttribute"];
}
return _AttributeVC;
}

Related

how to pass information from Objective-c's viewcontroller to a Swift storyboard

I started to work in a project which was already started in ObjectiveC and without Storyboard. I'm making the new sections in Swift and with Storyboard, but now I'm having a problem when I try to pass information (an UIImage) to my storyboard's controllers.
This is how I go to the ViewControllers:
-(void)expenseCamera:(VOCCustomCameraVC *)camera didFinishPickImage:(UIImage *)
image imageCreated:(NSString *)imageCreated
fileName:(NSString *)fileName {
[camera dismissViewControllerAnimated: YES completion: nil];
UIStoryboard *addExpenseStoryboard = [UIStoryboard
storyboardWithName:#"addExpense" bundle:nil];
CreateExpenseVC *createExpenseVC = [addExpenseStoryboard
instantiateViewControllerWithIdentifier:#"expenseNavController"];
UIImage *compressedImage = [UIImage compressImage: image];
createExpenseVC.expenseImage = compressedImage; // It crashes here
//createExpenseVC.delegate = self;
[self presentViewController: createExpenseVC animated: YES completion: nil];
CFRunLoopWakeUp(CFRunLoopGetCurrent());
}
If I take out the createExpenseVC.expenseImage = compressedImage everything goes well, but when I try to pass the image I get this error:
SIGABRT *** Terminating app due to uncaught exception 'NSInvalidArgumentException',
reason: '-[YourApp.CustomNavController setExpenseImage:]: unrecognized selector
sent to instance 0x12e09b200'
The destination viewController starts this way
#objcMembers class CreateExpenseVC: UIViewController {
/* ---- IBOutlets ---- */
#IBOutlet weak var containerView: UIView!
#IBOutlet weak var backButton: UIBarButtonItem!
#objc var expenseImage: UIImage?
And in the containerView I show another viewController where I pretend to show the image.
I tried to change the code I use to navigate to this
CreateExpenseVC * createExpenseVC = [[CreateExpenseVC alloc] init];
But it also crashes because (I think) it's not instantiating the Storyboard and therefore not recognizing all the IBOutlets and so on...
So how can I pass information like this?
Many thanks in advance
I tried to replicate your problem with the way you have implemented and it worked fine for me.
But your error said, that [YourApp.CustomNavController setExpenseImage:] is not implemented. That means your view controller is embedded inside a navigation controller.
And you are trying to get navigation controller by this code
[addExpenseStoryboard
instantiateViewControllerWithIdentifier:#"expenseNavController"]
so your createExpenseVC is actually of type Navigation controller and not CreateExpenseViewController.
And that is why it doesn't get the expenseImage.
That is where your problem lies
Here is what you can do..
UIStoryboard *addExpenseStoryboard = [UIStoryboard storyboardWithName:#"CreateExpense" bundle:nil];
UINavigationController *navController = [addExpenseStoryboard instantiateViewControllerWithIdentifier:#"ExpenseNavC"];
CreateExpenseViewController *createExpenseVC = navController.viewControllers[0];
UIImage *image = [UIImage imageNamed:#"383975"];
createExpenseVC.expenseImage = image;
[self presentViewController: navController animated: YES completion: nil];
When dealing with this issue, I would check multiple things.
Is storyboard ID assigned?
Is the correct UIViewController class assigned?
Is the UIViewController has view outlet?
Check these screenshot for a visual guide:
View controller identify inspector screenshot
View controller view outlet screenshot

UiViewControllers are not loaded from tab bar item in swift after using objective c

I am writing an application in swift 3 where I have several views connected by segues for different tab bar item.I imported a rotary wheel project from Github using bridging header and dropped all the objective c files inn my project. In the Appdelegate.swift file I converted the codes in Appdelegate.m file and paste it there
var window: UIWindow?
var viewController: SMViewController!
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
self.window! = UIWindow(frame: UIScreen.main.bounds)
self.viewController = SMViewController(nibName: "SMViewController", bundle: nil)
self.window!.rootViewController = self.viewController
self.window!.makeKeyAndVisible()
return true
}
The wheel image is loaded in the SMViewController.m file
- (void)viewDidLoad
{
[super viewDidLoad];
SMWheelControl *wheel = [[SMWheelControl alloc] initWithFrame:self.wheelContainer.bounds];
[wheel addTarget:self action:#selector(wheelDidChangeValue:) forControlEvents:UIControlEventValueChanged];
[wheel insertSubview:[[UIImageView alloc] initWithImage:[UIImage imageNamed:#"WheelBackground"]] atIndex:0];
wheel.delegate = self;
wheel.dataSource = self;
[wheel reloadData];
[self.wheelContainer addSubview:wheel];
self.wheel = wheel;
}
I imported SMViewController.xib file in the project. And also in the Main.Storyboard file I set the viewcontroller as SMViewController from the identity inspector. When my app launches, the wheel from the git project is showing fine after the launch screen but all my other views are not showing. I dont know why this is happening. Any help is highly appreciated.
I was running right. I don't know exactly meaning of "other views",Is it write in your SMViewController ? Can you upload your code in GitHub , I could help you more.
set your ViewController's Nav "is Initial View Controller"
set your ViewController's Class
remove code you added in "AppDelegate.swift"
Then you will see the VC you want, I guess.

Storyboard to a nib

I have an RSS parser in a UITableView that pushes to the detail view when one of the rows is selected. I had it working fine in the old UI format of using Nib files, but I wanted to transfer everything to UIStoryboard. I learned that UIStoryboards and nibs were compatible, so I decided I would keep the same code, but put the UITableView in the UIStoryboard and have the detail view be its own nib. I linked everything up and it should be working, and its not giving me any errors, but its not. Is there anything that I missed, or are storyboards and nibs not compatible at all.
Edit:
- (id)initWithItem:(NSDictionary *)theItem {
if (self == [super initWithNibName:#"RssDetailController" bundle:nil]) {
self.item = theItem;
self.title = [item objectForKey:#"title"];
}
return self;
}
Are you pushing from a UIViewController in UIStoryboard to a NIB file?
If so check out this sample project that pushes from storyboard to a NIB:
// in a navigation controller
// to load/push to new controller use this code
// this will be next screen
DetailsViewController *detailsViewController = [[DetailsViewController alloc ]init];
[self.navigationController pushViewController:detailsViewController animated:YES];
// to go back or pop controller use this
// now it will send back to parent screen
[self.navigationController popViewControllerAnimated:YES];

NSBundle loading a NSViewController

I'm looking into a project that would upload custom NSBundles that include a NSViewController. In my main program I've got this code to deal with the bundle after it's been loaded...
id principalClass = [loadedBundle principalClass];
id instance = [[principalClass alloc] init];
[localLabel setStringValue:[instance name]];
NSView *incomingView = [[instance viewController] view];
[localView addSubview:incomingView];
[localView display];
And the principal classes init method in the bundle looks like this...
-(id) init {
if(self = [super init]){
name = #"My Plugin";
viewController = [[ViewController alloc] initWithNibName:#"View" bundle:nil];
}
return self;
}
View.nib is a nib located in the bundles project. But whenever I load the bundle I get this error...
2010-05-27 09:11:18.423 PluginLoader[45032:a0f] unable to find nib named: View in bundle path: (null)
2010-05-27 09:11:18.424 PluginLoader[45032:a0f] -[NSViewController loadView] could not load the "View" nib.
I know I've got everything wired up because the line [label setStringValue:[instance name]]; sets the label text correctly. Plus, if I take all of the clases in the bundle and load them into my main applications project everything works as expect. Any thoughts on how I can correctly reference "View" in my bundle?
Thanks!
In the init method, you shouldn't pass nil to the bundle parameter. According to the UIViewController documentation, passing nil will look up the NIB file in the main bundle (the application's one), which is not what you want.
You can workaround, by using a specialized initializer like this:
- (id) initWithBundle:(NSBundle *)bundle {
if(self = [super init]){
name = #"My Plugin";
viewController = [[ViewController alloc] initWithNibName:#"View" bundle:bundle];
}
return self;
}
And use it as follow:
Class principalClass = [loadedBundle principalClass];
id instance = [[principalClass alloc] initWithBundle:loadedBundle];
"View.xib is a nib"? Either it is a xib or it is a nib. A xib is not a nib. xib files can only be used in the interface builder, in deployment applications they must be transformed to nib files. That is what ibtool is doing. Xcode does not simply copy your xib files, it runs them through ibtool that converts them to nib files that are then placed into the Resources folder.

Is it possible to go rootViewController page from any page?

I am trying to build an iPhone app. I am trying to build it at View based application.
Suppose I have gone to y.xib file from x.xib nib file. and x.xib nib file has been come from root.xib file. I would like to go root.xib file from y.xib.
How?
[self.navigationController popToRootViewControllerAnimated:YES]
Take a look at the popToRootViewControllerAnimated method on the UINavigationController.
in didFinishLaunchingWithOptions:
_initalStoryboard = self.window.rootViewController.storyboard;
- (void)resetWindowToInitialView
{
for (UIView* view in self.window.subviews)
{
[view removeFromSuperview];
}
UIViewController* initialScene = [_initalStoryboard instantiateInitialViewController];
self.window.rootViewController = initialScene;
}