React Native-通过AppDelegate方法显示ViewController

时间:2018-07-20 17:39:02

标签: ios objective-c react-native

AppDelegate.m
这可以正常工作(当应用进入后台时显示启动画面)

- (void)applicationDidEnterBackground:(UIApplication *)application
{
  UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"NativeScreens" bundle:nil];
  SplashVC *SplashViewController=[storyboard instantiateViewControllerWithIdentifier:@"SplashVC"];
  [self.window.rootViewController presentViewController:SplashViewController animated:NO completion:NULL];
}

但是现在我需要从react-native调用它。
我创建了具有相同功能的方法:

RCT_EXPORT_METHOD(showCustomNativeSplashScreen)
{
  UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"NativeScreens" bundle:nil];
  SplashVC *SplashViewController=[storyboard instantiateViewControllerWithIdentifier:@"SplashVC"];
  [self.window.rootViewController presentViewController:SplashViewController animated:NO completion:NULL];
}

它在RN中可用(我从console.log看到它),但是当我调用它时-什么也没发生。

import { NativeModules } from 'react-native';
...
console.log(NativeModules.AppDelegate); // Object{showCustomNativeSplashScreen: function}
NativeModules.AppDelegate.showCustomNativeSplashScreen(); // nothing :(

我在做什么错了?

1 个答案:

答案 0 :(得分:0)

有必要找到上级控制器source

<!DOCTYPE HTML>
<html>
<head>
    <title></title>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <link rel="stylesheet" href="https://unpkg.com/leaflet@1.3.1/dist/leaflet.css"
    integrity="sha512-Rksm5RenBEKSKFjgI3a41vrjkw4EVPlJ3+OiI65vTjIdo9brlAacEuKOiQ5OFh7cOI1bkDwLqdLw3Zg0cRJAAQ=="
    crossorigin=""/>
     <!-- Make sure you put this AFTER Leaflet's CSS -->
   <script src="https://unpkg.com/leaflet@1.3.1/dist/leaflet.js"
   integrity="sha512-/Nsx9X4HebavoBvEBuyp3I7od5tA0UzAxs+j83KgC8PU0kgB4XiK4Lfe4y4cgBtaRJQEIFCW+oC506aPT2L1zw=="
   crossorigin=""></script>
</head>
<body>
<div id="map" style="width: 1366px; height: 720px;"></div>
<script>


var userLocation = new L.LatLng(42.237, -71.96);
    var map = L.map('map').setView(userLocation, 6);

    L.tileLayer('http://tiles.mapc.org/basemap/{z}/{x}/{y}.png',
    {
        attribution: 'Tiles by <a href="http://mapc.org">MAPC</a>, Data by <a href="http://mass.gov/mgis">MassGIS</a>',
        maxZoom: 17,
        minZoom: 9
    }).addTo(map);

    var greenIcon = L.icon({
        iconUrl: 'icon/icon.png',
        iconSize: [64, 64]
    });

    //random locations around the Commonwealth
    var items = [{
        //Georgetown
        lat: "42.703",
        lon: "-70.98"
    }, {
        //Mattapoisett
        lat: "41.6577",
        lon: "-70.807"
    }, {
        //Otis
        lat: "42.1915",
        lon: "-73.08"
    }];

    drawData();

    //draw all the data on the map
    function drawData() {
        var item, o;
        //draw markers for all items
        for (item in items) {
            o = items[item];
            var loc = new L.LatLng(o.lat, o.lon);
            createPolyLine(loc, userLocation);
        }
    }

    //draw polyline
    function createPolyLine(loc1, loc2) {

        var latlongs = [loc1, loc2];
        var polyline = new L.Polyline(latlongs, {
            color: 'green',
            opacity: 1,
            weight: 1,
            clickable: false
        }).addTo(map);

        //distance
        var s = 'About ' + (loc1.distanceTo(loc2) / 1000).toFixed(0) + 'km away from you.</p>';

        var marker = L.marker(loc1).addTo(map);
        if (marker) {
            marker.bindPopup(s);
        }
    }
</script>
</html>