Trying to message a button from another class - iphone

I'm new to Objective-C so I may be doing this completely wrong, and if I am please correct me. I am trying to make a separate class in my iPhone app just for skinning buttons. My hope is that this will allow me to reuse as much code as possible but before I spend too much time on it, I would like to know if its possible/a good idea to send a message to a UI Control from another class, and if i can, how should I do it? right now im trying to pass the sender ID to my SkinTools class and message that but it doesn't look like it will allow me to message the layer object.
So, am I just completely off the wall here, or is this possible?

Consider looking into using the delegate pattern.

One could just use the addTarget:selector: method for this purpose. As target set the class you want to send the message to, as selector the method you want to call on the class.
You could add some iVars to your class, like id buttonTarget and SEL buttonSelector and create an initializer like -initButtonWithTarget:selector: to set these values on initialization.

It turns out categories was the answer I needed, then I can just add a skin method to each control I use. I can even put them all in the same file to make it easy to get to.

Related

How to pass a View to my ViewModel so an I*Service can use that View to do something

https://github.com/brianchance/MvvmCross-UserInteraction is a very nice plugin for showing cross platform Alerts!
But for this question, can we assume it can not use a UIAlertView (or some other top level MessageBox type call on other platforms) but needs to show a Message within a given subsection of the screen (i.e. on IPhone you would need to supply a UIView to the plugin which it will use to show the message within).
So, how would you set this up so the ViewModel knows what View to use as its display container?
As a specific example, if I wanted an Error Service, as so -
public interface IErrorPFService
{
void Show();
void Hide();
void SetErrors(List<Error> errors);
}
and I create a platform specific implementation for it.
If I inject this into my ViewModel so it can control Error Show/Hide/Set how do I tell it the UIView (or equivalent) that I want my Errors to show within?
Can I just expose the IErrorPFService field as a public property and do -
MyViewModel.ErrorPFService = new ErrorPFService(View);
in my ViewDidLoad ...
Or is this coupled incorrectly vs Mvvm Practice?
I would expect the ViewModel to subscribe itself to the ErrorService.
When receiving a message it would expose it in a collection(?) and the View would bind to that collection.
This way the View is unknown to the service and the ViewModel has the chance to influence the View contrary to your solution.
It would help if you could give an example for the scenario you are describing.
Sometimes, the way you visually want to display something might not be the best way, so if it's possible for you, you might find a different and simpler way, which spares you from having to find a solution regarding what you are describing.
Generally, I always do the best I can to avoid the idea of having to actually pass a 'view' or an abstraction of it, from the view-model to view. Also, cross-platform wise, things can work very different in terms of UI interaction. You can find yourself in a situation when things are complicated just because UI works differently than what you expected.
But let's try find another perspective:
At any given point, the view knows what data \ feature it's displaying. So when you are calling from the view-model an user interaction action (by a service, property change, event, etc) the view should 'expect' it.
For example, the platform specific user interaction implementation is able to get the currently displayed top-view and interact it in a platform specific manner or based a relationship. In your example, the message-box can be displayed in a specific sub-view of the top level view.
In advanced scenarios, I guess you could try to create a cross-platform approach for this, but you should try to put in balance all the abstraction you want to create just for that. Think about doing this as a plan ... Z. If possible. Again, giving an example might help.

Where to Update a JFrame

Hi I have a JFrame class that I update based on information I recieve from another class. I have created an update method within the JFrame which I use to update the different objects on the page. But my Question is is that the correct thing to do?
You can make it however you want, there is no "right" way. It's all a matter of preference. Personally, I like that way of doing it. In almost all of my GUI's I have a method called "update()" or something similar.
its depends on how updates are being triggered, but using event-listeners and actionCommands might be helpful. I don't necessarily like this methods but it is convenient.

Retrieving data from database to a different view controller

http://www.techotopia.com/index.php/An_Example_SQLite_based_iOS_4_iPhone_Application_%28Xcode_4%29
I have found this sqlite tutorial on google and it worked great for my requirement.
In this tutorial both adding and retrieving data are done on the same viewcontroller. but i want that 'save' method in the tutorial in one viewcontroller and 'find' method in the other.
And also someone tell me whether this tutorial is an effective way to save data or not.because this is the easiest tutorial I found so far.
You might also create a new class ,or use inheritance (upper class) call it for example "SqliteClass"
that have both the 'Save' and 'Find' method
then you can use any method in SqliteClass from any other class ,,
in case you don't want to use inheritance make sure that the methods in SqliteClass are "class" methods the one begins with "+" not"-"
I used both ways and they work just fine ..
Hope that helps :)
Yes, you can to that. you can have n number of view and can save data from any view controller.
check this tutorial.
http://www.icodeblog.com/2008/08/19/iphone-programming-tutorial-creating-a-todo-list-using-sqlite-part-1/

Why we use app delegate in our application

I am new in iphone development and i needed a array which i use to access in different class so my senior told me that you should declare in App delegate and access it another class in which it require, so i wrote line for that
MyAppAppDelegate * appObject =
(MyAppAppDelegate*)[[UIApplication sharedApplication]delegate];
I done my task successfully but i couldn't get it 100%. Someone tell that why we use this and in what exactly situation in which we've to use this?
AppDelegate is loaded first when you run your application as it contains window. So, the variable and objects you want to access throughout your project is declared in AppDelegate. You just have to create an instance and you can access all the objects in AppDelegate.
ApplicationDelegate can be useful as a singleton class but you have to use it with discretion - and there are varying opinions on this - but if you have a few global type properties or methods you want to recall from various other classes, and I emphasize few, then ApplicationDelegate may be a nice place to add these.
And yes, it is bad design - but you can get away with it if you are prudent and as #Sedate Alien mentions, take a look at dependency injection.
The purpose of ApplicationDelegate, by the way, is mainly to handle events like loading your application, when you return to home screen, when you come back from home screen, etc.

Objective-C Singleton Objects and Global Variables

I'm aware of other posts on this topic but I'm only really one rung up the ladder from being a noob so need a bit more help.
My iPhone app has several global variables - some I have declared and given values in a class but others need to be set during a login process (like a token for example) that then need to be accessible for the lifecycle of the app from any class or method. I am told I should really be using a Singleton object for all of this which I presume is a class that's instantiated on startup. If so, could someone give me the simplest example of such header and implementation file and how/where I should instantiate it? Then I need to have some strings that are set from the off and others that can be set/got later on?
Thanks very much in advance. Also, I'm new here so if my etiquette is off in any way, please let me know.
Thanks,
This link shows some code to create a singleton class : http://www.galloway.me.uk/tutorials/singleton-classes/
You would use it something like :
[[MyManager sharedManager] doSomething];
The call to sharedManager would get the one instance of the class (or, if this is the first time you called it, would create it) - this makes sure that you only have one of them :)
It also overrides release, retain, autorelease etc to make sure that you can't accidentally get rid of the sharedManager by mistake!
This class will instantiate itself the first time you use it but if you need it to be created on startup, just call [MyManager sharedManager] and it will create it for you.
You define the class like any other objective-c class - just add properties etc
Hope that helps :)
Global variables aren't good, but singletons aren't much better when they're just used to provide global access to some data. Anything bad you can say about a global variable, you can also say about a singleton that's used for global access. A better solution is to create a data model and pass that model from one view controller to the next.
Here's a previous SO question that might help.