最新消息:20210816 当前crifan.com域名已被污染,为防止失联,请关注(页面右下角的)公众号

【已解决】iOS的项目中swift文件中如何获得Storyboard中的UITextField控件

iOS crifan 3606浏览 0评论

折腾:

【已解决】用飞语FYRtcEngineKit去实现基本的iOS间的语音通话

期间,需要在iOS项目中的swift代码中获取在Storyboard中创建的UITextField控件,

由于获得其引用,获得或设置其text

其中,想要获得对应storyboard中的元素,比如输入文本框

ios storyboard 如何获取 uitextfield

ios get uitextfield in storyboard

ios – Input from UITextField connected from storyboard to Swift view controller file – Stack Overflow

结果,control+drag对应的控件到

ViewController.swift

中,却拖不过去:

ios – Get UITextField from UIView using an Identifier/Tag (SWIFT) – Stack Overflow

试试用tag

结果无法保存:tag不能为字符串,只能是数字

然后又出现:

【已解决】iOS带Storyboard的项目出错:Fatal error init(coder:) has not been implemented

【总结】

然后可以通过self.view.viewWithTag去获得对应的控件。

对应代码:

class ViewController: UIViewController, FYRtcEngineKitDelegate{
    let SelfTextFieldTag = 1
    let OtherTextFieldTag = 2
    let SpeakerButtonTag = 3
    let CallButtonTag = 4
    let MuteButtonTag = 5
    
    var selfTextField:UITextField!
    var otherTextField:UITextField!
    var speakerButton:UIButton!
    var callButton:UIButton!
    var muteButton:UIButton!
    
    var isCalling = false
    
    override init(nibName nibNameOrNil: String?, bundle nibBundleOrNil: Bundle?) {
        super.init(nibName: nibNameOrNil, bundle: nibBundleOrNil)
    }
    
    required init?(coder aDecoder: NSCoder) {
        //fatalError(“init(coder:) has not been implemented”)
        super.init(coder: aDecoder)
    }
    
    var fyRtcEngine: FYRtcEngineKit!
    override func viewDidLoad() {
        super.viewDidLoad()
        self.selfTextField = self.view.viewWithTag(SelfTextFieldTag) as! UITextField
        self.otherTextField = self.view.viewWithTag(OtherTextFieldTag) as! UITextField
        self.speakerButton = self.view.viewWithTag(SpeakerButtonTag) as! UIButton
        self.callButton = self.view.viewWithTag(CallButtonTag) as! UIButton
        self.muteButton = self.view.viewWithTag(MuteButtonTag) as! UIButton
        
//        fyRtcEngine = FYRtcEngineKit.sharedEngine(withAppId:”your appid”, appToken:”your apptoken”, delegate: nil)
        fyRtcEngine = FYRtcEngineKit.sharedEngine(withAppId: FY_APPID, appToken: FY_APPTOKEN, delegate: self)
//        fyRtcEngine.joinChannel(“channelId123”, uid: nil, optionData: nil, joinSuccess: nil)
    }
            let otherUid = self.otherTextField.text!
            let selfUid = self.selfTextField.text!
            fyRtcEngine.dialPeer(otherUid, callerUid: selfUid, optionData: dialOption)

Xcode中的Storyboard中的tag设置:

即可去代码中操作对应的UITextField控件了。

转载请注明:在路上 » 【已解决】iOS的项目中swift文件中如何获得Storyboard中的UITextField控件

发表我的评论
取消评论

表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
83 queries in 0.171 seconds, using 22.09MB memory