How can i create a react-native module with storyboard - ios

I have received a sdk with a storyboard and multiple view controllers.
In my react-native module I want to expose a method that will transition to this storyboard and the get the result back.
I have tried to follow this answer:
React-Native iOS - How can I navigate to a non-React-Native view (native iOS view controller) from a React-Native view with a button press?
I have also tried this code (but we don't have access to the navigationController):
- (void)openStoryBoard{
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"NAME_OF_THE_STORYBOARD" bundle:nil];
UIViewController *vc = [storyboard instantiateInitialViewController];
[self.navigationController pushViewController:vc animated:true];
}
in Android i have just called StartActivityForResult but i couldn't figure it out in ios.
any suggestions are welcomed.

Related

How to navigate to a view controller on a new storyboard file in objective-c

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];

Confused about storyboards and pushing programmatically ios Objective C

I am really confused about the relationship between storyboards and pushing to views programmatically.
I am using SWRevealViewController to display a menu of items.
If I push to the storyboard using
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"Main" bundle:nil];
PhotosViewController *controller = [storyboard instantiateViewControllerWithIdentifier:#"PhotosViewController"];
[self presentModalViewController:controller animated:YES];
[self.navigationController pushViewController:controller animated:YES];
All of the information in my storyboard is displayed but there is no "back" button to the SWRevealViewController.
If I push to the view controller using
PhotosViewController *frontViewController = [[StreamScreen alloc] init];
newFrontController = [[UINavigationController alloc] initWithRootViewController:frontViewController];
Then I can view everything that I have added programmatically but nothing from the storyboard.
My question is how can I access things from both storyboard and things Ive added programmatically.
if you present the view controller then it will not give you default back button because when you present a controller it will not added in navigation stack of NavigationController so it won't give you that option.
If you want push controller do not use presentModalViewController.
Try like below
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"Main" bundle:nil];
PhotosViewController *controller = [storyboard instantiateViewControllerWithIdentifier:#"PhotosViewController"];
[self.navigationController pushViewController:controller animated:YES];
and if you want to present controller then create manually add a back button like we have default in navigation back button and on it's click write below code to dismiss the controller.
[self dismissViewControllerAnimated:YES];
Hope this helps you.

Presenting login UINavigationController shows black screen

My iOS app requires user authentication, and when the user is not authenticated, the app should displayed a ViewController where the user can login. This screen also gives users the option to create an account, so the whole login screens (and optional registration screens) are setup using a UINavigationController that is displayed programatically.
The login screen is displayed with the following code:
- (void)promptCredentials{
dispatch_async(dispatch_get_main_queue(), ^{
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"Main" bundle:nil];
UINavigationController *loginScreenNav = [storyboard instantiateViewControllerWithIdentifier:#"LoginNavigationController"];
[loginScreenNav setModalPresentationStyle:UIModalPresentationFullScreen];
[self presentModalViewController:loginScreenNav animated:YES];
});
}
However, when this method is called, the app navigates to a black screen . What is the correct way to present the user with a different ViewController programatically? I have found several SO questions related to such black screens, however, none of the answers have worked for me. My app is running on iOS 8.2.
You need to create a UIViewcontroller and set that as the rootController for your UINAvigationController.
presentModalViewController is deprecated as of ios6, try using presentViewController.
UIViewController *vc = [storyboard instantiateViewControllerWithIdentifier:#"LoginNavigationController"];
UINavigationController *loginScreenNav = [[UINavigationController alloc] initWithRootViewController:vc];
[self presentViewController:loginScreenNav animated:YES];

Redirect to particular view controller when getting local notification

I am trying to implement local notification and its working fine but my problem is when my application is in foreground and I got the push from the server it will alert me but when I tap on that alert and trying to redirect on my home view controller with the using this simple code in app-delegate file
- (void)didTapOnNotificationView:(CMNavBarNotificationView *)notificationView
{
UIStoryboard *storyboard = [[self.window rootViewController] storyboard];
HomeViewController *obj_home=[storyboard instantiateViewControllerWithIdentifier:#"Home"];
UINavigationController* navigationViewController_test=[[UINavigationController alloc]initWithRootViewController:[storyboard instantiateViewControllerWithIdentifier:#"Home"]];
[navigationViewController_test pushViewController:obj_home animated:YES];
}
With this code it will show only black screen,can any one tell me where i making the mistake? and yes for the local notification alert I am using third party tool (CMNavBarNotificationView) and using its delegate method (didTapOnNotificationView)
You need to link up the navigationcontroller with a viewcontroller currently in the navigation stack. Here in the below code I am using the rootviewcontroller to present/push the HomeviewController, you can replace it with any controller that you want.
- (void)didTapOnNotificationView:(CMNavBarNotificationView *)notificationView
{
UIStoryboard *storyboard = [[self.window rootViewController] storyboard];
HomeViewController *obj_home=[storyboard instantiateViewControllerWithIdentifier:#"Home"];
// either present the navigation controller as a modal
UINavigationController* navigationViewController_test=[[UINavigationController alloc]initWithRootViewController:obj_home];
[self.window.rootViewController presentViewController:navigationViewController_test animated:YES completion:^{}];
// OR push the home view. Here my rootViewController is a navigation controller, so I am pushing the home view directly from it. You can push it from any of the controller that is in the current navigation controller stack.
[(UINavigationController *)self.window.rootViewController pushViewController:obj_home animated:YES];
}
Hope this helps.

Linking a new viewcontroller to Storyboard?

There is probably a simple solution but I can't figure it out.
I am using storyboards for the interface.
I start with a tab bar controller, but before the user is allowed to use the app the user has to authenticate himself trough a loginview which is modally pushed at the start.
I want to configure the loginview at the same storyboard, but I can't seam to figure out how to link the view controller at the storyboard and my code.
What I have done:
Create a new UIViewController subclass trough file > new > new file.
Drag a new UIViewController in the story board
Set the class in the custom class tab
drags a UILabel for test purpose.
run
No label...
Pull on a new UIViewController that will act as the login view controller onto the MainStoryboard. In the attribute inspector change the identifier to LoginViewController (or something appropriate). Then add
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"MainStoryboard" bundle:nil];
UIViewController *vc = [storyboard instantiateViewControllerWithIdentifier:#"LoginViewController"];
[vc setModalPresentationStyle:UIModalPresentationFullScreen];
[self presentModalViewController:vc animated:YES];
}
to the First view controller and the login screen will be loaded from your storyboard and presented.
Hope this helps.
The answer by Scott Sherwood above is most correct answer I found after lot of searching. Though very slight change as per new SDK (6.1), presentModalViewController shows deprecated.
Here is very small change to above answer.
UIStoryboard *sb = [UIStoryboard storyboardWithName:#"Storyboard" bundle:nil];
HomeViewController * hvc = [sb instantiateViewControllerWithIdentifier:#"LoginView"];
[hvc setModalPresentationStyle:UIModalPresentationFullScreen];
[self presentViewController:hvc animated:YES completion:nil];
I'm new in this field. But if the first view controller is a navigation view controller and its rootviewcontroller is a table view controller. If you want to push a view controller like the LoginViewController when you click the cell, and you also want to go back to the table view by using the navigation bar. I recommend this way:
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
UIStoryboard *sb = [UIStoryboard storyboardWithName:#"Main" bundle:nil];
UIViewController *controller = [sb instantiateViewControllerWithIdentifier:#"LoginViewController"];
[self.navigationController pushViewController:controller animated:YES];
}
In this way, you can have the navigation.
By the way, I don't know why this kind of problem you asked will appear. I guess when the loginviewcontroller is created in the code, its view is not the view in the storyboard. If someone know the cause, please tell me! thanks!

Resources