![](/img/trans.png)
[英]How to call a function half way through the scroll controller in flutter
[英]How to call a swift view controller using the navigation controller in flutter through the Flutter Method Channel
I am trying to call a UINavigationController I have defined in a separate file called "MainNavigationController.swift" through a private function I have defined in my AppDelegate.swift file called "callNavigator()" I am calling that method from the Flutter side through a成功發回消息的方法通道正在發生一些通信“Flutter/Dart 端沒有問題”。 我希望 function 使用“MainNavigationController.swift”中定義的導航器在 flutter 屏幕上推送視圖。我的 MainNavigationController.Z818056DBD7E201241206B9C7CD884 中的代碼如下:
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()
}
}
但是,我在 Xcode 中收到警告,試圖調用“MainNavigationController”的“callNavigator”function 沒有使用其結果,它說“'UINavigationController' 初始化程序的結果未使用”。 我是 swift 的新手,我不知道該怎么做。
我在我的 Main.storyBoard 中將 Initial navigator 設置為 FlutterViewController,我在其中放置了一些虛擬文本文件。 當我從 Flutter 端調用方法通道時,我希望運行“callNavigator” function ,然后調用“MainNavigationController.swift”文件中的“MainNavigationController”,它應該在視圖頂部推送一個新視圖Flutter屏。
我需要一些幫助來實現這一點,因為我是 Swift 語言的新手
那么我的dart代碼如下:
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],
),
],
),
));
}
}
而我的storyboard如下——Flutter視圖Controller是初始視圖Z9BBF373797BF7CF7BA62C800236。 在此處輸入圖像描述:
默認情況下,Flutter iOS 應用程序沒有UINavigationViewController
。 這是將視圖控制器推送到導航堆棧所必需的。
不過,通過AppDelegate
class 添加它非常容易。
import UIKit
import Flutter
@UIApplicationMain
@objc class AppDelegate: FlutterAppDelegate {
var navigationController: UINavigationController!
override func application(_ application: UIApplication,
didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
let controller = window?.rootViewController as! FlutterViewController
GeneratedPluginRegistrant.register(with: self)
// create and then add a new UINavigationController
self.navigationController = UINavigationController(rootViewController: controller)
self.window.rootViewController = self.navigationController
self.navigationController.setNavigationBarHidden(true, animated: false)
self.window.makeKeyAndVisible()
return super.application(application, didFinishLaunchingWithOptions: launchOptions)
}
}
稍后,當您將FlutterMethodChannel添加到您的應用程序委托時,您可以以標准 iOS 方式簡單地推送/顯示新的UIViewController
。
// called from within the FlutterMethodChannel handler
private func pushViewController() {
//get view controller from StoryBoard, XIB or in code
let vc = ...
self.navigationController.setNavigationBarHidden(false, animated: true)
self.navigationController.pushViewController(vc, animated: true)
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.