I have just started learning iOS development with RubyMotion and I'm doing this tutorial. It seems pretty easy to understand, but right now I'm stuck with this error I get when I run the rake command:
ColorController.rb:7:in `initWithColor:': undefined method `initWithNibName' for #<ColorController:0x9b4f470> (NoMethodError)
from SearchController.rb:46:in `open_color:'
from Color.rb:35:in `block in find:'
from query.rb:358:in `call_delegator_with_response'
from query.rb:128:in `connectionDidFinishLoading:'
I got to the part where WHEW WELL THAT IS A TON OF CODE NOW ISN'T IT. is found on the tutorial page and I'm stuck there. Currently my code looks like:
# app_delegate.rb
class AppDelegate
def application(application, didFinishLaunchingWithOptions:launchOptions)
#window = UIWindow.alloc.initWithFrame(UIScreen.mainScreen.bounds)
#search_controller = SearchController.alloc.initWithNibName(nil, bundle: nil)
#navigation_controller = UINavigationController.alloc.initWithRootViewController(#search_controller)
#window.rootViewController = #navigation_controller
#window.makeKeyAndVisible
true
end
end
# ColorController.rb
class ColorController < UIViewController
attr_accessor :color
def initWithColor(color)
initWithNibName(nil, bunlde: nil)
self.color = color
self
end
def viewDidLoad
super
self.title = self.color.hex
#info_container = UIView.alloc.initWithFrame [[0, 0], [self.view.frame.size.width, 110]]
#info_container.backgroundColor = UIColor.lightGrayColor
self.view.addSubview #info_container
#color_view = UIView.alloc.initWithFrame [[10, 10], [90, 90]]
#color_view.backgroundColor = String.new(self.color.hex).to_color
self.view.addSubview #color_view
#color_label = UILabel.alloc.initWithFrame [[110, 30], [0, 0]]
#color_label.text = self.color.hex
#color_label.sizeToFit
self.view.addSubview #color_label
#text_field = UITextField.alloc.initWithFrame [[110, 60], [100, 26]]
#text_field.placeholder = "tag"
#text_field.textAlignment = UITextAlignmentLeft
#text_field.autocapitalizationType = UITextAutocapitalizationTypeNone
#text_field.borderStyle = UITextBorderStyleRoundedRect
self.view.addSubview #text_field
#add_button = UIButton.buttonWithType(UIButtonTypeRoundedRect)
#add_button.setTitle("Add", forState: UIControlStateNormal)
#add_button.setTitle("Adding", forState: UIControlStateDisabled)
#add_button.setTitleColor(UIColor.lightGrayColor, forState: UIControlStateDisabled)
#add_button.sizeToFit
#add_button.frame = [[#text_field.frame.origin.x + #text_field.frame.size.width + 10, #text_field.frame.origin.y], #add_button.frame.size]
self.view.addSubview(#add_button)
table_frame = [[0, #info_container.frame.size.height], [self.view.bounds.size.width, self.view.bounds.size.height - #info_container.frame.size.height - self.navigationController.navigationBar.frame.size.height]]
#table_view = UITableView.alloc.initWithFrame(table_frame, style: UITableViewStylePlain)
self.view.addSubview(#table_view)
end
end
# SearchController.rb
class SearchController < UIViewController
def viewDidLoad
super
self.title = "Search"
self.view.backgroundColor = UIColor.whiteColor
#text_field = UITextField.alloc.initWithFrame [[10, 10], [self.view.frame.size.width - 20, 30]]
#text_field.placeholder = "#abcabc"
#text_field.textAlignment = UITextAlignmentLeft
#text_field.autocapitalizationType = UITextAutocapitalizationTypeNone
#text_field.borderStyle = UITextBorderStyleRoundedRect
#text_field.center = CGPointMake(self.view.frame.size.width / 2, self.view.frame.size.height / 2 - 100)
self.view.addSubview #text_field
#search_button = UIButton.buttonWithType(UIButtonTypeRoundedRect)
#search_button.setTitle("Search", forState: UIControlStateNormal)
#search_button.setTitle("Loading", forState: UIControlStateDisabled)
#search_button.sizeToFit
#search_button.center = CGPointMake(self.view.frame.size.width / 2, #text_field.center.y + 40)
self.view.addSubview #search_button
#search_button.when(UIControlEventTouchUpInside) do
#search_button.enabled = false
#text_field.enabled = false
hex = #text_field.text
hex = hex[1..-1] if hex[0] == "#"
Color.find(hex) do |color|
if color.nil?
#search_button.setTitle("None :(", forState: UIControlStateNormal)
else
#search_button.setTitle("Search", forState: UIControlStateNormal)
self.open_color(color)
end
#search_button.enabled = true
#text_field.enabled = true
end
end
end
def open_color(color)
self.navigationController.pushViewController(ColorController.alloc.initWithColor(color), animated: true)
end
end
I also have another two models which I'm sure they are not relevant unless you're going to run the code.
Can anyone tell me what am I supposed to do ? I realise that I'm trying to call the initWithNibName method which is not defined on my class, but that is what the tutorial does so I assumed it's correct. Do I need to define it on the ColorController class ? Or can I call it in some other way ?
You just spelled bundle incorrectly.
change:
initWithNibName(nil, bunlde: nil)
to:
initWithNibName(nil, bundle: nil)
Related
local OrionLib = loadstring(game:HttpGet(('https://raw.githubusercontent.com/shlexware/Orion/main/source')))()
local Window = OrionLib:MakeWindow({Name = "BoxHub", HidePremium = false, SaveConfig = true, ConfigFolder = "OrionTest"})
---Values
getgenv().AutoFarm = true
--tabs
local MainTab = Window:MakeTab({
Name = "Main",
Icon = "rbxassetid://4483345998",
PremiumOnly = false
})
--toggels
MainTab:AddToggle({
Name = "AutoFarm",
Default = false,
Callback = function(Value)
wait(1)
game.Players.LocalPlayer.Character.HumanoidRootPart.CFrame = CFrame.new(-118.933174, 3.50129104, 63.7992935, -0.99859041, -2.3044068e-08, -0.0530771464, -2.18308767e-08, 1, -2.34369075e-08, 0.0530771464, -2.22451497e-08, -0.99859041)
wait(1)
game:GetService("ReplicatedStorage").Strength_Exercises.Squat1:FireServer()
wait(17)
game.Players.LocalPlayer.Character.HumanoidRootPart.CFrame = CFrame.new(-144.613525, 3.50129104, -52.2876129, -0.0295129567, -3.152`48556e-08, 0.999564409, -2.02664463e-08, 1, 3.09402104e-08, -0.999564409, -1.93444816e-08, -0.0295129567)`
wait(1)
game:GetService("ReplicatedStorage").Strength_Exercises.Speed_Bag2:FireServer()
wait(3)
end
})
i tried doing a function like this
function getgenv().AutoFarm
AutoFarm() = Value
and it just made the toggle button not work`
I'm new to the concept of "faking" OOP in lua with metatables, indexes and OOP in general.
I'm trying to create an instance from an Object in LUA which use variables in the table, so that every instance have its own values. The Fighter model seams pretty common, so I'm trying to explain :-)
FIGHTER = {
["id"] = "Fighter",
["params"] = {
["route"] =
{
["points"] =
{
[1] =
{
["x"] = wp01x,
["y"] = wp01y,
["speed"] = fighterSpeed,
["task"] =
{
["id"] = "ComboTask",
},
},
},
},
},
}
function FIGHTER:new(t)
t = t or {}
setmetatable(t,self)
self.__index = self
return t
end
local wp01x = 0.38746345345
local wp01y = 1.39876268723
local fighterSpeed = 123
local test = FIGHTER:new({FIGHTER})
When the table is created, x, y, speed are nil (so the entries missing on construction).
Passing the values in the Constructor with something like
self.["Mission"].["params"].["route"].["points"].[1].["speed"] = 99
don't work either.
What would be best practice to create such instances? Use some preset values and clone the table later?
I correct your version a little bit and hope it opens your eyes...
FIGHTER = {
["id"] = "Fighter",
["params"] = {
["route"] =
{
["points"] =
{
[1] =
{
["x"] = 0,
["y"] = 0,
["speed"] = 0,
["task"] =
{
["id"] = "ComboTask",
},
},
},
},
},
}
function FIGHTER:new(t, wpx, wpy, speed)
t = t or {id = "Fighter", params = {route = {points = {[1] = {x = wpx, y = wpy, fighterSpeed = speed, task = {id = "ComboTask"}}}}}}
setmetatable(t,self)
self.__index = self
return t
end
local wp01x = 0.38746345345
local wp01y = 1.39876268723
local fighterSpeed = 123
-- The t = t or >>{...}<< case
test = FIGHTER:new(_, wp01x, wp01y, fighterSpeed)
-- The t = >>t<< case
test = FIGHTER:new({id = "Fighter", params = {route = {points = {[1] = {x = wp01x, y = wp01y, fighterSpeed = fighterSpeed, task = {id = "ComboTask"}}}}}})
I'm using the seed-fu gem and am able to get 2 files to seed the db successfully. The following file, while it produces output in the terminal, isn't populating the db:
# residents.rb
Resident.seed do |r|
r.account_id = 1
r.employee_id = 1
r.first_name = "solomon"
r.last_name = "gibsom"
r.gender = "Male"
r.dob = Date.parse("1937-02-20")
r.case_number = "H-3827-JKZ-0329"
r.veteran_status_number = "G-15 classified"
r.marital_status = "Married"
r.arrival_date = Date.today
r.religious_preferences = "None"
r.insurance_info = "Medicaid"
r.burial_provisions = "Cremation"
r.admission_weight = "121 lbs"
r.admission_height = "5ft 9in"
r.allergies = "Corn, wheat and peanut allergies"
end
Address.seed do |r|
r.account_id = 1
r.employee_id = 1
r.resident_id = 1
r.street_address = "55298 solomon hills blvd"
r.city = "Flint"
r.state = "MI"
r.zip = "48289"
end
PrimaryPhone.seed do |r|
r.account_id = 1
r.employee_id = 1
r.resident_id = 1
r.area_code = "810"
r.number = "565-0255"
end
Terminal output:
The output looks good so I check for the records in the console:
I'm sort of at a loss on where further to look to check for errors.
Here are two files that work as expected:
#accounts.rb
Account.seed do |a|
a.facility_name = "Chuck Norris AFC"
end
#admins.rb
Employee.seed do |a|
a.account_id = 1
a.is_admin = true
a.first_name = "kenneth"
a.last_name = "the paige"
a.email = "test#example.com"
a.password = "12345678"
a.password_confirmation = "12345678"
a.dob = Date.parse("1965-05-05")
a.gender = "Male"
end
Address.seed do |address|
address.account_id = 1
address.employee_id = 1
address.street_address = "123 Chuckleburger dr."
address.city = "New York"
address.state = "NY"
address.zip = "00001"
end
some background:
I'm using seed-fu ~>2.3,
residents.rb lives inside of db/fixtures/, and the rails version is 4.2
Thank you in advance for having a look!
Pay attention to the SQL queries printed in the console when you run Resident.all: they have condition WHERE resident.account_id is NULL. But in seeds file Resident has account_id = 1.
You might've set default_scope for Resident model. Try running Resident.unscoped.all or remove the default scope.
When players lose the game the gameFun function is called but for some reason when i switch alert(win) to gameFun and the alert(lose) in processAnswer to alert(win), it doesn't work. My aim is to ask users a question after they won or lost the game.
--ball collides with balloon
function ballCollision(e)
if (e.other.name == 'balloon') then
--ball collision codes
end
if(target.text == '0') then
alert('win')
end
end
function checkGameover()
print( "Check game over" )
for i = 1, cannonBalls.numChildren do
print( i )
if(tonumber(ballRemain.text) <= 0) then
gameFun()
end
end
end
function gameFun()
balloonText.isVisible = false
balloonTextt.isVisible = false
balloonTexttt.isVisible = false
questionText.isVisible = false
askUser = display.newText('Is the cannon hard to use?', display.contentCenterX, display.contentWidth / 4, native.systemFont, 20 )
askUser:setFillColor(135, 75, 44)
yesBtn = display.newImage("Yes.png",120,290)
noBtn = display.newImage("No.png",190,290)
yesBtn:addEventListener ('tap', ansQuestion)
noBtn:addEventListener ('tap', ansQuestion)
end
function ansQuestion(event)
if event.target==noBtn then
answer = answer+0
else
answer = answer+1
end
print('send mail')
sendMail()
askUser.isVisible = false
yesBtn.isVisible = false
noBtn.isVisible = false
yesBtn:removeEventListener ('tap', ansQuestion)
noBtn:removeEventListener ('tap', ansQuestion)
alert('lose')
end
function alert(state)
gameListeners('rmv')
local alert
if(state == 'win') then
alert = display.newImage('win.png')
else
alert = display.newImage('lose.png')
end
askUser.isVisible = false
yesBtn.isVisible = false
noBtn.isVisible = false
print("time from start: ", (system.getTimer()-gameTime))
alert.anchorX = 0.5
alert.anchorX = 0.5
alert.x = display.contentCenterX
alert.y = display.contentCenterY
transition.from(alert, {time = 3000, xScale = 0.3, yScale = 0.3})
local score = display.newText(score.text, 300, -30, native.systemFontBold, 20)
score:setFillColor(135, 75, 44)
end
Also when sendMail function is on, it gives me this
As far as i can tell, it keeps going back and forth between ansQuestion and alert(state). Is there a way to fix this?
When you call alert(lose) in ansQuestion(), Lua interprets lose as a variable, not a string. Since the variable does not exist, it is nil, so the second branch of the if (state == 'win') gets executed. It so happens that in the case of "lose", this is the branch you want, but it is just luck. When you do alert(win), win is also a variable so it is nil, so the same branch gets executed, and that time it is the wrong branch. Use
alert('lose')
and
alert('win')
as you did in ballCollision listener. Note that there are several lines of duplicate code in ansQuestion and alert.
In your alert function you passed the string value 'lose'. That's why it's always calling losing condition.
As per my knowledge, you need to call this function.
local answer = 0
function ansQuestion(event)
if event.target==noBtn then
answer = 0
else
answer = 1
end
sendMail()
askUser.isVisible = false
yesBtn.isVisible = false
noBtn.isVisible = false
yesBtn:removeEventListener ('tap', ansQuestion)
noBtn:removeEventListener ('tap', ansQuestion)
alert(answer)
end
function alert(state)
gameListeners('rmv')
local alert
if(state == 1) then
alert = display.newImage('win.png')
else
alert = display.newImage('lose.png')
end
askUser.isVisible = false
yesBtn.isVisible = false
noBtn.isVisible = false
print("time from start: ", (system.getTimer()-gameTime))
alert.anchorX = 0.5
alert.anchorX = 0.5
alert.x = display.contentCenterX
alert.y = display.contentCenterY
transition.from(alert, {time = 3000, xScale = 0.3, yScale = 0.3})
local score = display.newText(score.text, 300, -30, native.systemFontBold, 20)
score:setFillColor(135, 75, 44)
end
I am struggling at the moment to make a custom Form with Formotion(Also I'm very new in Rubymotion). Maybe I have to try another framework or maybe not doing any framework at all....
I try to make this:
At this moment I created this in Formotion:
The problem is the Border between password and email. It has to start under the text field and not under the icon. It is a small change but I don't know how to do this with Formotion. Are those kind of things easy possible? Thanks in advanced!
Here is my code:
class LoginScreen < PM::FormotionScreen
title ""
def table_data
{
sections: [
{
title: "Login",
rows: [
{
title: "Email",
key: :email,
type: :email,
placeholder: "me#mail.com",
auto_correction: :no,
auto_capitalization: :none,
text_alignment: UITextAlignmentLeft
}, {
title: "Password",
key: :password,
type: :password,
placeholder: "required",
secure: true,
text_alignment: UITextAlignmentLeft
}
]
}
]
}
end
end
I overwritten the EmailRow to insert the icon
motion_require 'string_row'
module Formotion
module RowType
class EmailRow < StringRow
def build_cell(cell)
cell.selectionStyle = self.row.selection_style || UITableViewCellSelectionStyleBlue
field = UITextField.alloc.initWithFrame(CGRectZero)
field.tag = TEXT_FIELD_TAG
observe(self.row, "value") do |old_value, new_value|
break_with_semaphore do
update_text_field(new_value)
end
end
field.clearButtonMode = UITextFieldViewModeWhileEditing
field.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter
field.textAlignment = row.text_alignment || UITextAlignmentRight
field.keyboardType = keyboardType
field.secureTextEntry = true if row.secure?
field.returnKeyType = row.return_key || UIReturnKeyNext
field.autocapitalizationType = row.auto_capitalization if row.auto_capitalization
field.autocorrectionType = row.auto_correction if row.auto_correction
field.clearButtonMode = row.clear_button || UITextFieldViewModeWhileEditing
field.enabled = row.editable?
field.inputAccessoryView = input_accessory_view(row.input_accessory) if row.input_accessory
add_callbacks(field)
cell.swizzle(:layoutSubviews) do
def layoutSubviews
old_layoutSubviews
# viewWithTag is terrible, but I think it's ok to use here...
formotion_field = self.viewWithTag(TEXT_FIELD_TAG)
formotion_field.sizeToFit
field_frame = formotion_field.frame
field_frame.origin.x = self.textLabel.frame.origin.x + Formotion::RowType::Base.field_buffer * 2
field_frame.origin.y = ((self.frame.size.height - field_frame.size.height) / 2.0).round
field_frame.size.width = self.frame.size.width - field_frame.origin.x - Formotion::RowType::Base.field_buffer
formotion_field.frame = field_frame
end
end
if UIDevice.currentDevice.systemVersion >= "6.0"
field.swizzle(:setText) do
def setText(text)
r = old_setText(text)
self.sendActionsForControlEvents(UIControlEventEditingChanged)
r
end
end
end
field.font = BW::Font.new(row.font) if row.font
field.placeholder = row.placeholder
field.text = row_value
icon = UIImage.imageNamed("icons/mail.png")
icon_view = UIImageView.alloc.initWithImage(icon)
icon_view.setFrame(CGRectMake(20, 18, icon_view.frame.size.width, icon_view.frame.size.height))
cell.addSubview(icon_view)
cell.addSubview(field)
cell.textLabel.hidden = true
field
end
end
end
end
This was extremely easy in the end. Just add an image in cell.imageView in EmailRow.
added this line:
cell.imageView.image = UIImage.imageNamed("icons/mail.png")
cell.imageView.setFrame(CGRectMake(20, 18, cell.imageView.frame.size.width, cell.imageView.frame.size.height))
and you access the cell with the code below, for example:
cell = #form.table.visibleCell.first
I'd recommend disabling the built-in border entirely and adding a pseudo-border using a thin UIView. That way you can set the frame to position it in the right position and give it a backgroundColor of #E3E3E5k like in your mockup.