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];
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 am working on an application that is transitioning to having multiple storyboards for different sections of the app. They are linked together by programmatically loading them at the appropriate times.
In a function for when a UIButton is tapped, it should programmatically load a storyboard. The code is like so:
- (void)loginTapped:(id)sender {
UIStoryboard *sb = [UIStoryboard storyboardWithName:#"Home.storyboard" bundle:nil];
UIViewController *vc = [sb instantiateViewControllerWithIdentifier:#"homeStoryboard"];
vc.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
[self presentViewController:vc animated:YES completion:NULL];
}
The storyboard is named the name listed in in the function:
and is listed as a target in the project settings appropriately:
and has the appropriate ID like so:
However, even after setting all this I get the error:
'NSInvalidArgumentException', reason: 'Could not find a storyboard named 'Home.storyboard' in bundle NSBundle
I have tried to follow the advice from this post here, but nothing has worked. How do I get this storyboard to load?
The solution (as mentioned in the comments above) is to not include the .storyboard when providing the name of the storyboard.
Old
UIStoryboard *sb = [UIStoryboard storyboardWithName:#"Home.storyboard" bundle:nil];
New
UIStoryboard *sb = [UIStoryboard storyboardWithName:#"Home" bundle:nil];
Try this:
UIStoryboard *sb = [UIStoryboard storyboardWithName:#"Home" bundle:nil];
To call any ViewController programmatically in ios using Objective c:
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:Home bundle:nil];
yourDestinationViewController *vc = [storyboard instantiateViewControllerWithIdentifier:#homeStoryboard];
[self presentViewController:vc animated:YES completed:^{}];
yourDestinationViewController in the code is your view controller.h file.
You want to import that viewcontroller.h file in your program, where you want to call the View controller programmatically.
and to create the property like below,
#import "viewcontroller.h"
#property viewController *yourDestinationViewController;
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 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.
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":