如何使Swift委派在React本机iOS应用程序中工作
我正在尝试制作一个自定义键盘,以替换我的React native应用程序中的本机iOS键盘。然而,当我将Swift委托模式连接到React Native时,我很难让它正常工作。有人知道我能做什么吗 我完全用Swift(以编程方式)制作了一个测试应用程序,以确保自定义键盘工作正常,按钮动作注册正常。但是,当我连接这个工作的Swift键盘以响应本机时,按钮停止工作。当我点击文本输入时,键盘会正常显示(目前,这也是用Swift制作的,但很快就会改变),但按钮根本不会注册(打印语句不起作用) 我还尝试从委托模式切换到只使用回调/函数。这一次,按钮寄存器(print语句工作),但是文本输入永远不会更新,我用来保存按钮值的任何变量都会重置 KeyboardViewManager.swift-在测试应用程序中,这是viewcontroller如何使Swift委派在React本机iOS应用程序中工作,swift,react-native,native,react-native-ios,Swift,React Native,Native,React Native Ios,我正在尝试制作一个自定义键盘,以替换我的React native应用程序中的本机iOS键盘。然而,当我将Swift委托模式连接到React Native时,我很难让它正常工作。有人知道我能做什么吗 我完全用Swift(以编程方式)制作了一个测试应用程序,以确保自定义键盘工作正常,按钮动作注册正常。但是,当我连接这个工作的Swift键盘以响应本机时,按钮停止工作。当我点击文本输入时,键盘会正常显示(目前,这也是用Swift制作的,但很快就会改变),但按钮根本不会注册(打印语句不起作用) 我还尝试从
class KeyboardViewManager: RCTViewManager {
let textField: UITextField = UITextField(frame: CGRect(x: 80, y: 134, width: 255.00, height: 78.00));
override func view() -> UIView! {
// initialize custom keyboard
let keyboardView = Keyboard(frame: CGRect(x: 0, y: 0, width: 375, height: 440))
// the view controller will be notified by the keyboard whenever a key is tapped
keyboardView.delegate = self as? KeyboardDelegate
textField.inputView = keyboardView
return textField
}
func keyWasTapped(character: String) {
if (character == "<") {
textField.deleteBackward()
} else {
textField.insertText(character)
}
}
func closeIOSKeyboard() {
textField.endEditing(true)
}
}
注意:我觉得这个问题可能与使用RCTViewManager而不是viewController来操作UIView有关。不幸的是,我不熟悉Swift和React Native,我不太清楚RCTViewManager是如何工作的
我希望每次单击键盘上的按钮时,文本输入都会更新。相反,当点击按钮时,什么也不会发生。幸运的是,键盘的样式确实正确
编辑:这里是我的React Native应用程序的示例代码(不是我的实际应用程序,但它是我如何在Swift和React Native之间连接的示例)
从“React”导入React,{Fragment};
进口{
看法
要求活动组件,
}从“反应本机”;
const CustomKeyboard=requirenActiveComponent(“键盘视图”);
进口{
标题,
学习重新链接,
颜色,
调试说明,
重新加载指令,
}来自“react native/Libraries/NewAppScreen”;
常量应用=()=>{
返回(
);
为了澄清,我没有包含桥接文件的代码,但我已经将本机模块连接到我的React-native应用程序,并且可以使键盘显示。通过委派模式创建的按钮不起作用。React-native中使用的代码在哪里?我只是用一个示例编辑了说明!我无法理解是确切的应用程序代码,但这是我使用它的方式。为了澄清,我没有包括桥接文件的代码,但我已经将本机模块连接到我的React本机应用程序,并且可以显示键盘。通过委派模式创建的按钮不起作用。您在React本机中使用的代码在哪里?我刚刚编辑过我无法分享确切的应用程序代码,但我就是这样使用它的。
protocol KeyboardDelegate: class {
func keyWasTapped(character: String)
func closeIOSKeyboard()
}
class Keyboard: UIView {
weak var delegate: KeyboardDelegate?
// MARK:- keyboard initialization
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
initializeSubviews()
}
override init(frame: CGRect) {
super.init(frame: frame)
initializeSubviews()
}
func makeButtonWithText(text:String, x:Int, y:Int, width:Int, height:Int, fontSize:CGFloat = 24, bgColor:UIColor = UIColor.clear) -> UIButton {
let myButton = UIButton(type: UIButton.ButtonType.system)
myButton.frame = CGRect(x: x, y: y, width: width, height: height)
myButton.setTitle(text, for: UIControl.State.normal)
myButton.setTitleColor(UIColor.white, for: UIControl.State.normal)
myButton.titleLabel?.font = .systemFont(ofSize: fontSize)
myButton.backgroundColor = bgColor
myButton.addTarget(self,
action: #selector(keyTapped),
for: .allEvents
)
return myButton
}
func initializeSubviews() {
let view = UIView()
view.backgroundColor = UIColor(red: 42/255, green: 41/255, blue: 39/255, alpha: 1)
view.addSubview(makeButtonWithText(text: "1",x: 40,y: 40,width: 85,height: 37))
// similar code for other buttons here
view.addSubview(makeButtonWithText(text: "OK",x: 0,y: 340,width: 375,height: 50,
fontSize: 15, bgColor: UIColor(red: 15/255, green: 154/255, blue: 142/255, alpha: 1)
))
self.addSubview(view)
view.frame = self.bounds
}
@objc
@IBAction func keyTapped(sender: UIButton) {
KeyboardViewManager().keyTapped(sender: sender, character: sender.titleLabel!.text!)
// When a button is tapped, send that information to the
// delegate (ie, the view controller)
self.delegate?.keyWasTapped(character: sender.titleLabel!.text!)
}
@objc
@IBAction func closeKeyboard(sender: UIButton) {
// When a ok is tapped, close the keyboard
self.delegate?.closeIOSKeyboard()
}
}
import React, {Fragment} from 'react';
import {
View,
requireNativeComponent,
} from 'react-native';
const CustomKeyboard = requireNativeComponent("KeyboardView");
import {
Header,
LearnMoreLinks,
Colors,
DebugInstructions,
ReloadInstructions,
} from 'react-native/Libraries/NewAppScreen';
const App = () => {
return (
<View style={{flex: 1, alignItems: "stretch"}}>
<CustomKeyboard style={{flex: 1, alignItems: "center", justifyContent: "center", position: 'relative', top: -100}}/>
</View>
);