我是 Xcode/Swift 编程的新手,我遇到了一个小问题。
我想使用委托(delegate)将信息从一个 ViewController 发送到第二个 ViewController,而不使用 segue。我阅读了很多相关内容,发现最常见的解决方案是在 ViewDidLoad 中使用“instance”.delegate = self,但它对我不起作用。
-- 应用的定义--
这很容易。在第一个 ViewController 上,我有一个按钮和一个标签。该按钮打开第二个 ViewController,它有一个 textField 和一个 Button。 Button 将 textField 中的内容发送到第一个 ViewController 以更新 Label。
-- 代码--
这是第一个 ViewController 的代码:
class ViewController: UIViewController, clickedButtonDelegate {
@IBOutlet weak var Label: UILabel!
func didClickedButton(text: String) {
Label.text = text
}
var secondView = SecondViewController()
override func viewDidLoad() {
super.viewDidLoad()
secondView.delegate = self
}
}
这是第二个 ViewController 的代码:
protocol clickedButtonDelegate {
func didClickedButton(text: String)
}
class SecondViewController: UIViewController {
@IBOutlet weak var introducedText: UITextField!
var delegate : clickedButtonDelegate!
@IBAction func sendData(_ sender: Any) {
if delegate != nil {
let information:String = introducedText.text!
delegate!.didClickedButton(text: information)
dismiss(animated: true, completion: nil)
self.navigationController?.popViewController(animated: true)
}
}
}
使用此代码没有任何反应,因为在 SecondViewController 中委托(delegate)始终为 nil。
你能帮忙吗?
提前致谢!
最佳答案
其实我原来的代码比较复杂。我试图通过一个更简单的练习来“减少问题”,以便更好地理解如何在使用和不使用 Segues 的情况下传递信息。
我正在将两个 ViewController 与一个 Segue 连接起来(见图)。
到目前为止,我发现了两种在这里有效的信息传递方式:
Segue:通过使用 Segue 连接两个 ViewController 并使用 prepare(for segue),如下例所示,然后使用协议(protocol)函数:
//This piece of code goes inside the first ViewController
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "showSecondView" {
let secondView: SecondViewController = segue.destination as! SecondViewController
secondView.delegate = self
}
}
实例化:通过创建第一个 ViewController 的新实例并使用我需要的来自第二个 View Controller 的信息,如示例代码所示:
//This piece of code goes inside the second ViewController
@IBAction func sendData(_ sender: Any) {
let text_to_pass = introducedText.text
let firstVC = storyboard?.instantiateViewController(withIdentifier: "GetData") as! ViewController
self.present(firstVC, animated: true)
firstVC.Label.text = text_to_pass
}
这两个选项在这里都可以,但我想了解为什么上面的代码不起作用。我读了很多文章,大多数人都在 ViewDidLoad 中使用 secondView.delegate = self 并像魅力一样工作。
我做错了什么?
关于ios - 在没有 segue 的 2x ViewController 之间传递信息时,Delegate 始终为 nil,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47735120/
大约一年前,我决定确保每个包含非唯一文本的Flash通知都将从模块中的方法中获取文本。我这样做的最初原因是为了避免一遍又一遍地输入相同的字符串。如果我想更改措辞,我可以在一个地方轻松完成,而且一遍又一遍地重复同一件事而出现拼写错误的可能性也会降低。我最终得到的是这样的:moduleMessagesdefformat_error_messages(errors)errors.map{|attribute,message|"Error:#{attribute.to_s.titleize}#{message}."}enddeferror_message_could_not_find(obje
我主要使用Ruby来执行此操作,但到目前为止我的攻击计划如下:使用gemsrdf、rdf-rdfa和rdf-microdata或mida来解析给定任何URI的数据。我认为最好映射到像schema.org这样的统一模式,例如使用这个yaml文件,它试图描述数据词汇表和opengraph到schema.org之间的转换:#SchemaXtoschema.orgconversion#data-vocabularyDV:name:namestreet-address:streetAddressregion:addressRegionlocality:addressLocalityphoto:i
我好像记得Lua有类似Ruby的method_missing的东西。还是我记错了? 最佳答案 表的metatable的__index和__newindex可以用于与Ruby的method_missing相同的效果。 关于ruby-难道Lua没有和Ruby的method_missing相媲美的东西吗?,我们在StackOverflow上找到一个类似的问题: https://stackoverflow.com/questions/7732154/
我构建了两个需要相互通信和发送文件的Rails应用程序。例如,一个Rails应用程序会发送请求以查看其他应用程序数据库中的表。然后另一个应用程序将呈现该表的json并将其发回。我还希望一个应用程序将存储在其公共(public)目录中的文本文件发送到另一个应用程序的公共(public)目录。我从来没有做过这样的事情,所以我什至不知道从哪里开始。任何帮助,将不胜感激。谢谢! 最佳答案 无论Rails是什么,几乎所有Web应用程序都有您的要求,大多数现代Web应用程序都需要相互通信。但是有一个小小的理解需要你坚持下去,网站不应直接访问彼此
我有一个奇怪的问题:我在rvm上安装了rubyonrails。一切正常,我可以创建项目。但是在我输入“railsnew”时重新启动后,我有“程序'rails'当前未安装。”。SystemUbuntu12.04ruby-v"1.9.3p194"gemlistactionmailer(3.2.5)actionpack(3.2.5)activemodel(3.2.5)activerecord(3.2.5)activeresource(3.2.5)activesupport(3.2.5)arel(3.0.2)builder(3.0.0)bundler(1.1.4)coffee-rails(
我想在一个没有Sass引擎的类中使用Sass颜色函数。我已经在项目中使用了sassgem,所以我认为搭载会像以下一样简单:classRectangleincludeSass::Script::FunctionsdefcolorSass::Script::Color.new([0x82,0x39,0x06])enddefrender#hamlengineexecutedwithcontextofself#sothatwithintemlateicouldcall#%stop{offset:'0%',stop:{color:lighten(color)}}endend更新:参见上面的#re
如果您尝试在Ruby中的nil对象上调用方法,则会出现NoMethodError异常并显示消息:"undefinedmethod‘...’fornil:NilClass"然而,有一个tryRails中的方法,如果它被发送到一个nil对象,它只返回nil:require'rubygems'require'active_support/all'nil.try(:nonexisting_method)#noNoMethodErrorexceptionanymore那么try如何在内部工作以防止该异常? 最佳答案 像Ruby中的所有其他对象
似乎无法为此找到有效的答案。我正在阅读Rails教程的第10章第10.1.2节,但似乎无法使邮件程序预览正常工作。我发现处理错误的所有答案都与教程的不同部分相关,我假设我犯的错误正盯着我的脸。我已经完成并将教程中的代码复制/粘贴到相关文件中,但到目前为止,我还看不出我输入的内容与教程中的内容有什么区别。到目前为止,建议是在函数定义中添加或删除参数user,但这并没有解决问题。触发错误的url是http://localhost:3000/rails/mailers/user_mailer/account_activation.http://localhost:3000/rails/mai
在Cooper的书BeginningRuby中,第166页有一个我无法重现的示例。classSongincludeComparableattr_accessor:lengthdef(other)@lengthother.lengthenddefinitialize(song_name,length)@song_name=song_name@length=lengthendenda=Song.new('Rockaroundtheclock',143)b=Song.new('BohemianRhapsody',544)c=Song.new('MinuteWaltz',60)a.betwee
这里有一个很好的答案解释了如何在Ruby中下载文件而不将其加载到内存中:https://stackoverflow.com/a/29743394/4852737require'open-uri'download=open('http://example.com/image.png')IO.copy_stream(download,'~/image.png')我如何验证下载文件的IO.copy_stream调用是否真的成功——这意味着下载的文件与我打算下载的文件完全相同,而不是下载一半的损坏文件?documentation说IO.copy_stream返回它复制的字节数,但是当我还没有下