iOS混合开发调用flutter_module用swift5语言怎么写哒?
来源:11-5 Flutter iOS混合开发实战-集成与调用【iOS技术与Flutter融合】
小葱与奥特曼
2019-05-16
如题,劳烦老师看看?oc我是小白。
AppDelegate.swift
import UIKit
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
return true
}
func applicationWillResignActive(_ application: UIApplication) {
// Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
// Use this method to pause ongoing tasks, disable timers, and invalidate graphics rendering callbacks. Games should use this method to pause the game.
}
func applicationDidEnterBackground(_ application: UIApplication) {
// Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
// If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}
func applicationWillEnterForeground(_ application: UIApplication) {
// Called as part of the transition from the background to the active state; here you can undo many of the changes made on entering the background.
}
func applicationDidBecomeActive(_ application: UIApplication) {
// Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}
func applicationWillTerminate(_ application: UIApplication) {
// Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}
}
ViewController.swift
import UIKit
class ViewController: UIViewController {
@IBOutlet weak var testBtn: UIButton!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
@IBAction func clickTest(_ sender: UIButton) {
print("clickTest~~~~")
}
}
写回答
1回答
-
小葱与奥特曼
提问者
2019-05-17
跟着官方github,有点奇怪,有问题。麻烦解答一下?
https://github.com/flutter/flutter/wiki/Add-Flutter-to-existing-apps
import UIKit import Flutter import FlutterPluginRegistrant // Only if you have Flutter Plugins. @UIApplicationMain class AppDelegate: FlutterAppDelegate { var flutterEngine : FlutterEngine?; // Only if you have Flutter plugins. override func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool { self.flutterEngine = FlutterEngine(name: "io.flutter", project: nil); self.flutterEngine?.run(withEntrypoint: nil); GeneratedPluginRegistrant.register(with: self.flutterEngine); return super.application(application, didFinishLaunchingWithOptions: launchOptions); } }
import UIKit import Flutter class ViewController: UIViewController { @IBOutlet weak var testBtn: UIButton! override func viewDidLoad() { super.viewDidLoad() let button = UIButton(type:UIButton.ButtonType.custom) button.addTarget(self, action: #selector(handleButtonAction), for: .touchUpInside) button.setTitle("Press me", for: UIControl.State.normal) button.frame = CGRect(x: 80.0, y: 210.0, width: 160.0, height: 40.0) button.backgroundColor = UIColor.blue self.view.addSubview(button) } @objc func handleButtonAction() { let flutterEngine = (UIApplication.shared.delegate as? AppDelegate)?.flutterEngine; let flutterViewController = FlutterViewController(engine: flutterEngine, nibName: nil, bundle: nil)!; flutterViewController.setInitialRoute("route1") self.present(flutterViewController, animated: false, completion: nil) } @IBAction func clickTest(_ sender: UIButton) { print("clickTest~~~~") } }
点击press me,出来的似乎不是自己的代码。是一个默认的界面
而我的代码是用Android时的flutter_module...
import 'dart:ui'; import 'package:flutter/material.dart'; void main() => runApp(MyApp(initParams: window.defaultRouteName,)); class MyApp extends StatelessWidget { final String initParams; const MyApp({Key key, this.initParams}) : super(key: key); // This widget is the root of your application. @override Widget build(BuildContext context) { return MaterialApp( title: 'Flutter 混合开发', theme: ThemeData( primarySwatch: Colors.blue, ), home: MyHomePage(title: '22Flutter 混合开发哦', initParams: initParams,), ); } } class MyHomePage extends StatefulWidget { MyHomePage({Key key, this.title, this.initParams}) : super(key: key); final String title; final String initParams; @override _MyHomePageState createState() => _MyHomePageState(); } class _MyHomePageState extends State<MyHomePage> { int _counter = 0; void _incrementCounter() { setState(() { _counter++; }); } @override Widget build(BuildContext context) { return Scaffold( appBar: AppBar( title: Text(widget.title), ), body: Center( child: Column( mainAxisAlignment: MainAxisAlignment.center, children: <Widget>[ Text( 'initParams:${widget.initParams}', style: TextStyle(color: Colors.red,fontSize: 20), ), Text( '$_counter', style: Theme.of(context).textTheme.display1, ), ], ), ), floatingActionButton: FloatingActionButton( onPressed: _incrementCounter, tooltip: 'Increment', child: Icon(Icons.add), ), // This trailing comma makes auto-formatting nicer for build methods. ); } }
042019-05-19
相似问题