RubyMotion
I am trying to create a toggle button for each cell in my table. When pressed, it will change the image and when pressed again it will change the image again -- Toggle.
In the UIButton
class I don't see a selected
state.
I'm looking for a way to create a toggle button with UIButton so that I can change the state on each click.
This is how I'm doing it in rubymotion
right now using rmq
@fav_button.on(:touch) do |sender|
puts "pressed fav button for id: " + data[:id] + " and name: " + data[:name]
#how do I change the state here?
end
Source: (StackOverflow)
am on a rubymotion project
have required afmotion in rake file, gem file and bundle installed. I get the above error when I try to run rake pod:install.
Any ideas? Thanks in advance.
Source: (StackOverflow)
How can I achieve paging the navigation bar title in iOS? That is, having the title in the nav bar slide left or right depending on which way you swipe, fade in/out depending on your swipe and get replaced by the next UIPageView
's title.
Twitter's new iOS app does exactly what I am referring to, any thoughts on how this can be achieved is much appreciated. I have attached a link to an image of Twitters app

Source: (StackOverflow)
I just read the author of MagicalRecord's blog post on Why contextForCurrentThread Doesn't work in MagicalRecord.
contextForCurrentThread
is deprecated and saveWithBlock
should be used instead because it creates a safe new NSManagedObjectContext
for the relevant thread.
I've been using contextForCurrentThread
extensively in my app so far. However, I'm having trouble figuring out how to use saveWithBlock
instead as my fetching and saving are not necessarily happening sequentially.
Currently I'm doing things like:
localContext = NSManagedObjectContext.MR_contextForCurrentThread
person = Person.MR_createInContext(localContext)
person.name = "John Smith"
The user may then browse around the app, different controllers, views etc. are displayed. Other objects may be created using a similar method to the code above.
Then at some arbitrary point in the future, when the user decides to save, I run this method:
localContext = NSManagedObjectContext.MR_contextForCurrentThread
localContext.MR_saveToPersistentStoreWithCompletion(
lambda { |success, error|
# ...
}
)
What is the recommended way to create & update objects and then save them without using contextForCurrentThread
?
Source: (StackOverflow)
I'm experimenting with cocosmotion, a Rubymotion implementation of Cocos2d. In the tutorial I'm working through, I have a method that takes a struct as an argument. I consulted the Rubymotion and MacRuby docs, but can't figure out how to instantiate a new object based on the struct I need. I think the main issue is that the struct starts with a lowercase letter and Ruby thinks it's a local variable when I try to work with it.
I believe I have the same problem as stated here:
https://github.com/MacRuby/MacRuby/issues/121
Is there a solution or workaround?
The struct is defined in Objective-C like so:
typedef struct _ccTexParams {
GLuint minFilter;
GLuint magFilter;
GLuint wrapS;
GLuint wrapT;
} ccTexParams;
The method I'm trying to call expects a ccTexParams struct.
Here is what I've tried:
@mysprite = Pointer.new("{ccTexParams=
{GLUint=GL_LINEAR_MIPMAP_LINEAR}
{GLUint=GL_LINEAR}
{GLUint=GL_CLAMP_TO_EDGE}
{GLUint=GL_CLAMP_TO_EDGE}}", 4)
When I try it this way:
@mysprite = Pointer.new(:object, 4)
@mysprite[0] = GL_LINEAR_MIPMAP_LINEAR
@mysprite[1] = GL_LINEAR
@mysprite[2] = GL_CLAMP_TO_EDGE
@mysprite[3] = GL_CLAMP_TO_EDGE
The runtime error is:
expected instance of Pointer of type `{_ccTexParams=IIII}', got `@' (TypeError)
I also tried:
@mysprite = Pointer.new("_ccTexParams", 4)
@mysprite[0] = GL_LINEAR_MIPMAP_LINEAR
@mysprite[1] = GL_LINEAR
@mysprite[2] = GL_CLAMP_TO_EDGE
@mysprite[3] = GL_CLAMP_TO_EDGE
The error:
Can't find pointer description for type `_ccTexParams'
I've also tried to replace it as CcTexParams, _ccTexParams, and just ccTexParams in a bunch of different ways, no versions of it work.
I tried this:
@mysprite = CcTexParams.new
@mysprite.minFilter = GL_LINEAR_MIPMAP_LINEAR
@mysprite.magFilter = GL_LINEAR
@mysprite.wrapS = GL_CLAMP_TO_EDGE
@mysprite.wrapT = GL_CLAMP_TO_EDGE
RubyMotion complains it expected an instance of Pointer, got '#<CcTexParams minFilter=9987 magFilter=9729 wrapS=33071 wrapT=33071>' (CcTexParams) (TypeError)
I tried just passing [GL_LINEAR_MIPMAP_LINEAR, GL_LINEAR, GL_CLAMP_TO_EDGE, GL_CLAMP_TO_EDGE]
directly to the method. It complains that it expected a Pointer but got an Array
.
Perhaps I should just rename ccTexParams
in the lib to something like CCTexParams
? I'm guessing that's not an ideal workaround.
Source: (StackOverflow)
I have a UICollectionView in my app, and each cell is a UIImageView and some text labels. The problem is that when I have the UIImageViews displaying their images, the scrolling performance is terrible. It's nowhere near as smooth as the scrolling experience of a UITableView or even the same UICollectionView without the UIImageView.
I found this question from a few months ago, and it seems like an answer was found, but it's written in RubyMotion, and I don't understand that. I tried to see how to convert it to Xcode, but since I have never used NSCache either, it's a little hard to. The poster there also pointed to here about implementing something in addition to their solution, but I'm not sure where to put that code either. Possibly because I don't understand the code from the first question.
Would someone be able to help translate this into Xcode?
def viewDidLoad
...
@images_cache = NSCache.alloc.init
@image_loading_queue = NSOperationQueue.alloc.init
@image_loading_queue.maxConcurrentOperationCount = 3
...
end
def collectionView(collection_view, cellForItemAtIndexPath: index_path)
cell = collection_view.dequeueReusableCellWithReuseIdentifier(CELL_IDENTIFIER, forIndexPath: index_path)
image_path = @image_paths[index_path.row]
if cached_image = @images_cache.objectForKey(image_path)
cell.image = cached_image
else
@operation = NSBlockOperation.blockOperationWithBlock lambda {
@image = UIImage.imageWithContentsOfFile(image_path)
Dispatch::Queue.main.async do
return unless collectionView.indexPathsForVisibleItems.containsObject(index_path)
@images_cache.setObject(@image, forKey: image_path)
cell = collectionView.cellForItemAtIndexPath(index_path)
cell.image = @image
end
}
@image_loading_queue.addOperation(@operation)
end
end
Here is the code from the second question that the asker of the first question said solved the problem:
UIImage *productImage = [[UIImage alloc] initWithContentsOfFile:path];
CGSize imageSize = productImage.size;
UIGraphicsBeginImageContext(imageSize);
[productImage drawInRect:CGRectMake(0, 0, imageSize.width, imageSize.height)];
productImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
Again, I'm not sure how/where to implement that.
Many thanks.
Source: (StackOverflow)
Ruby Motion just came out, and the screencast doesn't seem to say anything about whether or not you can use Interface Builder to build your interfaces and integrate them in your RubyMotion project. Is such a thing possible? Or do I really have code everything by hand, not to mention maintain to different pieces of code for iPhone/iPad?
My rep isn't high enough to create a rubymotion tag, so please help out if appropriate.
Source: (StackOverflow)
How do I translate the following method call from ObjectiveC to RubyMotion syntax:
[self.faceView addGestureRecognizer:[
[UIPinchGestureRecognizer alloc] initWithTarget:self.faceView
action:@selector(pinch:)]];
I got this far:
self.faceView.addGestureRecognizer(
UIPinchGestureRecognizer.alloc.initWithTarget(
self.faceView, action:???))
I understand the @selector(pinch:)
indicates a delegation to the receiver object pinch
method, but how would I do this in RubyMotion? Maybe using a block?
Source: (StackOverflow)
In Rubymotion we start terminal with rake
Ankits-MacBook-Pro:Magic ankitgupta$ rake
Build ./build/iPhoneSimulator-6.0-Development
Simulate ./build/iPhoneSimulator-6.0-Development/Magic.app
(main)>
By default it is starting iPhoneSimulator-6.0-Development
. How can i start iPhone 5.1
or 5.0 Simulator
?
Source: (StackOverflow)
I'm compiling my app (it's a Rubymotion app) with SSPullToRefresh and keep getting this message:
will be fat and ar(1) will not be able to operate on it
Anybody know what it means and how I should deal with it?
Source: (StackOverflow)
I added an extra method to the String class. I want to use this method later on, but I get a no Method error.
class String
def as_file_full_path
NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, true) [0].stringByAppendingPathComponent(self)
end
end
When I try the following in the REPL it works:
(main)> "image.jpg".as_full_path
=> "/Users/user/Library/Application Support/iPhone Simulator/6.1/Applications/30D186A9-B1C7-4377-AE91-0D14BD3B4A6D/Documents/image.jpg"
But when I call a method on a my model class, it does not work anymore:
(main)> w = Word.first
=> #<Word:0x94d7df0>
(main)> w.filename
=> "2C86A58A-A92A-4A0F-B26C-0F5F583E142C.caf"
(main)> w.filename.class
=> String
(main)> w.filename.as_full_path
2013-02-28 09:17:55.935 project[74283:c07] undefined method `as_full_path' for "2C86A58A-A92A-4A0F-B26C-0F5F583E142C.caf":String (NoMethodError)
=> NoMethodError: undefined method `as_full_path' for "2C86A58A-A92A-4A0F-B26C-0F5F583E142C.caf":String
The model is implemented using NanoStore::Model.
Edit:
When I clone the String returned by the model, the added method is present.
w.filename.dup.as_full_path
=> "/Users/user/Library/Application Support/iPhone Simulator/6.1/Applications/30D186A9-B1C7-4377-AE91-0D14BD3B4A6D/Documents/2C86A58A-A92A-4A0F-B26C-0F5F583E142C.caf"
Source: (StackOverflow)
I'm various issues getting CocoaPods dependencies to work in RubyMotion. Firstly, if I add dependency 'JSONKit'
to my Rakefile and then run rake
it get's aborted with a can't convert Pathname into String
error. rake --trace
then produces the following output:
** Invoke default (first_time)
** Invoke simulator (first_time)
** Invoke build:simulator (first_time)
** Execute build:simulator
/usr/bin/gen_bridge_metadata --format complete --no-64-bit --cflags "-I. -I." JSONKit.h -o "JSONKit.bridgesupport"
invalid option: --no-64-bit
Usage: gen_bridge_metadata [options] <headers...>
Use the `-h' flag or consult gen_bridge_metadata(1) for help.
rake aborted!
Command failed with status (1): [/usr/bin/gen_bridge_metadata --format comp...]
/Users/xxxx/.rbenv/versions/1.9.3-p194/lib/ruby/1.9.1/rake/file_utils.rb:53:in `block in create_shell_runner'
/Users/xxxx/.rbenv/versions/1.9.3-p194/lib/ruby/1.9.1/rake/file_utils.rb:45:in `call'
/Users/xxxx/.rbenv/versions/1.9.3-p194/lib/ruby/1.9.1/rake/file_utils.rb:45:in `sh'
/Users/xxxx/.rbenv/versions/1.9.3-p194/lib/ruby/1.9.1/rake/file_utils_ext.rb:39:in `sh'
/Library/RubyMotion/lib/motion/project/vendor.rb:93:in `block in build_static'
/Library/RubyMotion/lib/motion/project/vendor.rb:34:in `chdir'
/Library/RubyMotion/lib/motion/project/vendor.rb:34:in `build_static'
/Library/RubyMotion/lib/motion/project/vendor.rb:23:in `build'
/Library/RubyMotion/lib/motion/project/builder.rb:37:in `block in build'
/Library/RubyMotion/lib/motion/project/builder.rb:36:in `each'
/Library/RubyMotion/lib/motion/project/builder.rb:36:in `build'
/Library/RubyMotion/lib/motion/project/app.rb:50:in `build'
/Library/RubyMotion/lib/motion/project.rb:33:in `block (2 levels) in <top (required)>'
/Users/xxxx/.rbenv/versions/1.9.3-p194/lib/ruby/1.9.1/rake/task.rb:205:in `call'
/Users/xxxx/.rbenv/versions/1.9.3-p194/lib/ruby/1.9.1/rake/task.rb:205:in `block in execute'
/Users/xxxx/.rbenv/versions/1.9.3-p194/lib/ruby/1.9.1/rake/task.rb:200:in `each'
/Users/xxxx/.rbenv/versions/1.9.3-p194/lib/ruby/1.9.1/rake/task.rb:200:in `execute'
/Users/xxxx/.rbenv/versions/1.9.3-p194/lib/ruby/1.9.1/rake/task.rb:158:in `block in invoke_with_call_chain'
/Users/xxxx/.rbenv/versions/1.9.3-p194/lib/ruby/1.9.1/monitor.rb:211:in `mon_synchronize'
/Users/xxxx/.rbenv/versions/1.9.3-p194/lib/ruby/1.9.1/rake/task.rb:151:in `invoke_with_call_chain'
/Users/xxxx/.rbenv/versions/1.9.3-p194/lib/ruby/1.9.1/rake/task.rb:176:in `block in invoke_prerequisites'
/Users/xxxx/.rbenv/versions/1.9.3-p194/lib/ruby/1.9.1/rake/task.rb:174:in `each'
/Users/xxxx/.rbenv/versions/1.9.3-p194/lib/ruby/1.9.1/rake/task.rb:174:in `invoke_prerequisites'
/Users/xxxx/.rbenv/versions/1.9.3-p194/lib/ruby/1.9.1/rake/task.rb:157:in `block in invoke_with_call_chain'
/Users/xxxx/.rbenv/versions/1.9.3-p194/lib/ruby/1.9.1/monitor.rb:211:in `mon_synchronize'
/Users/xxxx/.rbenv/versions/1.9.3-p194/lib/ruby/1.9.1/rake/task.rb:151:in `invoke_with_call_chain'
/Users/xxxx/.rbenv/versions/1.9.3-p194/lib/ruby/1.9.1/rake/task.rb:176:in `block in invoke_prerequisites'
/Users/xxxx/.rbenv/versions/1.9.3-p194/lib/ruby/1.9.1/rake/task.rb:174:in `each'
/Users/xxxx/.rbenv/versions/1.9.3-p194/lib/ruby/1.9.1/rake/task.rb:174:in `invoke_prerequisites'
/Users/xxxx/.rbenv/versions/1.9.3-p194/lib/ruby/1.9.1/rake/task.rb:157:in `block in invoke_with_call_chain'
/Users/xxxx/.rbenv/versions/1.9.3-p194/lib/ruby/1.9.1/monitor.rb:211:in `mon_synchronize'
/Users/xxxx/.rbenv/versions/1.9.3-p194/lib/ruby/1.9.1/rake/task.rb:151:in `invoke_with_call_chain'
/Users/xxxx/.rbenv/versions/1.9.3-p194/lib/ruby/1.9.1/rake/task.rb:144:in `invoke'
/Users/xxxx/.rbenv/versions/1.9.3-p194/lib/ruby/1.9.1/rake/application.rb:116:in `invoke_task'
/Users/xxxx/.rbenv/versions/1.9.3-p194/lib/ruby/1.9.1/rake/application.rb:94:in `block (2 levels) in top_level'
/Users/xxxx/.rbenv/versions/1.9.3-p194/lib/ruby/1.9.1/rake/application.rb:94:in `each'
/Users/xxxx/.rbenv/versions/1.9.3-p194/lib/ruby/1.9.1/rake/application.rb:94:in `block in top_level'
/Users/xxxx/.rbenv/versions/1.9.3-p194/lib/ruby/1.9.1/rake/application.rb:133:in `standard_exception_handling'
/Users/xxxx/.rbenv/versions/1.9.3-p194/lib/ruby/1.9.1/rake/application.rb:88:in `top_level'
/Users/xxxx/.rbenv/versions/1.9.3-p194/lib/ruby/1.9.1/rake/application.rb:66:in `block in run'
/Users/xxxx/.rbenv/versions/1.9.3-p194/lib/ruby/1.9.1/rake/application.rb:133:in `standard_exception_handling'
/Users/xxxx/.rbenv/versions/1.9.3-p194/lib/ruby/1.9.1/rake/application.rb:63:in `run'
/Users/xxxx/.rbenv/versions/1.9.3-p194/bin/rake:32:in `<main>'
Tasks: TOP => default => simulator => build:simulator
The vendor directory in the project contains various JSONKit files.
Secondly, in another RubyMotion app, if I add dependency 'Nimbus'
to my Rakefile and then run rake
the app builds but errors out with uninitialized constant errors when I try to use anything Nimbus-related in my code and no vendor directory is created.
Anyone know what the problem is in these two instances?
Thanks
Source: (StackOverflow)
I've just got started with RubyMotion and, from what I can tell, it suggests using NSUserDefaults for a datastore. This seems kind of strange to me as the name suggests that it should be only used for storing config variables etc and not objects.
Is it okay to store my objects (such as User, Company, Tasks etc) in NSUserDefaults or should I use another approach?
Source: (StackOverflow)
I try to parse a text representation of a time into a ruby Time object.
Normally in ruby I would do it like this:
require 'time'
Time.parse('2010-12-15T13:16:45Z')
# => 2010-12-15 13:16:45 UTC
In RubyMotion I am unable to require libs and Time.parse is not available:
(main)> require 'time'
=> #<RuntimeError: #require is not supported in RubyMotion>
(main)>
(main)> Time.parse
=> #<NoMethodError: undefined method `parse' for Time:Class>
Is there a way to require the time library provided by ruby without having to copy and rewrite the whole code to make it compatible with RubyMotion?
Source: (StackOverflow)