I am not able to include the particular controller inside my code but other controllers are included, I included the controller as header but I am not able to include it inside. I attach my code here, I am not able to include IndexPageTableViewController as favoritesViewController?
#import "IndexPageTableViewController.h"
#import "FavoritesViewController.h"
-(void) navigateToFavorites
{
UIStoryboard *mainStoryboard = [UIStoryboard storyboardWithName:#"Main_iPhone"
bundle: nil];
if(mainStoryboard)
{
FavoritesViewController *favorites = (FavoritesViewController *) [mainStoryboard instantiateViewControllerWithIdentifier:#"FavoritesViewController"];
[[SlideNavigationController sharedInstance ] popToRootAndSwitchToViewController:favorites withSlideOutAnimation:NO andCompletion:^{
[self initTableView];
}];
}
}
As you mentioned in comment that you are getting signal sigabrt error that means you may not set FavoritesViewController as identifier in interface builder. you can set Storyboard ID under identity under Identity Inspector. Custom class and storyboard id both are different thing. I think FavoritesViewController is your class that you have set from identity inspector. If you want to instantiate storyboard then you have to set storyboard id and then you can use that id to instantiate view controller.
Second thing make sure that your storyboard's name is correct and your view controller is within this storyboard. And you have make proper setup if you are using multiple storyboard.
Related
What is the functional difference between instantiating a View Controller from the storyboard and creating a new instance of it? For example:
#import "SomeViewController.h"
...
SomeViewController *someViewController = [SomeViewController new];
versus
#import "SomeViewController.h"
...
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"Main" bundle: nil];
SomeViewController *someViewController = [storyboard instantiateViewControllerWithIdentifier:#"SomeViewController"];
In either case, is someViewController effectively the same thing?
The main difference is in how the subviews of your UIViewController get instantiated.
In the second case, all the views you create in your storyboard will be automatically instantiated for you, and all the outlets and actions will be set up as you specified in the storyboard.
In the first, case, none of that happens; you just get the raw object. You'll need to allocate and instantiate all your subviews, lay them out using constraints or otherwise, and hook up all the outlets and actions yourself. Apple recommends doing this by overriding the loadView method of UIViewController.
In the second case, the view controller will load its view from the storyboard and you will be happy.
In the first case, it won't. Unless you've taken other steps (like overriding loadView or viewDidLoad or creating a xib named SomeViewController.xib), you'll just get an empty white view and be sad.
In Swift you can do the same with,
var someVC = self.storyboard?.instantiateViewControllerWithIdentifier("SomeViewController") as! SomeViewController
You will need to give the Identifier in the Storyboard to the SomeViewController and tick the checkmark to Use Storyboard ID
It is not the same thing. In the storyboard you probably have some UI elements laid out. They might have constraints and properties setup through the storyboard. When you instantiate the viewcontroller via the storyboard, you are getting all the instructions for where those subviews are and what their properties are. If you just say [SomeViewController new] you are not getting all the instructions that the storyboard has for the view controller.
A nice test will be to add a UIViewController to a storyboard and drag a red view onto it. Instantiate it using both methods and see what the differences are.
simple swift 3 extension
fileprivate enum Storyboard : String {
case main = "Main"
}
fileprivate extension UIStoryboard {
static func loadFromMain(_ identifier: String) -> UIViewController {
return load(from: .main, identifier: identifier)
}
static func load(from storyboard: Storyboard, identifier: String) -> UIViewController {
let uiStoryboard = UIStoryboard(name: storyboard.rawValue, bundle: nil)
return uiStoryboard.instantiateViewController(withIdentifier: identifier)
}
}
// MARK: App View Controllers
extension UIStoryboard {
class func loadHomeViewController() -> HomeViewController {
return loadFromMain("HomeViewController") as! HomeViewController
}
}
In case you don't want to instantiate a new VC using instantiateViewControllerWithIdentifier but accessing the instance created by the storyboard from the AppDelegate:
create a property in AppDelegate.h so it will be accessible from classes using it
#property (nonatomic, strong) myViewControllerClass*vC;
in viewDidLoad inside myViewControllerClass.m I access the shared instance of AppDelegate and feed the property with self: [AppDelegate sharedInstance].vC = self;
I had to use this solution in a complex storyboard and still can't get over the fact that I cannot find an easy way to access all (or at least the ones I need) objects in storyboard simply by addressing their identifiers.
another thing to check for is if the viewcontroller that's throwing the error has a storyboardIdentifier, you can check the storyboard xib file.
the identifier was missing in my case, the error stopped when i added it
I have a storyboard that handles all of my applications UI, apart from the header on each page which is handled by its own .xib file following this guide:
http://patientprogrammer.wordpress.com/2012/03/12/re-usable-subviews-in-ios/
Within my header .xib I have a button that I want to have when clicked load a view that is part of my story board.
I have tried:
- (IBAction)clicked:(id)sender {
UIStoryboard *mainStoryBoard = [UIStoryboard storyboardWithName:#"Main" bundle:nil];
someViewController *storyViewController = [mainStoryBoard instantiateViewControllerWithIdentifier:#"someViewController"];
[self.navigationController pushViewController:storyViewController animated:YES];
}
However as my class uses the interface UIView controller navigationController is not found, what can I do to launch a view within the story board from my UIView.
Or if there another interface I can use that will still let me have this as a subview.
what can I do to launch a view within the story board from my UIView
This is bad. A view is a view. Its purpose is to show something to the user, not controlling the app.
UIViewController is where you will need to do this. The navigationController is a property on this class, not on UIView. Read UIViewController class reference for more info.
So i'm using a storyboard AND a xib.
I use a tableview in my ViewController and it has a custom header as a xib file (the VC is the File Owner).
I want to present my new VC when swiping on the header, I added a swipe gesture to the xib using IB, but now i'm having problem presenting it.
Crash comes when i'm trying to instantiateViewControllerWithIdentifier. I made a property of my presented VC. And set the right Identifier "swipeRight".
- (IBAction)swipedRight:(id)sender {
NSLog(#"right");
if (_mpvc == nil) { //user those if only so the use wont push it twice and more
_mpvc = [self.storyboard instantiateViewControllerWithIdentifier:#"swipeRight"];
}
[self presentViewController:_mpvc animated:YES completion:nil];
}
The error:
Cannot find executable for CFBundle 0x9022120 </Applications/Xcode.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator7.1.sdk/System/Library/AccessibilityBundles/GeoServices.axbundle> (not loaded)
If you are inside a XIB file, how do you know which storyboard are you using? So first you have to instantiate storyboard which contains that viewController with specified identifier:
UIStoryboard *st = [UIStoryboard storyboardWithName:#"StoryboardName" bundle:nil];
After you have to instantiate viewController with this storyboard that you created:
UIViewController _mpvc = [st instantiateViewControllerWithIdentifier:#"swipeRight"];
Then finally you have to present the viewController:
[self presentViewController:_mpvc animated:YES completion:nil];
Hope it solves your problem!
I created empty IOS project and want to create UI. I searched for information about connecting storyboard to code but there is nothing about it, just about xib files. So is it possible to use storyboard instead xib in empty project? And how?
You can get the storyboard object like this assuming you have a Storyboard file already in the Project
UIStoryboard *storyBoard = [UIStoryboard storyboardWithName:#"Main_iPhone" bundle:[NSBundle mainBundle]];
YourViewController *yourVC = [storyBoard instantiateViewControllerWithIdentifier:#"identifier"];
//Do whatever with the viewcontroller object
[self.navigationController pushViewController:yourVC animated:YES ];
Note:
Check the Storyboard name
Check your viewcontroller identifier in the Storyboard
First create an empty project with some class name
then create an storyboard from file->userInterface->storyboard
then give a name for the storyboard after that press storyboard in the left menu and place a viewcontroller and hold viewcontroller give class name as already created Uiviewcontrollerclass
finally press project choose main interface as ViewController.storyboard
if you wanna code suppose segue use [self performSegueWithIdentifier: #"nameofcontroller" sender: self];
if don want segue go like this
[self performSegueWithIdentifier: #"TheSequeName" sender: self];
The #"TheSequeName" is defined when you ctrl drag a button to open a new view controller on the storyboard
If you don't use segue, there are many variants to open a new view controller. The basic one is using a separate NIB (without storyboard)
SecondViewController *view = [[SecondViewController allow] initWithNibName:#"NibName" bundle:nil];
If you declare your view controller on Storyboard, you can use
viewController *view = [self.storyboard instantiateViewControllerWithIdentifier:#"viewStoryboardId"];
Then you show your viewController using navigation controller
[self.navigationController pushViewController:view animated:YES];
Hope it works
From the 'File' menu, create a new Storyboard.
Call it Main.storyboard.
In your project settings, set it as the main interface file under the Deployment Info.
New File - > Objective-C Class (of type View Controller) -> without Xib file
now on on storyboard, go to that particular xib file
in property window -> Show Identity Inspector -> change class to above added ViewController.
That's it, now you can control your storyboard xib files from code.
How do you identify a UIStoryboard?
The class has methods which create and instantiate but I don't see an #property with something like name. E.g.
Getting a Storyboard Object
+ storyboardWithName:bundle:
Instantiating Storyboard View Controllers
– instantiateInitialViewController
– instantiateViewControllerWithIdentifier:
Any suggestions?
====
UPDATE
I was hoping for something like self.storyboard.name or [self.storyboard description], e.g.:
NSLog(#"This Storyboard is: %#", self.storyboard.name);
Perhaps it's not meant to be.
You can identify a storyboard by its name in the project navigator:
You can identify a view controller from a storyboard by setting its Storyboard ID in the identity inspector in interface builder:
Once you have these, then you can access them through your code:
UIStoryboard *iPhoneStoryboard = [UIStoryboard storyboardWithName:#"MainStoryboard_iPhone" bundle:nil];
ViewController *firstViewController = [iPhoneStoryboard instantiateViewControllerWithIdentifier:#"FirstViewController"];