Eas*_* PI 7 uiviewcontroller ios flutter
我想在我们的 Flutter 视图上“推送”一个新的原生视图(Android 中的 Activity 和 iOS 中的 UIViewController ),在新的原生视图完成/完成后,让屏幕回到我们的 Flutter 视图。
我可以在 Android 中做到这一点。但是当我尝试在以下位置执行此操作时,我对 iOS 非常陌生ios/Runner/AppDelegate.m:
SwiftViewController *swiftViewController = [controller.storyboard instantiateViewControllerWithIdentifier:@"SwiftViewController"];
[(FlutterViewController *)self.window.rootViewController pushViewController:swiftViewController animated:YES];
Run Code Online (Sandbox Code Playgroud)
它给出了错误:
“FlutterViewController”没有可见的@interface 声明了选择器“pushViewController:animated”
那么如何在iOS中做到这一点呢?谢谢
小智 6
在 Swift 中,我们可以使用方法通道从 flutter 操作事件打开本机 iOS viewController,
var platform = const MethodChannel('com.nativeActivity/iosChannel');
final String result = await platform.invokeMethod('StartNativeIOS');
Run Code Online (Sandbox Code Playgroud)
let controller : FlutterViewController = window?.rootViewController as! FlutterViewController
let methodChannel = FlutterMethodChannel(name: "com.nativeActivity/iosChannel", binaryMessenger:controller.binaryMessenger)
methodChannel.setMethodCallHandler({
(call: FlutterMethodCall, result: @escaping FlutterResult) -> Void in
if call.method == "StartNativeIOS" {
let mainVC = MainViewController() // Your viewController
let navigationController = UINavigationController(rootViewController: mainVC)
self.window.rootViewController = navigationController
self.window.makeKeyAndVisible()
} else {
result(FlutterMethodNotImplemented)
return
}
})
Run Code Online (Sandbox Code Playgroud)
它会起作用的!干杯
注意:但是无法返回到 flutter 视图。
所以这是正确的做法。 使用协调器模式进行 Flutter 导航
您需要以编程方式将 FlutterViewController 嵌入到容器UINavigationController中或故事板中,然后您就可以推送下一个控制器。
以下是如何以编程方式嵌入的示例:
@interface AppDelegate()
@property (nonatomic, strong) UINavigationController *navigationController;
@end
@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
[GeneratedPluginRegistrant registerWithRegistry:self];
UIViewController *flutterViewController = [[FlutterViewController alloc] init];
self.navigationController = [[UINavigationController alloc] initWithRootViewController:flutterViewController];
[self.navigationController setNavigationBarHidden:YES];
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
self.window.rootViewController = self.navigationController;
[self.window makeKeyAndVisible];
return true;
}
- (void)pushExample {
UIViewController *viewController = [[UIViewController alloc] init];
[self.navigationController pushViewController:viewController animated:true];
}
@end
Run Code Online (Sandbox Code Playgroud)
当需要时(例如点击按钮)调用pushExample. 您也可以在该视频中查看“故事板”方式
| 归档时间: |
|
| 查看次数: |
8763 次 |
| 最近记录: |