按下React-Native View组件按钮上的原生iOS导航

时间:2022-09-18 18:59:13

I have a simple React-Native componentReactNativeMasterView with a button I want to use for native navigation.

我有一个简单的React-Native componentReactNativeMasterView,带有一个我想用于本机导航的按钮。

in ReactNativeMasterView:

在ReactNativeMasterView中:

var MasterViewController = NativeModules.MasterViewController

then the button calls:

然后按钮调用:

MasterViewController.buttonEvent()

I have a basic Obj-C view controller called MasterViewController

我有一个名为MasterViewController的基本Obj-C视图控制器

In MasterViewController.h:

在MasterViewController.h中:

@interface MasterViewController : UIViewController <RCTBridgeModule>

And in MasterViewController.m I have a reference to the react native view in my storyboard:

在MasterViewController.m中,我在storyboard中引用了react本机视图:

@property (weak, nonatomic) IBOutlet RNMasterView *reactViewWrapper;

and native module and method look like this:

和本机模块和方法如下所示:

RCT_EXPORT_MODULE();

RCT_EXPORT_METHOD(buttonEvent) {

    RCTLogInfo(@"Button Pressed");

    UIViewController *detail = [[UIViewController alloc]init];

    UIViewController *rootViewController = [UIApplication sharedApplication].delegate.window.rootViewController;
    dispatch_async(dispatch_get_main_queue(), ^{
        [rootViewController showViewController:detail sender:self];
    });

}

It almost works too...

它几乎也有效......

Sadly the transition takes me to a black screen, and when i inspect the view hierarchy it looks like UITransitionView is just presenting an empty View I can actually navigate back from this state too.

可悲的是,转换将我带到黑屏,当我检查视图层次结构时,看起来UITransitionView只是呈现一个空视图我实际上也可以从这个状态导航回来。

Anyone have any ideas how to make this work?

任何人有任何想法如何使这项工作?

UPDATE:

更新:

I can make it work with a UIAlertController:

我可以使用UIAlertController:

UIAlertController * alert = [UIAlertController
                             alertControllerWithTitle:name
                             message:param
                             preferredStyle:UIAlertControllerStyleAlert];

UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleCancel handler:^(UIAlertAction * action) {
    NSLog(@"Cancel");
}];

[alert addAction:cancelAction];


UIViewController *rootViewController = [UIApplication sharedApplication].delegate.window.rootViewController;
dispatch_async(dispatch_get_main_queue(), ^{
    [rootViewController presentViewController:alert animated: YES completion: nil];
});

that appears to work as intended.

似乎按预期工作。

1 个解决方案

#1


1  

It's black because you're initializing a viewController programmatically and push it UIViewController *detail = [[UIViewController alloc]init];.

它是黑色的,因为你以编程方式初始化一个viewController并将其推送到UIViewController * detail = [[UIViewController alloc] init] ;.

Try this:

尝试这个:

UIViewController *detail = [[UIViewController alloc] init];
detail.view.backgroundColor = [UIColor whiteColor];

EDIT

编辑

You shouldn't really be initializing view controllers like that unless you need to. You should be using storyboards for creating view controllers and laying out the views and then presenting them onto a UINavigationViewController. Here is a good tutorial.

除非你需要,否则你不应该像这样初始化视图控制器。您应该使用故事板来创建视图控制器并布置视图,然后将它们呈现到UINavigationViewController上。这是一个很好的教程。

#1


1  

It's black because you're initializing a viewController programmatically and push it UIViewController *detail = [[UIViewController alloc]init];.

它是黑色的,因为你以编程方式初始化一个viewController并将其推送到UIViewController * detail = [[UIViewController alloc] init] ;.

Try this:

尝试这个:

UIViewController *detail = [[UIViewController alloc] init];
detail.view.backgroundColor = [UIColor whiteColor];

EDIT

编辑

You shouldn't really be initializing view controllers like that unless you need to. You should be using storyboards for creating view controllers and laying out the views and then presenting them onto a UINavigationViewController. Here is a good tutorial.

除非你需要,否则你不应该像这样初始化视图控制器。您应该使用故事板来创建视图控制器并布置视图,然后将它们呈现到UINavigationViewController上。这是一个很好的教程。