1

I'm fairly new to Ruby/MacRuby. I followed the stopwatch example on the MacRuby website and i used that example to try and make a simple app which takes 6 small text fields and then displays the input in another larger text field. I found a couple (although vague and very horribly documented) examples

class AppDelegate

    attr_accessor :window

    attr_accessor :subjectField, :messageField, :emailTo, :emailFrom, :smtpServerAddress, :smtpPort, :password

    attr_writer :subjectField, :messageField, :emailTo, :emailFrom, :smtpServerAddress, :smtpPort, :password

    def setMessageField(sender)
        @messageField += "SMTP Server Address: #{@smtpServerAddress.to_s}\n")
        @messageField += "SMTP Port: #{@smtpPort.to_s}\n")
        @messageField += "User Email: #{@emailFrom.to_s}\n")
        @messageField += "User Password: #{@password.to_s}\n")
        @messageField += "To Email: #{@emailTo.to_s}\n")
        @messageField += "Subject: #{@subjectField.to_s}\n")
    end

end

Obviously there is something wrong here but can anyone give me a hint in the right direction? I saw this post on here but I didn't understand it very well.

Community
  • 1
  • 1
pattmorter
  • 991
  • 9
  • 21

2 Answers2

0

Assuming that the IBOutlets are connected and all of those outlets are NSTextFields, try this:

class AppDelegate
    attr_accessor :window
    attr_accessor :subjectField, :messageField, :emailTo, :emailFrom, :smtpServerAddress, :smtpPort, :password

    def setMessageField(sender)
        @messageField.stringValue += "SMTP Server Address: #{@smtpServerAddress.stringValue}\n"
        @messageField.stringValue += "SMTP Port: #{@smtpPort.stringValue}\n"
        @messageField.stringValue += "User Email: #{@emailFrom.stringValue}\n"
        @messageField.stringValue += "User Password: #{@password.stringValue}\n"
        @messageField.stringValue += "To Email: #{@emailTo.stringValue}\n"
        @messageField.stringValue += "Subject: #{@subjectField.stringValue}\n"
    end
end
jtomschroeder
  • 1,034
  • 7
  • 11
  • i get an error saying `'setMessageField': undefined method 'stringValue' for nil:NilClass (NoMethodError)` – pattmorter Mar 14 '13 at 23:23
  • Verify that all of the instance variables used in setMessageField: are being assigned somewhere - most likely in Interface Builder. See the "Connect the Text Field Outlet" section of the Stopwatch example. – jtomschroeder Mar 15 '13 at 01:31
  • I followed that and connected everything but it still does not work. – pattmorter Mar 15 '13 at 03:36
  • @jtomscroeder: Here is an [link](https://pbs.twimg.com/media/BFXlX5sCcAALuRA.png) to an image of what my outlets look like. I also changed the `small text field` back to a `text` field incase that effected it but it did not – pattmorter Mar 15 '13 at 03:42
  • The problem here is that setMessageField is being called before the xib is being loading, so the text fields have not been initialized yet. – jtomschroeder Mar 15 '13 at 17:14
0

Figured out what was going on. Within the setMessageField method, the += operator was not valid. So I had to change it to the following. I changed the method name btw.

attr_accessor :window
attr_accessor :subjectField, :messageField, :emailTo, :emailFrom, :smtpServerAddress, :smtpPort, :password

def setText(sender)
    @messageField.stringValue = "SMTP Server Address: #{@smtpServerAddress.stringValue}\n"
    @messageField.stringValue = "#{@messageField.stringValue}SMTP Port: #{@smtpPort.stringValue}\n"
    @messageField.stringValue = "#{@messageField.stringValue}User Email: #{@emailFrom.stringValue}\n"
    @messageField.stringValue = "#{@messageField.stringValue}User Password: #{@password.stringValue}\n"
    @messageField.stringValue = "#{@messageField.stringValue}To Email: #{@emailTo.stringValue}"
    @messageField.stringValue = "#{@messageField.stringValue}Subject: #{@subjectField.stringValue}\n"
end

Works like a charm now.

pattmorter
  • 991
  • 9
  • 21