Below is an example of using initWithNibName with separate xib views:
TerminalViewController *ctrl = [[TerminalViewController alloc]
initWithNibName:#"ControllerView" bundle:[NSBundle mainBundle]];
ctrl.appDelegate = self;
viewCtrl = ctrl;
However i need to implement it with a storyboard UI layout. For 'initWithNibName' how can i point to a View in my storyboard:
i.e. :
TerminalViewController *ctrl = [[TerminalViewController alloc]
initWithNibName:#"STORYBOARD.ControllerView" bundle:[NSBundle mainBundle]];
ctrl.appDelegate = self;
Any help is much appreciated.
Thanks,
Dave
You can give the controller an Identifier in the storyboard and then use this...
[self.storyBoard instantiateViewControllerWithIdentifier:#"TheIdentifier"];
Just to add to this...
When creating a UIViewController subclass from a xib file. If you have a class called MyViewController and a xib called MyViewController.xib then all you need to do is...
MyViewController *controller = [[MyViewController alloc] init];
The system will then look for a xib file called MyViewController.xib and use that to init the object and only fallback to doing it in code if the xib file doesn't exist.
get storyboard instance with name of storyboard and set identifier to all scene and get the instance
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"MainStoryboard_iPhone" bundle:nil];
MyNewViewController *myVC = (MyNewViewController *)[storyboard instantiateViewControllerWithIdentifier:#"myViewCont"];
UIStoryboard *stryBoard=[UIStoryboard storyboardWithName:#"MainStoryboard" bundle:nil];
TerminalViewController *ctrl = [stryBoard instantiateViewControllerWithIdentifier:#"ControllerView"];
Related
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.
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 am using UINavigationController but when i push a view controller just simply by making object of the class the class load but not the components in it.
this is my AppDelegate.m
UIStoryboard *sb = [UIStoryboard storyboardWithName:#"Main" bundle:nil];
ViewController *vc = [sb instantiateInitialViewController];
UINavigationController *nav = [[UINavigationController alloc] init];
self.window.rootViewController = nav;
nav.viewControllers = #[vc];
and my first UIViewController if action button code is
NextView *next = [[NextView alloc] init];
[self.navigationController pushViewController:next animated:YES ];
it gets to next UIViewController but no internal components are loaded such as - button,textfield,etc.
and if i try this code it give me runtime error
UIStoryboard *sb = [UIStoryboard storyboardWithName:#"Main" bundle:nil];
NextView *next = [sb instantiateViewControllerWithIdentifier:#"two"];
[self.navigationController pushViewController:next animated:YES ];
make sure that NextView inherit UIViewController and you have given identifier as two to that view controller and you have set class NextView from identity inspector to that viewcontroller.
Hope this will help :)
You could not get outlets (buttons etc.) to be loaded with alloc init in the subclass of UIViewController. You should init it with the xib file, using -initWithNibName:bundle: method. Or you can instantiate it with storyboard.
// You can get current storyboard from the View Controller
UIStoryboard *sb = vc.storyboard;
NextView *next = [sb instantiateViewControllerWithIdentifier:#"two"];
[self.navigationController pushViewController:next animated:YES];
I have a form design by xib file. In form have a button "Done". I want when click done button, it will forward to my view controller.
I tried code like this:
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"Main.storyboard" bundle:nil];
CommentViewController *myVC = (CommentViewController *)[storyboard instantiateViewControllerWithIdentifier:#"Comment"];
[self presentModalViewController:myVC animated:YES];
But error at "presentModelViewController".
" No visible #interface for 'my form' declares the selector 'presentModalViewController'...
If self is UINavigationController, use this:
[self.navigationController pushViewController:yourViewController animated:YES];
If self is UIViewController, use this:
[self presentModalViewController:yourViewController animated:YES];
Note that: presentModalViewController: animated: is deprecated now.
The problem is that self is not a UIViewController. So you can't send presentModalViewController to self. Send it to a view controller!
UIStoryboard *loStoryboard ;
loStoryboard = [UIStoryboard storyboardWithName:#"Main" bundle:nil]; // Here only "Main" not "Main.storyboard".
// Instantiate the initial view controller object from the storyboard
UIViewController *initialViewController = [loStoryboard instantiateInitialViewController];
// Instantiate a UIWindow object and initialize it with the screen size of the iOS device
self.view = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Set the initial view controller to be the root view controller of the window object
[self.navigationController pushViewController:initialViewController animated:YES];
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.