I downloaded the following code to implement a PDF page flipper from the apple ios developer library : https://developer.apple.com/library/ios/samplecode/ZoomingPDFViewer/Introduction/Intro.html
I copied the 2 uiviewcontrollers into my own storyboard and i added all the classes except the appdelegate. Then, in one of my previous uiviewcontrollers, i wrote :
RootViewController *page = [[RootViewController alloc] init];
[self.navigationController pushViewController:page animated:YES];
but this line returns nil instead of an instance of DataViewController :
DataViewController *dataViewController = [storyboard instantiateViewControllerWithIdentifier:#"DataViewController"];
Though I'm sure i set the storyboard ID to DataView Controller...
Thanks for your help !
If anyone encounters the same problem , here is what I did wrong : I copied/pasted the code but I was not careful about a line in the code which used
self.storyboard
You have to replace it by :
UIStoryboard *mainStoryboard = [UIStoryboard storyboardWithName:#"Main" bundle: nil];
where "Main" must be replaced by the name of your storyboard.
Related
I have the following code:
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"MyViewController" bundle:nil];
UINavigationController *navControllerInstance = [storyboard instantiateViewControllerWithIdentifier:NSStringFromClass([MyViewController class])];
When I use targets my problem is [MyViewController class] is returning "MyTarget_2.MyViewController" then my app crash because "'Storyboard (<UIStoryboard: 0x600001e65d80>) doesn't contain a view controller with identifier "
I have two targets:
MyTarget and MyTarget_2 and is only failing with the last one.
I don't understand is why [MyViewController class] is returning
MyTarget.MyViewController. When I start a new project this is not happening, only returns MyViewController . Why is such a difference?
This seems to be related because I'm using swift+objective-c
Calling NSStringFromClass on a Swift Class in Objective-C returns module mangled name
You can get class name like this :
NSString *className = [[NSStringFromClass([MyViewController class]) componentsSeparatedByString:#"."] lastObject];
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"MyViewController" bundle:nil];
UINavigationController *navControllerInstance = [storyboard instantiateViewControllerWithIdentifier:className];
and make sure that your storyboard id should be MyViewController
Did you give Identifier to your ViewController in storyboard ?
You can give it against Storyboard ID in ViewController's identify Inspector section.
Because ,
UINavigationController *navControllerInstance = [storyboard instantiateViewControllerWithIdentifier:NSStringFromClass([MyViewController class])];
method work on the identifier and not on class name
I think you haven't copied that particular storyboard in to your target. You can verify it. Click on target. Then on Build Phase -> Copy bundle resource. Check if storyboard file exist in both targets. And if storyboard is not there click on + button add that file. Clean and run. Hope it help.
I got a source code for an existing app. throughout the app the developer didn't use storyboard for the user interface but rather he used interface builder xib files. As a new developer, i don't know how to develop iOS apps in xcode using xib files for viewcontrollers, i learnt the storyboard way. Now I want to add more screens to the apps source code, i created a new storyboard file to define my new screens, how do I navigate to the entry point of this new storyboard from a button action i defined in one of the xib files so that i can easily implement my features.
while searching i got some suggestions like this one
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"newStoryboard_iPhone" bundle:nil];
MyNewViewController *myVC = (MyNewViewController *)[storyboard instantiateViewControllerWithIdentifier:#"myViewCont"];
but i dont think i understand how to implement this even if this can be in a button IBAction method. and besides subsequent navigations would be segues through the view controllers i define in the new storyboard
It's right suggestion.
You should get storyboard object by its name. Then you should create your viewController using [storyboard instantiateViewControllerWithIdentifier:#"..."];
Then you may navigate to your new VC using appropriate navigation method. For example:
[self.navigationController pushViewController:vc animated:YES]
ViewController identifier you should set in Interface Builder under section "Identity Inspector" in field "Storyboard ID".
That code you should place in IBAction method. But better would be to extract it in separate method. Something like this:
- (IBAction)buttonPressed:(UIButton *)sender {
[self navigateToMyNewViewController];
}
- (void)navigateToMyNewViewController {
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"myNewStoryboard" bundle:nil];
MyNewViewController *myNewVC = (MyNewViewController *)[storyboard instantiateViewControllerWithIdentifier:#"MyNewViewController"];
[self.navigationController pushViewController:myNewVC animated:YES];
}
Navigation from MyNewViewController your can implement as you want. It may be segues.
MyNewViewController *myVC = [self.storyboard instantiateViewControllerWithIdentifier:#"YOUR_VC_NAME_HERE"];
This will instantiate your ViewController, from here you can present it like so
[self presentViewController:myVC animated:YES completion:nil]
The image shows where you set the storyboard name that you're referring to
Try This it's working for me ,
Use below line of code :
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"Main"bundle:nil];
LoginViewController *loginVC = (LoginViewController*)[storyboard instantiateViewControllerWithIdentifier:#"LoginViewController"];
[self.navigationController pushViewController:loginVC animated:YES];
We need to set storyboard id as per mention in below,
you can navigate one storyboard to another storybord:
first you import your viewcontroller class to appdelegate.h
AppDelegate *appDelegateTemp = [[UIApplication sharedApplication]delegate];
appDelegateTemp.window.rootViewController = [[UIStoryboard storyboardWithName:#"storyboard_name" bundle:[NSBundle mainBundle]] instantiateInitialViewController];
Like the way you use storyboard, use
MainViewController *mainVC = [[MainViewController alloc] initWithNibName:#"MainViewController" bundle:nil]
[self.navigationController pushViewController:mainVC animated:YES];
I can't push from one view controller to the next. I keep getting the errors Use of undeclared identifier 'DriverViewController'
DriverViewController *vC = [[DriverViewController alloc] init];
[self.navigationController pushViewController:vC animated:YES];
I created another swift file and this seemed to work just fine. Idk why I cant get the code above to do the exact same thing.
var dVC = DriverViewController()
self.navigationController?.pushViewController(dVC, animated: false)
In Objective-C, you need to import your header files, even if they are written in Swift. To do that, you need an umbrella header.
Apple has covered this in the documentation. It will tell you how to exactly import Swift files in Objective-C, but simply put you need to import the umbrella header with this:
#import "ProductModuleName-Swift.h"
Documentation: https://developer.apple.com/library/ios/documentation/Swift/Conceptual/BuildingCocoaApps/MixandMatch.html#//apple_ref/doc/uid/TP40014216-CH10-XID_78
Are you using StroyBoard? If yes then you have to added identifier into your Viewcontroller.
For Ex:-
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"MainStoryboard_iPhone" bundle:nil];
DriverViewController *myVC = (DriverViewController *)[storyboard instantiateViewControllerWithIdentifier:#"DriverViewControllerID"];
Or
DriverViewController *myObj = [self.storyboard instantiateViewControllerWithIdentifier:#"DriverViewControllerID"];
[self.navigationController pushViewController:myObj animated:YES];
We have an error with our iOS game. The gameover screen (containing the score) doesn't come up after losing the game, as it just hangs up. Can anyone suggest what could be the problem and how can we fix this?
We are currently testing and we are not sure if this is the case because of the code or the test settings? But so far the Xcode doesn't show any errors.
Thanks for your help.
Open a view from a normal class:
UIStoryboard *storyBoard = [UIStoryboard storyboardWithName:storyBoardName bundle:nil];
UIViewController *viewController = [storyBoard instantiateViewControllerWithIdentifier:StoryboardID];
[self presentViewController:viewController animated:NO completion:nil];
Edit these strings to:
storyBoardName = The name of your storyboard.
storyBoardID = The StoryBoardID of your ViewController.
Open a view from the AppDelegate
UIStoryboard *storyBoard = [UIStoryboard storyboardWithName:storyBoardName bundle:nil];
UIViewController *viewController = [storyBoard instantiateViewControllerWithIdentifier:storyBoardID];
self.window = [[UIWindow alloc] initWithFrame:UIScreen.mainScreen.bounds];
self.window.rootViewController = viewController;
[self.window makeKeyAndVisible];
Edit these strings to:
storyBoardName = The name of your storyboard.
storyBoardID = The StoryBoardID of your ViewController.
Other information
How to get the storyBoardName?
If your StoryBoard is called Main.storyboard Main is the title.
If your StoryBoard is called MyName.storyboard MyName is the title
How to set a StoryBoardID?
Select your ViewController. Your View must now be highlighted blue. Go to the identity inspector and under identity you'll find StoryBoardID. Set it to whatever you like.
If it still doesn't work when you use this code you have put in the wrong variables, or there's an unknown error.
I don't know if this is possible or not but I wrote a project using xibs and want to modally switch to a storyboard project.
I know that to switch VC's you do something like
NewViewController *NewVC = [[NewViewController alloc] init];
[self presentModalViewController:NewVC animated:YES];
upon clicking a button, that I'll call "switch"
There is another project using solely storyboards that I want to call using "switch"
How do I go about doing this? Is it possible to do this?
Yes it is possible. You can do something like this:
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"YourStoryboardName" bundle:nil];
MainMenuViewController *mainmenuvc = [storyboard instantiateViewControllerWithIdentifier:#"mainMenuViewController"];
[self.navigationController pushViewController:mainmenuvc animated:YES];
EDIT
If I understand your comment, you want to change the root view controller. You can do so by:
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"RVCStoryboard" bundle:nil];
MainMenuViewController *mainmenuvc = [storyboard instantiateViewControllerWithIdentifier:#"mainMenuViewController"];
[[UIApplication sharedApplication].delegate].window.rootViewController = mainmenuvc;
Please note that for this to work, in your RVCStoryboard you have to select the viewcontroller you are trying to load, and assign a name to it so that the instantiateViewControllerWithIdentifier: method will work. Check the attached image, the field "Storyboard ID":