Flutter 如何通过颤振方法通道在颤振中使用导航控制器调用swift view控制器

Flutter 如何通过颤振方法通道在颤振中使用导航控制器调用swift view控制器,flutter,flutter-dependencies,Flutter,Flutter Dependencies,我试图通过在AppDelegate.swift文件中定义的名为“callNavigator()”的私有函数调用在名为“MainNavigationController.swift”的单独文件中定义的UINavigationController我正在通过一个方法通道从颤振端调用该方法,该通道成功地发回一条消息,即某些通信正在发生“颤振/省道端没有问题”。我希望该功能使用“MainNavigationController.swift”中定义的导航器在颤振屏幕顶部推送视图。我的MainNavigati

我试图通过在AppDelegate.swift文件中定义的名为“callNavigator()”的私有函数调用在名为“MainNavigationController.swift”的单独文件中定义的UINavigationController我正在通过一个方法通道从颤振端调用该方法,该通道成功地发回一条消息,即某些通信正在发生“颤振/省道端没有问题”。我希望该功能使用“MainNavigationController.swift”中定义的导航器在颤振屏幕顶部推送视图。我的MainNavigationController.swift中的代码如下:

import Foundation
    import UIKit

    class MainNavigationController: UINavigationController{
        override func viewDidLoad() {
            super.viewDidLoad()
        }
        private func navigateToMainInterface() {
            let mainStoryboard = UIStoryboard(name: "Main", bundle: Bundle.main)
           guard let mainNavigationVc = mainStoryboard.instantiateViewController(withIdentifier:"MainNavigationController") as? MainNavigationController else {
                return

            }
            present(mainNavigationVc, animated: true, completion: nil)

        }
    }
Then I am trying to call this class in my "AppDelegate.swift file" through my "callNavigator()" private function as a constructor. The code is as follows in the Appdelegate.swift file :


    @UIApplicationMain
    @objc class AppDelegate: FlutterAppDelegate {
      override func application(
        _ application: UIApplication,
        didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?

      ) -> Bool {
        let controller: FlutterViewController = window?.rootViewController as! FlutterViewController
        GeneratedPluginRegistrant.register(with: self)      

        let navigatorChannel = FlutterMethodChannel(name: "com.fluttertonative/navigateToCameraSDK", binaryMessenger:  controller.binaryMessenger)
       navigatorChannel.setMethodCallHandler ({
          [weak self] (call: FlutterMethodCall, result: FlutterResult) -> Void in
          // Note: this method is invoked on the UI thread.
          guard call.method == "getSwiftNavigator" else {
            result(FlutterMethodNotImplemented)
            return
          }
          self?.callNavigator()

       })
        GeneratedPluginRegistrant.register(with: self)
        return super.application(application, didFinishLaunchingWithOptions: launchOptions)
      }
        private func receiveGreeting(result: FlutterResult){
            result("Hello from Swift Code...")
        }
        private func callNavigator(){
            MainNavigationController()

        }
    }
class MyHomePage extends StatefulWidget {
  MyHomePage({Key key, this.title}) : super(key: key);
  final String title;

  @override
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  static const navigateToCameraSDK = const MethodChannel('com.fluttertonative/navigateToCameraSDK');

  String _greeting = 'awaiting swift call..';


  Future _getnavigator() async{
    try {
      final result = navigateToCameraSDK.invokeMapMethod('getSwiftNavigator');
      print(result);
    } on PlatformException catch (e){
      print('error:$e');
      _greeting = 'error occured:$e';
    }
    setState(() {
      _greeting ='call successful you have got the swift Navigator';
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: AppBar(
          title: Text(widget.title),
        ),
        body: Container(
          child: Column(
            mainAxisAlignment: MainAxisAlignment.center,
            crossAxisAlignment: CrossAxisAlignment.center,
            children: <Widget>[
              Text(_greeting),
              CupertinoButton(
                child: Text('call swift to get the navigator'),
                onPressed: _getnavigator,
                borderRadius:BorderRadius.all(Radius.circular(13.0)),
                color: Colors.blue[300],
              ),
            ],
          ),
        ));
  }
}
但是,我在Xcode中收到一条警告,“callNavigator”函数试图调用“MainNavigationController”时没有使用其结果,而是说“UINavigationController”初始值设定项的结果未使用”。我是斯威夫特的新手,不知道该怎么办

我已将初始导航器设置为Main.storyBoard中的FlatterViewController,并在其中放置了一些虚拟文本字段。当我从颤振端调用方法通道时,我希望运行“callNavigator”函数,该函数依次调用“MainNavigationController.swift”文件中的“MainNavigationController”,该文件应在颤振屏幕的视图顶部推送一个新视图

我需要一些帮助来实现这一点,因为我是Swift语言的新手

那么我的dart代码如下:

import Foundation
    import UIKit

    class MainNavigationController: UINavigationController{
        override func viewDidLoad() {
            super.viewDidLoad()
        }
        private func navigateToMainInterface() {
            let mainStoryboard = UIStoryboard(name: "Main", bundle: Bundle.main)
           guard let mainNavigationVc = mainStoryboard.instantiateViewController(withIdentifier:"MainNavigationController") as? MainNavigationController else {
                return

            }
            present(mainNavigationVc, animated: true, completion: nil)

        }
    }
Then I am trying to call this class in my "AppDelegate.swift file" through my "callNavigator()" private function as a constructor. The code is as follows in the Appdelegate.swift file :


    @UIApplicationMain
    @objc class AppDelegate: FlutterAppDelegate {
      override func application(
        _ application: UIApplication,
        didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?

      ) -> Bool {
        let controller: FlutterViewController = window?.rootViewController as! FlutterViewController
        GeneratedPluginRegistrant.register(with: self)      

        let navigatorChannel = FlutterMethodChannel(name: "com.fluttertonative/navigateToCameraSDK", binaryMessenger:  controller.binaryMessenger)
       navigatorChannel.setMethodCallHandler ({
          [weak self] (call: FlutterMethodCall, result: FlutterResult) -> Void in
          // Note: this method is invoked on the UI thread.
          guard call.method == "getSwiftNavigator" else {
            result(FlutterMethodNotImplemented)
            return
          }
          self?.callNavigator()

       })
        GeneratedPluginRegistrant.register(with: self)
        return super.application(application, didFinishLaunchingWithOptions: launchOptions)
      }
        private func receiveGreeting(result: FlutterResult){
            result("Hello from Swift Code...")
        }
        private func callNavigator(){
            MainNavigationController()

        }
    }
class MyHomePage extends StatefulWidget {
  MyHomePage({Key key, this.title}) : super(key: key);
  final String title;

  @override
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  static const navigateToCameraSDK = const MethodChannel('com.fluttertonative/navigateToCameraSDK');

  String _greeting = 'awaiting swift call..';


  Future _getnavigator() async{
    try {
      final result = navigateToCameraSDK.invokeMapMethod('getSwiftNavigator');
      print(result);
    } on PlatformException catch (e){
      print('error:$e');
      _greeting = 'error occured:$e';
    }
    setState(() {
      _greeting ='call successful you have got the swift Navigator';
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: AppBar(
          title: Text(widget.title),
        ),
        body: Container(
          child: Column(
            mainAxisAlignment: MainAxisAlignment.center,
            crossAxisAlignment: CrossAxisAlignment.center,
            children: <Widget>[
              Text(_greeting),
              CupertinoButton(
                child: Text('call swift to get the navigator'),
                onPressed: _getnavigator,
                borderRadius:BorderRadius.all(Radius.circular(13.0)),
                color: Colors.blue[300],
              ),
            ],
          ),
        ));
  }
}
类MyHomePage扩展StatefulWidget{
MyHomePage({Key,this.title}):超级(Key:Key);
最后的字符串标题;
@凌驾
_MyHomePageState createState()=>\u MyHomePageState();
}
类_MyHomePageState扩展状态{
静态常量navigateToCameraSDK=常量MethodChannel('com.fluttonative/navigateToCameraSDK');
字符串_greeting='等待快速呼叫..';
Future\u getnavigator()异步{
试一试{
最终结果=navigateToCameraSDK.invokeMapMethod('getSwiftNavigator');
打印(结果);
}平台上异常捕获(e){
打印('error:$e');
_问候语='发生错误:$e';
}
设置状态(){
_问候语:“‘呼叫成功,您已获得swift Navigator’;
});
}
@凌驾
小部件构建(构建上下文){
返回脚手架(
appBar:appBar(
标题:文本(widget.title),
),
主体:容器(
子:列(
mainAxisAlignment:mainAxisAlignment.center,
crossAxisAlignment:crossAxisAlignment.center,
儿童:[
文字(_问候语),
丘比特纽扣(
child:Text('调用swift获取导航器'),
onPressed:\u getnavigator,
borderRadius:borderRadius.all(半径.圆形(13.0)),
颜色:颜色。蓝色[300],
),
],
),
));
}
}
我的故事板如下:颤振视图控制器是初始视图控制器。在此处输入图像描述:

您好,您找到解决方案了吗?我也在尝试做类似的事情,但因为我是新手,所以遇到了麻烦swift@karens我遵循了这个例子,并对它进行了一点重新调整。非常感谢!我要一个look@K.chim我正在尝试向iOS模块添加新的Swift文件,并从AppDelegate访问它,但它无法看到它。我必须在AppDelegate文件中添加所有代码吗?嗨,找到解决方案了吗?我也在尝试做类似的事情,但因为我是新手,所以遇到了麻烦swift@karens我遵循了这个例子,并对它进行了一点重新调整。非常感谢!我要一个look@K.chim我正在尝试向iOS模块添加新的Swift文件,并从AppDelegate访问它,但它无法看到它。我必须在AppDelegate文件中添加所有代码吗?