RubyMotion: 'Name Error' - rubymotion

Have an error as follows:
Terminating app due to uncaught exception 'NameError', reason: 'weather_controller.rb:3:in `viewDidLoad': uninitialized constant WeatherController::Name (NameError)
AppDelegate:
class AppDelegate
def application(application, didFinishLaunchingWithOptions:launchOptions)
puts "Hello! You just launched: #{App.name}, \n location: (#{App.documents_path})"
#window = UIWindow.alloc.initWithFrame(UIScreen.mainScreen.bounds)
#window.backgroundColor = UIColor.lightGrayColor
#window.rootViewController = MyController.alloc.init
#window.makeKeyAndVisible
true
end
end
my_controller.rb:
class MyController < UIViewController
def viewDidLoad
#name_label = setup_label [[10, 10], [300, 50]], UIColor.orangeColor, Name
#place_label = setup_label [[10, 80], [300, 50]], UIColor.yellowColor, Place
#temp_label = setup_label [[10, 150], [300, 50]], UIColor.greenColor, Temperature
end
def setup_label frame, bgcolor, text
label = UILabel.alloc.initWithFrame(frame)
label.textColor = UIColor.darkGrayColor
label.backgroundColor = bgcolor
label.textAlignment = UITextAlignmentCenter
label.text = text.to_s
view.addSubview label
label
end
end
Any ideas? Thanks in advance

In your setup_label method, you're accepting the following arguments, frame, bgcolor and text where your text argument suppose to be a String object.
Therefore, your viewDidLoad method should be the following
def viewDidLoad
#name_label = setup_label [[10, 10], [300, 50]], UIColor.orangeColor, "Name"
#place_label = setup_label [[10, 80], [300, 50]], UIColor.yellowColor, "Place"
#temp_label = setup_label [[10, 150], [300, 50]], UIColor.greenColor, "Temperature"
end

Related

replacing a new interface with old one in tkinter

I've created a class that has a function called mainScreen(). It simply prints the main screen with two buttons on it. If you press any button, it must go to another function called signup(). I want to clear the whole frame and create new widgets but I can't clear the widgets
class graphics:
def __init__(self, master):
self.root = master
def mainscreen(self):
helv36 = tkFont.Font(family='Century Gothic', size=20)
mainFrame = Frame(self.root)
mainFrame.config(relief='sunken', width=1280, height=720, bg='light
blue')
mainFrame.pack(expand='yes', fill='both')
inButton = Button(mainFrame, text = "Sign up", bd = 10, relief =
GROOVE, font = helv36)
inButton.bind("<Button-1>", self.signup)
inButton.place(bordermode = OUTSIDE, width =160, height = 60, x =
600, y = 300)
upButton = Button(mainFrame, text = "Sign in", bd = 10, relief =
GROOVE, font = helv36)
upButton.bind("<Button-1>", self.signup)
upButton.place(bordermode = OUTSIDE, width =160, height = 60, x =
600, y = 400)
mainFrame.pack_propagate(FALSE)
self.root.mainloop()
def signup(self,event):
signUpShow = Frame(self.root)
signUpShow.config(relief='sunken', width=1280, height=720, bg='light
yellow')
signUpShow.pack(expand='yes', fill='both')
You __init__ needs to have its code indented and it needs a call to mainscreen. The solution to mainFrame being local within mainscreen is to make it also an attribute.
self.mainframe = mainFrame = Frame(self.root)
Then you can access self.mainframe within signup.

RubyMotion and UIPickerView

I want to use UIPickerView to select a number and assign the selected number to a label. I worked out how to do it using the ib gem and using interface builder to create the initial interface and it works fine. However, I would like to do it purely using RubyMotion code and I can't for the life of me get it to work. The best I have managed is for the label to return True and not a number.
I'm using the following standard code for the picker view delegate methods:
def pickerView(pickerView, numberOfRowsInComponent:component)
101
end
def pickerView(pickerView, titleForRow:row, forComponent:component)
row.to_s
end
def numberOfComponentsInPickerView (pickerView)
1
end
def pickerView(pickerView, didSelectRow:row, inComponent:component)
end
def pickerView(pickerView, titleForRow:row, forComponent:component)
" #{row+1}"
end
def submit
totals.addTotals(myPicker.selectedRowInComponent(0))
end
and then the label text is populated like this:
numLabel = UILabel.new
numLabel.text = "Number Selected: #{submit}"
numLabel.font = UIFont.boldSystemFontOfSize(18)
numLabel.frame = [[20,320],[260,340]]
numLabel.numberOfLines = 2
numLabel.adjustsFontSizeToFitWidth = 'YES'
self.view.addSubview numLabel
The totals is a shared client.
Here is how to do it in RubyMotion alone. Note that the label and picker are set up in viewDidLoad. The label gets updated in pickerView:didSelectRow:inComponent:
app_delegate.rb
class AppDelegate
def application(application, didFinishLaunchingWithOptions:launchOptions)
#window = UIWindow.alloc.initWithFrame(UIScreen.mainScreen.bounds)
#window.rootViewController = PickerDemo.new
#window.makeKeyAndVisible
true
end
end
picker_demo.rb
class PickerDemo < UIViewController
def viewDidLoad
view.backgroundColor = UIColor.whiteColor
#numLabel = UILabel.new
#numLabel.text = "Number Selected: 0"
#numLabel.font = UIFont.boldSystemFontOfSize(18)
#numLabel.frame = [[20,100],[260,120]]
#numLabel.numberOfLines = 2
#numLabel.adjustsFontSizeToFitWidth = true
view.addSubview(#numLabel)
#picker = UIPickerView.new
#picker.frame = [[0, 183], [320, 162]]
#picker.delegate = self
#picker.dataSource = self
view.addSubview(#picker)
end
def pickerView(pickerView, numberOfRowsInComponent:component)
101
end
def pickerView(pickerView, titleForRow:row, forComponent:component)
row.to_s
end
def numberOfComponentsInPickerView(pickerView)
1
end
def pickerView(pickerView, didSelectRow:row, inComponent:component)
#numLabel.text = "Number Selected: #{row}"
end
end

Promotion/bubble-wrap TableScreen data

I'm stuck on this:
I need to populate data into my app.
I'm using Promotion for the very first time....
Without ProMotion I use to fetch the data in the init method
Now my code looks like below:
class Parties < ProMotion::TableScreen
attr_accessor :_cells
#news = []
include MyUiModules
title 'Yazarlar'
refreshable callback: :on_refresh,
pull_message: "Pull to refresh",
refreshing: "Refreshing data…",
updated_format: "Last updated at %s",
updated_time_format: "%l:%M %p"
def on_refresh
#MyItems.pull_from_server do |items|
##my_items = items
end_refreshing
#update_table_data
# end
end
def table_data
_cells = []
[{
title: nil,
cells: create_cells(_cells)
}]
end
def will_appear
Barbutton.create_bar(self)
set_attributes self.view, {
backgroundColor: hex_color("DBDBDB")
}
end
def go_to_next
App.delegate.slide_menu.show_menu
end
def create_cells(_cells)
BW::HTTP.get(URL) do |response|
json = BW::JSON.parse response.body.to_str
for line in json
_cells << { title: line["val_for_title"]}
end
end
_cells
end
end
Unfotunately this does return an empty array, and I can't figure out how to solve it.
Thx for your help
You can't do that because BW::HTTP.get is asynchronous !
Instead try something like this:
def on_init
#data = []
end
def table_data
[
{
title: nil,
cells: #data
}
]
end
def on_refresh
BW::HTTP.get(URL) do |response|
#data = []
json = BW::JSON.parse(response.body.to_str)
json.each do |hash|
#data << { title: hash["val_for_title"]}
end
update_table_data
end_refreshing
end
end
Hope it helps :-)

How to use .on_delete callback in ProMotion-Formotion

I use ProMotion with PM::FormotionScreen screen.
How to use row.on_delete callback from Formotion in ProMotion?
I have this table_data method
def table_data
{
sections: [{
rows: [
{
title: "URL",
key: :url,
placeholder: "http://myapp/dj_mon/",
action: :delete_account,
deletable: true,
type: :string,
auto_correction: :no,
auto_capitalization: :none
}
]
}]
}
end
screenshot: http://i.stack.imgur.com/e1dlu.png
Instead of using a hash to initialize the Formotion form, you'll have to use the DSL:
form = Formotion::Form.new
form.build_section do |section|
section.build_row do |row|
row.title = 'URL'
row.key = :url
row.placeholder = "http://myapp/dj_mon/"
row.type = :string
row.auto_correction = :no
row.auto_capitalization = :none
row.deletable = true
row.on_tap do |row|
p "I'm tapped!"
end
row.on_delete do |row|
p "I'm called before the delete animation"
end
end
end

Manually rendering django template with date filter

I've got the template to render manually, but not the dates with a filter. Is there a way of doing this without formatting the dates before I put them in the context?
Here is the code:
t= This is to notify you that you are booked on the course {{title}} that starts on {{start_date|date:"F j, Y"}} at {{start|time}} and is being held at {{venue}}.
c = {'startdate': datetime.datetime(2010, 12, 1, 9, 0), 'enddate': datetime.datetime(2010, 12, 1, 12, 0), 'has_cpd': False, 'title': 'Course 1', 'closes': None, 'creator': 1L, 'venue': 'Venue for Course 1', 'summary': 'Course 1 Summary', 'tags': '', 'attachment': <FieldFile: None>, 'organiser': 3L, 'id': 1L, 'opens': datetime.datetime(2010, 11, 30, 20, 1, 50, 951490), 'venue_map': None}
t.render(c)
This gives the output:
This is to notify you that you are booked on the course Course 1 that starts on at and is being held at Venue for Course 1.
Why don't the dates show up?
Looks like a simple typo between start_date vs. startdate.
In [18]: t= Template("""This is to notify you that you are booked on the course {{title}} that starts on {{startdate|date:"F j, Y"}} at {{start|time}} and is being held at {{venue}}.""")
In [19]: t.render(c)Out[19]: u'This is to notify you that you are booked on the course Course 1 that starts on December 1, 2010 at and is being held at Venue for Course 1.'