我正在尝试使用 setter 和 getter 函数将变量从 subview (inputPopUp.swift) 传递到父 View 。这是我的 subview 中的代码:
var char: String!
var buttonPressed = String()
@IBAction func addButtonPressed(sender: AnyObject) {
buttonPressed = "addButton"
setChar("+")
getChar()
self.removeAnimate()
}
@IBAction func minusButtonPressed(sender: AnyObject) {
buttonPressed = "minusButton"
setChar("-")
self.removeAnimate()
}
@IBAction func divideButtonPressed(sender: AnyObject) {
buttonPressed = "divideButton"
setChar("/")
self.removeAnimate()
}
@IBAction func multiplyButtonPressed(sender: AnyObject) {
buttonPressed = "multiplyButton"
setChar("*")
self.removeAnimate()
}
//setter method
func setChar(var thisChar: String){
char = thisChar
}
//getter method
func getChar()-> String{
if (buttonPressed == "addButton"){
char = "+"
}
if (buttonPressed == "minusButton"){
char = "-"
}
if (buttonPressed == "divideButton"){
char = "/"
}
if (buttonPressed == "multiplyButton"){
char = "*"
}
return char
}
我正尝试像这样访问父 View 中的“char”变量,但它返回 nil - 大概是因为我正在调用该函数的新实例。
@IBAction func runButtonPressed(sender: AnyObject) {
inputPopUp().getChar()
}
我还想根据 subview 中的按钮点击更新父 View 中的标签。我正在尝试实现键值观察。这是我到目前为止所得到的。
class ObserveChar: NSObject {
dynamic var char = String()
func updateChar(){
char = String()
}
private var myContext = 0
class Observer: NSObject {
var objectToObserve = ObserveChar()
override init(){
super.init()
objectToObserve.addObserver(self, forKeyPath: "char", options: .New, context: &myContext)
}
override func observeValueForKeyPath(keyPath: String, ofObject object: AnyObject, change: [NSObject : AnyObject], context: UnsafeMutablePointer<Void>) {
if context == &myContext {
println("Char updated: \(change[NSKeyValueChangeNewKey])")
} else {
super.observeValueForKeyPath(keyPath, ofObject: object, change: change, context: context)
}
}
deinit {
objectToObserve.removeObserver(self, forKeyPath: "char", context: &myContext)
}
最佳答案
看来你的项目是关于计算器的。你不应该使用
ParentView().tapLabel.text = getChar()
因为,你已经多次创建了parentView,你应该将parentView设置为它的weak属性
也可以使用KVO观察 subview 的char变量;也可以使用NSNotification更新父 View 的tapLabel;或者也可以使用delegate。 代表喜欢下面
protocol UpdateTapLabel {
func didTapSomeOperateAction(operateInfo : String)
class ParentClass : NSObject, UpdateTapLabel {
var tapLabel : UILabel!
var childView : ChildView!
func didTapSomeOperateAction(operateInfo: String) {
self.tapLabel.text = operateInfo
}
//you should initialize the child view like this
/*
childView = ChildView()
childView.delegate = self
*/
class ChildView: NSObject {
var char: String!
var delegate : UpdateTapLabel
//getter method
func getChar() {
if (buttonPressed == "addButton"){
char = "+"
}
if (buttonPressed == "minusButton"){
char = "-"
}
if (buttonPressed == "divideButton"){
char = "/"
}
if (buttonPressed == "multiplyButton"){
char = "*"
}
self.delegate.didTapSomeOperateAction(char)
}
KVO 喜欢下面
//you should add observer like this
//self.addObserver(self, forKeyPath: "childView.char", options: NSKeyValueObservingOptions.New, context: nil)
//also you should remove it in deinit, otherwise you will get crash when release parentView
deinit {
self.removeObserver(self, forKeyPath: "childView.char")
}
//you can handle the kvo here
override func observeValueForKeyPath(keyPath: String, ofObject object: AnyObject, change: [NSObject : AnyObject], context: UnsafeMutablePointer<Void>) {
if keyPath == "childView.char" {
self.tapLabel.text = change[NSKeyValueChangeNewKey]
}
}
关于swift - 在 Swift 中将变量从 subview 传递到父 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32097578/
我的目标是转换表单输入,例如“100兆字节”或“1GB”,并将其转换为我可以存储在数据库中的文件大小(以千字节为单位)。目前,我有这个:defquota_convert@regex=/([0-9]+)(.*)s/@sizes=%w{kilobytemegabytegigabyte}m=self.quota.match(@regex)if@sizes.include?m[2]eval("self.quota=#{m[1]}.#{m[2]}")endend这有效,但前提是输入是倍数(“gigabytes”,而不是“gigabyte”)并且由于使用了eval看起来疯狂不安全。所以,功能正常,
我需要从一个View访问多个模型。以前,我的links_controller仅用于提供以不同方式排序的链接资源。现在我想包括一个部分(我假设)显示按分数排序的顶级用户(@users=User.all.sort_by(&:score))我知道我可以将此代码插入每个链接操作并从View访问它,但这似乎不是“ruby方式”,我将需要在不久的将来访问更多模型。这可能会变得很脏,是否有针对这种情况的任何技术?注意事项:我认为我的应用程序正朝着单一格式和动态页面内容的方向发展,本质上是一个典型的网络应用程序。我知道before_filter但考虑到我希望应用程序进入的方向,这似乎很麻烦。最终从任何
我想要做的是有2个不同的Controller,client和test_client。客户端Controller已经构建,我想创建一个test_clientController,我可以使用它来玩弄客户端的UI并根据需要进行调整。我主要是想绕过我在客户端中内置的验证及其对加载数据的管理Controller的依赖。所以我希望test_clientController加载示例数据集,然后呈现客户端Controller的索引View,以便我可以调整客户端UI。就是这样。我在test_clients索引方法中试过这个:classTestClientdefindexrender:template=>
我正在查看instance_variable_set的文档并看到给出的示例代码是这样做的:obj.instance_variable_set(:@instnc_var,"valuefortheinstancevariable")然后允许您在类的任何实例方法中以@instnc_var的形式访问该变量。我想知道为什么在@instnc_var之前需要一个冒号:。冒号有什么作用? 最佳答案 我的第一直觉是告诉你不要使用instance_variable_set除非你真的知道你用它做什么。它本质上是一种元编程工具或绕过实例变量可见性的黑客攻击
我正在编写一个gem,我必须在其中fork两个启动两个webrick服务器的进程。我想通过基类的类方法启动这个服务器,因为应该只有这两个服务器在运行,而不是多个。在运行时,我想调用这两个服务器上的一些方法来更改变量。我的问题是,我无法通过基类的类方法访问fork的实例变量。此外,我不能在我的基类中使用线程,因为在幕后我正在使用另一个不是线程安全的库。所以我必须将每个服务器派生到它自己的进程。我用类变量试过了,比如@@server。但是当我试图通过基类访问这个变量时,它是nil。我读到在Ruby中不可能在分支之间共享类变量,对吗?那么,还有其他解决办法吗?我考虑过使用单例,但我不确定这是
我是一个Rails初学者,但我想从我的RailsView(html.haml文件)中查看Ruby变量的内容。我试图在ruby中打印出变量(认为它会在终端中出现),但没有得到任何结果。有什么建议吗?我知道Rails调试器,但更喜欢使用inspect来打印我的变量。 最佳答案 您可以在View中使用puts方法将信息输出到服务器控制台。您应该能够在View中的任何位置使用Haml执行以下操作:-puts@my_variable.inspect 关于ruby-on-rails-如何在我的R
我是rails的新手,想在form字段上应用验证。myviewsnew.html.erb.....模拟.rbclassSimulation{:in=>1..25,:message=>'Therowmustbebetween1and25'}end模拟Controller.rbclassSimulationsController我想检查模型类中row字段的整数范围,如果不在范围内则返回错误信息。我可以检查上面代码的范围,但无法返回错误消息提前致谢 最佳答案 关键是您使用的是模型表单,一种显示ActiveRecord模型实例属性的表单。c
我没有找到太多关于如何执行此操作的信息,尽管有很多关于如何使用像这样的redirect_to将参数传递给重定向的建议:action=>'something',:controller=>'something'在我的应用程序中,我在路由文件中有以下内容match'profile'=>'User#show'我的表演Action是这样的defshow@user=User.find(params[:user])@title=@user.first_nameend重定向发生在同一个用户Controller中,就像这样defregister@title="Registration"@user=Use
我正在使用RubyonRails3.0.9,我想生成一个传递一些自定义参数的link_toURL。也就是说,有一个articles_path(www.my_web_site_name.com/articles)我想生成如下内容:link_to'Samplelinktitle',...#HereIshouldimplementthecode#=>'http://www.my_web_site_name.com/articles?param1=value1¶m2=value2&...我如何编写link_to语句“alàRubyonRailsWay”以实现该目的?如果我想通过传递一些
我收到格式为的回复#我需要将其转换为哈希值(针对活跃商家)。目前我正在遍历变量并执行此操作:response.instance_variables.eachdo|r|my_hash.merge!(r.to_s.delete("@").intern=>response.instance_eval(r.to_s.delete("@")))end这有效,它将生成{:first="charlie",:last=>"kelly"},但它似乎有点hacky和不稳定。有更好的方法吗?编辑:我刚刚意识到我可以使用instance_variable_get作为该等式的第二部分,但这仍然是主要问题。