Error:does't contain a view controller with identifier? - ios

-(void)btnclick1:(UIButton *)sender
{
NSString *storyboardName=#"Main";
UIStoryboard* storyboard1 = [UIStoryboard storyboardWithName:storyboardName
bundle:nil];
secondViewController *add = [[secondViewController alloc]init];
add =[storyboard1 instantiateViewControllerWithIdentifier:#"nextseg"];
[self.view addSubview:add.view];
[self presentViewController:add
animated:YES
completion:nil];
}
I got error: reason: 'Storyboard () doesn't contain a view controller with identifier 'nextseg''

You need to set the Storyboard ID for the xib you want to use in the MainStoryboard Settings for the XIB, see image. In your case it should be #"nextseg"
Hope this helps.
Cheers.

Just add [NSBundle mainBundle] to the bundle argument where you get the storyboard from. Hope this helps
-(void)btnclick1:(UIButton *)sender
{
NSString *storyboardName=#"Main";
UIStoryboard* storyboard1 = [UIStoryboard storyboardWithName:storyboardName
bundle:[NSBundle mainBundle]];
secondViewController *add = [[secondViewController alloc]init];
add =[storyboard1 instantiateViewControllerWithIdentifier:#"nextseg"];
[self.view addSubview:add.view];
[self presentViewController:add
animated:YES
completion:nil];
}
Also make sure you have a identifier "nextseg" for your view controller. "nextseg" should not be the identifier of the segue leading to the view controller, but it should be for the view controller

Related

Initialize Swift storyboard from Objective-C

I'm trying to add a new storyboard in Swift to an old Objective-C app:
UIStoryboard *sb = [UIStoryboard storyboardWithName:#"MainStoryboard01" bundle:nil];
UIViewController *vc = [sb instantiateViewControllerWithIdentifier:#"OnboardingViewController"];
[self presentViewController:vc animated:YES completion:NULL];
With Swift storyboard:
I'm always getting error:
'Could not find a storyboard named 'MainStoryboard01' in bundle NSBundle <.../Developer/CoreSimulator/Devices/753636C1-ABB5-4D6E-B184-5C4638FB2CE9/data/Containers/Bundle/Application/398410C0-BB2C-4574-B058-95D30F8D1B5D/Credit Call.app> (loaded)'
Calling swift classes like
TestClass *instance = [TestClass new];
[instance testFunction];
Normally works. Any idea how to call swift storyboard + it's swfit controller in Objective-C app?
// EDIT:
I finally get working this:
UIStoryboard *sb = [UIStoryboard storyboardWithName:#"Onboarding" bundle:nil];
UIViewController *vc = [sb instantiateViewControllerWithIdentifier:#"MainStoryboard01"];
but on next line
[self presentViewController:vc animated:YES completion:NULL];
I get this error:
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[MyAppDelegate presentViewController:animated:completion:]: unrecognized selector sent to instance 0x6080000b3b60'
Any ideas? I need to replace this with the storyboard:
Registration *reg = [[Registration alloc] init];
registrationNav = [[UINavigationController alloc] initWithRootViewController:reg];
[reg release];
[self.mainBackgroundView addSubview:[registrationNav view]];
=> https://codepaste.net/jkfras I've added skeleton of the MyAppDelegate.m
You are making mistake here, MainStoryboard01 that you have set is Storyboard Identifer using that you can called instantiateViewControllerWithIdentifier. It is not the another storyboard name. So its should be simply like this.
//You need to put storyboard name here the check the image for more detail
UIStoryboard *sb = [UIStoryboard storyboardWithName:#"Main" bundle:nil];
UIViewController *vc = [sb instantiateViewControllerWithIdentifier:#"MainStoryboard01"];
[self presentViewController:vc animated:YES completion:nil];
Edit: Try this way.
UIStoryboard *sb = [UIStoryboard storyboardWithName:#"Main" bundle:nil];
UIViewController *vc = [sb instantiateViewControllerWithIdentifier:#"MainStoryboard01"];
[self.mainBackgroundView addSubview:[vc view]];
If you want to reference to a storyboard then you should use its name, so if your storyboard has a name "MyNewStoryboard" (MyNewStoryboard.storyboard) then the right way to create its reference is like this:
UIStoryboard *sb = [UIStoryboard storyboardWithName:#"MyNewStoryboard" bundle:nil];
Don't use a storyboard ID here. Storyboard ID you just set in the controller's attributes is the identifier for your controller. So you can use it this way:
UIViewController *vc = [sb instantiateViewControllerWithIdentifier:#"MainStoryboard01"];
Change your new controller's Storyboard ID to something more informative, so for example "OnboardingViewController", and then after putting your storyboard file name in your first line your code should be fine.

Search tableview row click to go details view

I have implement UISearchController in my HomeController.By this way
SearchTableViewController *searchResults = (SearchTableViewController *)self.controller.searchResultsController;
[self addObserver:searchResults forKeyPath:#"myResult" options:NSKeyValueObservingOptionNew context:nil];
searchResults.rootHomeController=_homeController;
[self presentViewController:self.controller animated:YES completion:nil];
My Search value showing well in SearchTableViewController.Now I need to go details after SearchTableViewController UITableView cell click i tried by this way but not working.
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:
#"Main" bundle:[NSBundle mainBundle]];
AlbumSongListController *detailSongList=[storyboard instantiateViewControllerWithIdentifier:#"AlbumSongListControllerID"];
[self presentViewController:detailSongList animated:YES completion:nil];
I need to pass navigation controller so that i can back to search controller
pass your result one view controller to another by declaring a varialble like:
let storyBoard : UIStoryboard = UIStoryboard(name: "Main", bundle:nil)
let resultViewController = storyBoard.instantiateViewControllerWithIdentifier("DestViewController") as! DestViewController
resultViewController.result = self.result
self.presentViewController(resultViewController, animated:true, completion:nil)
You need to present a navigation controller with SearchTableViewController as root in your HomeController. Thus you can perform navigations.
i.e. on click of table cell in SearchTableViewController (didSelectRowAtIndexPath delegate), push the detail view controller using [self.navigationcontroller pushviewcontroller] , you will get back navigation to SearchTableViewController by doing so.
plz use this code in app delegate
-(BOOL) application: (UIApplication * ) application didFinishLaunchingWithOptions: (NSDictionary * ) launchOptions {
UIStoryboard * storyboard = [UIStoryboard storyboardWithName:
# "Main"
bundle: [NSBundle mainBundle]
];
HomeVcController * HomeVcControllerOb = [storyboard instantiateViewControllerWithIdentifier: # "HomeVcControllerID"];
UINavigationController * navController = [
[UINavigationController alloc] initWithRootViewController: HomeVcControllerOb
];
self.window.rootViewController = navController;
}
and hidden NavigationBar on homeVWcontroller
[self.navigationController setNavigationBarHidden:YES animated:YES];
then after just use this
UIStoryboard * storyboard = [UIStoryboard storyboardWithName:
# "Main"
bundle: [NSBundle mainBundle]
];
AlbumSongListController * detailSongList = [storyboard instantiateViewControllerWithIdentifier: # "AlbumSongListControllerID"];
[self presentViewController: detailSongList animated: YES completion: nil];
and in AlbumSongListController show navigation bar
[self.navigationController setNavigationBarHidden:No animated:YES];

How to custom object between two storyboard in one project?

I have two storyboard in my project. I am usually can navigate from one View of storyboard to another View of another storyboard?
I am writing code as below
UIStoryboard* storyboard = [UIStoryboard storyboardWithName:#"Details" bundle:nil];
UIViewController * vc = [storyboard instantiateViewControllerWithIdentifier:#"Detail"];
// Get button tag number (or do whatever you need to do here, based on your object
NSInteger tagIndex = [(UIButton *)sender tag];
vc.modalTransitionStyle =UIModalTransitionStyleFlipHorizontal;
[self.navigationController pushViewController:vc animated:YES];
Now, I have three buttons, and tag on them, How can I pass Custom Object, kept in array to next controller of another storyboard ("DETAIL").
Thanks
Maybe this can help you :
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"storyboard2" bundle:nil];
[self presentModalViewController:[storyboard instantiateViewControllerWithIdentifier:#"storyboard2initialviewcontroller"] animated:NO];
EDIT :
Maybe like this, not sure it will help but i ll try :)
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([segue.identifier isEqualToString:#"MySegue"]) {
MyOtherViewController *destination = (MyOtherViewController *)segue.destinationViewController;
destination.someProperty = self.someOtherProperty;
}
}
and
UIStoryboard *secondStoryBoard = [UIStoryboard storyboardWithName:#"secondStoryBoard" bundle:nil];
MyOtherViewController *myViewController = (MyOtherViewController *)[secondStoryBoard instantiateViewControllerWithIdentifier:#"myOtherViewController"];
myViewController.someProperty = self.someOtherProperty;
[self.navigationController pushViewController:myViewController animated:YES];
You can pass the custom object like below
UIStoryboard *storyBoard = [UIStoryboard storyboardWithName:#"Details" bundle:nil];
HomeViewController *homeVC = [storyBoard instantiateViewControllerWithIdentifier:#"view2"];
// here I am passing string to the another controller (i.e HomeViewController), you can pass any object that you want
homeVC.info = #"This is test string";
[self.navigationController pushViewController:homeVC animated:YES];
Hope this helps you

UIStoryboard with object

I have multiple storyboards within my app. I want to pass an object when a new storyboard is opened.
I'am doing this:
UIStoryboard* storyboard = [UIStoryboard storyboardWithName:#"SetupStoryboard" bundle:[NSBundle mainBundle]];
UINavigationController* initialHelpView = [storyboard instantiateInitialViewController];
SetupViewController *setup = (SetupViewController*) [initialHelpView topViewController];
setup.data = self.data;
initialHelpView.modalPresentationStyle = UIModalPresentationFormSheet;
[self presentViewController:initialHelpView animated:YES completion:nil];
But when the storyboard is presented the setup.data is nil in viewDidLoad, viewWillAppear etc of the SetupViewController...
Why is that?
I don't see anything wrong with this code. Problem may be elsewhere.

How to call a View Controller programmatically?

I have looked at all the tutorials I can find on this one, and I still don't have the answer. I need to call another view from the code. I am using UIStoryboards. I have changed the view many times by control-dragging from UIButtons, but now it must be from the code. I am trying to call the info page from the main menu if it is the first time the user has opened the app. I cannot seem to find a way to change the views from the code, however. All my views are controlled by the same files (ViewController2). The identifier of my main menu is ViewControllerMain, and the identifier of the info page is ViewControllerInfo. First I tried this:
[ViewControllerMain presentViewController: ViewControllerInfo
animated:YES
completion: NULL];
Then I tried making different UIViewControllers for each and saying:
[ViewController2 presentViewController: ViewController
animated:YES
completion: NULL];
Neither worked. For the first one, it says:
Use of undeclared identifier ViewControllerMain.
In the second one, it says:
unexpected interface name 'ViewController': expected identifier.
What can I do?
To create a view controller:
UIViewController * vc = [[UIViewController alloc] init];
To call a view controller (must be called from within another viewcontroller):
[self presentViewController:vc animated:YES completion:nil];
For one, use nil rather than null.
Loading a view controller from the storyboard:
NSString * storyboardName = #"MainStoryboard";
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:storyboardName bundle: nil];
UIViewController * vc = [storyboard instantiateViewControllerWithIdentifier:#"IDENTIFIER_OF_YOUR_VIEWCONTROLLER"];
[self presentViewController:vc animated:YES completion:nil];
Identifier of your view controller is either equal to the class name of your view controller, or a Storyboard ID that you can assign in the identity inspector of your storyboard.
You need to instantiate the view controller from the storyboard and then show it:
ViewControllerInfo* infoController = [self.storyboard instantiateViewControllerWithIdentifier:#"ViewControllerInfo"];
[self.navigationController pushViewController:infoController animated:YES];
This example assumes that you have a navigation controller in order to return to the previous view. You can of course also use presentViewController:animated:completion:. The main point is to have your storyboard instantiate your target view controller using the target view controller's ID.
Swift
This gets a view controller from the storyboard and presents it.
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let secondViewController = storyboard.instantiateViewController(withIdentifier: "secondViewControllerId") as! SecondViewController
self.present(secondViewController, animated: true, completion: nil)
Change the storyboard name, view controller name, and view controller id as appropriate.
You can call ViewController this way, If you want with NavigationController
1.In current Screen : Load new screen
VerifyExpViewController *addProjectViewController = [[VerifyExpViewController alloc] init];
[self.navigationController pushViewController:addProjectViewController animated:YES];
2.1 In Loaded View : add below in .h file
#interface VerifyExpViewController : UIViewController <UINavigationControllerDelegate>
2.2 In Loaded View : add below in .m file
#implementation VerifyExpViewController
- (void)viewDidLoad
{
[super viewDidLoad];
self.navigationController.delegate = self;
[self setNavigationBar];
}
-(void)setNavigationBar
{
self.navigationController.navigationBar.backgroundColor = [UIColor clearColor];
self.navigationController.navigationBar.translucent = YES;
[self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:#"B_topbar.png"] forBarMetrics:UIBarMetricsDefault];
self.navigationController.navigationBar.titleTextAttributes = #{NSForegroundColorAttributeName: [UIColor whiteColor]};
self.navigationItem.hidesBackButton = YES;
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:#"Btn_topback.png"] style:UIBarButtonItemStylePlain target:self action:#selector(onBackButtonTap:)];
self.navigationItem.leftBarButtonItem.tintColor = [UIColor lightGrayColor];
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:#"Save.png"] style:UIBarButtonItemStylePlain target:self action:#selector(onSaveButtonTap:)];
self.navigationItem.rightBarButtonItem.tintColor = [UIColor lightGrayColor];
}
-(void)onBackButtonTap:(id)sender
{
[self.navigationController popViewControllerAnimated:YES];
}
-(IBAction)onSaveButtonTap:(id)sender
{
//todo for save button
}
#end
Hope this will be useful for someone there :)
There's 2 ways you can do this:
1, Create a segue to your ViewController in your Storyboard as explained in my answer here: How to perform a segue that is not related to user input in iOS 5?
2, Give your ViewController and identifier and call it using the code in my answer here: Call storyboard scene programmatically (without needing segue)?
main logic behind this is_,
NSString * storyboardIdentifier = #"SecondStoryBoard";
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:storyboardIdentifier bundle: nil];
UIViewController * UIVC = [storyboard instantiateViewControllerWithIdentifier:#"YourviewControllerIdentifer"];
[self presentViewController:UIVC animated:YES completion:nil];
UIStoryboard* storyboard = [UIStoryboard storyboardWithName:#"MainStoryboard_iPhone_iOS7" bundle:nil];
AccountViewController * controller = [storyboard instantiateViewControllerWithIdentifier:#"accountView"];
// [self presentViewController:controller animated:YES completion:nil];
UIViewController *topRootViewController = [UIApplication sharedApplication].keyWindow.rootViewController;
while (topRootViewController.presentedViewController)
{
topRootViewController = topRootViewController.presentedViewController;
}
[topRootViewController presentViewController:controller animated:YES completion:nil];
Import the view controller class which you want to show and use the following code
KartViewController *viewKart = [[KartViewController alloc]initWithNibName:#"KartViewController" bundle:nil];
[self presentViewController:viewKart animated:YES completion:nil];

Resources