In my RubyMotion app I would like to have a attribute, called access_token, accessible in every UIViewController instance.
All my controllers are either a subclass of TableController or AppController.
I tried creating an attr_accessor for TableController and AppController, though the problem is that assigning a new value will not be set for TableController or AppController at the same time.
How could I achieve this?
Make TableController and AppController subclasses of a Controller class, and add the attribute in it.
I personally use class variables for things like this, since iOS apps are not multi-user. Taking advantage of the fact that inherited classes share class variables, ##access_token will have the same value for all of your UIViewController subclasses (or your own subclass if you prefer).
I have something similar to this:
# Reopen and extend
class UIViewController # Actually I prefer UIViewController.class_eval do
##access_token = nil # This will have the same value for all UIViewController children
def self.access_token=(value)
##access_token = value
end
def self.access_token
##access_token
end
end
In reality, I would build a class that holds properties including and in addition to the token.
Related
In rails app, how are helpers included in viewcontrollers, and what's the order of includion(which helper is included first, second, so on)?
Also, are all helpers available to views? If so, does the inclusion order same as the view's corresponding controller?
Any controller can have a corresponding helper.
For example if you have a controller named Question (questions_controller.rb), it can have a helper named questions_helper.rb. The question helper is accesible only to the views corresponding to the question controller.
Besides you will have an Application helper (application_helper.rb) which is accessible to all the views in the application.
All helpers are not available to views by default. But you can include other helpers in a helper like this:
module MyHelper
include QuestionsHelper
end
You can also use a helper inside a controller like:
class MyController < ApplicationController
include MyHelper
def my_method
...
end
end
How can I access the controller name inside a partial or a view thats rendered by the controller.
I want to create a dynamic template that changes depending on the controller rendering it, whats the best way to do it? I would prefer to access the controller inside the view rather than setting a new variable.
There are two methods to detect the entry point in Padrino: request.controller and request.action.
controller :foo do
get :bar do
[request.controller, request.action].inspect
end
end
=> ["foo", :bar]
I have a very simple application test in which I want to drag and drop files in a NSImageView object.
I can already get the list of files from this action, but now I want to store this data in an array to be accessed in the View using data bindings. The only code I have that works fine with data bindings, tough, has an array in AppDelegate that I access using an Array Controller. But then, my data is inside a class I created to the NSImageView called "DropView", which extends "NSImageView" class and handles the drop action.
How can I pass the array information to make the bindings possible?
Any suggestion is welcome. Thanks!
I decided to use a singleton in a bigger project. In this smaller one I did like this:
1 - Add AppDelegate reference to the subclass (m file):
#import "AppDelegate.h"
2 - Call a method declared in the AppDelegate passing all the information needed like this:
[[NSApp delegate] doSomething:someInformation];
3 - Set all the information in the method used.
4 - Make the bindings in the interface.
The simple answer is that you should not be storing model data (the array of file URLs) in a view.
Your view should pass the list of files to some other controller object, which then stores the list of files in some sort of model object. This is what MVC is about.
One way to do this is to use the delegate pattern, where your view would declare a protocol method something like this:
#protocol YourViewProtocol
- (void)imageView:(YourImageViewClass*)aView receivedDroppedURLs:(NSArray*)arrayOfURLs;
#end
Your view would also have a weak datasource property:
#interface YourImageViewClass : NSImageView
#property (weak) id <YourViewProtocol> datasource;
#end
You would then set your controller object as the datasource of the view. The controller must conform to the protocol and implement its method:
#interface YourController <YourViewProtocol>
- (void)imageView:(YourImageViewClass*)aView receivedDroppedURLs:(NSArray*)arrayOfURLs;
#end
Then, in the view method that receives the drop, you'd do something like:
- (void)receivedDroppedURLs:(NSArray*)urls
{
if([datasource conformsToProtocol:#protocol(YourViewProtocol)])
{
[datasource imageView:self receivedDroppedURLs:urls];
}
}
The other way to do this is via custom bindings. Implementing bindings in a custom view is a bit more complicated and I'd recommend that you use the delegate/datasource pattern initially, if you're not an experienced Cocoa developer.
Using bindings, how can I make NSButton invoke a method in my AppDelegate class? I tried setting the target sleector to buttonClicked: but in console it gives me:
unrecognized selector sent to instance
0x100105060
Also there's no place in bindings to specify which instance to look for the method in so how can I set it to AppDelegate?
Thanks.
As Simon says, bindings are not appropriate for this situation. Bindings allow you to "bind" UI elements to a data source. In your situation, you need an action - not a binding.
You are getting the error because you haven't actually defined the buttonClicked action. Thus, make sure you declare it with something like in your AppDelegate.m:
- (IBAction) buttonClicked:(id) sender;
Then set the action via interface builder by first setting the "FilesOwner" to the AppDelegate and then connecting the onTouchUpInside event to the action "buttonClicked".
Hope this helps!
Bindings are used to synchronize a variable and an element of the interface.
In your case, I guess an action would be better. Declare your method with IBAction and control-drag from your button to your AppDelegate instance in Interface Builder to be able to select the action.
I'm creating an app that has a UITableView.
The data will be comming from an XML fetched over the net. I'm using NSXMLParser for this and it works. I used my tableView controller as the delegate for this so it implements the protocol for it:
#protocol NSXMLParserDelegate;
#interface MainView : UITableViewController <NSXMLParserDelegate>
Now this works perfectly, as I've nslogged the resulting parse.
Now, I want to populate the NStableView, so Reading I find that I need to add the datasource and delegate.
UITableViewDataSource
and
UITableViewDelegate
both of which are protocols.
How would I go about doing this on the same class? can I implement more than one protocol with the same class? should I move the delegation of the parser to another object and use this controller for this purpose?
Basically the question is what is the best way to do this?
thank you
Sure, you can implement as many protocols in a class as you want:
#interface MainView : UITableViewController <NSXMLParserDelegate, UITableViewDataSource, UITableViewDelegate>
Is that the "proper" way of doing that? I don't think there's a "right" answer to that. A purist might say no. I'd say do it where is makes sense, but err on the side of breaking it out into separate classes. For example, if your view controller is a simple menu then it would make sense for your view controller to also be your table view delegate and data source; there's no advantage in breaking it out into multiple classes.
If you have to parse XML my intuition suggests that it's starting to get a bit more complex. Maybe have a data class that implements the data source and XML parser and a controller class?