how to make a public class/method using objective-c? - iphone

we are a group working on an iPhone app. and its our first time. I just would like to know the easy way to implement a public class so that my partner can give me a variable that contains a value i need to use in my code. It doesn't have to be a class any other easy solution would work.
Just to make it more clear my friend has a value in his code that i need to use to display it on the screen "using my code". What i know is that everything has to communicate through a controller class which in this case the thing i need to implement my friend must be able to call it, give it the value then i take this value and display it. THANK YOU IN ADVANCE!

I. Just make an property in .h
#property (nonatomic, retain) NSString *userName;
and in .m file
#synthesize userName;
Docs here
II. He can make and delegate protocol in his class, the you need to set your object to be the delegate of his class instance and implement the delegate method. When he has the vale will call the delegate method and you will recive it in your method.
Docs here

Related

When is #property and #synthesize needed?

When exactly do I have to add #property (nonatomic, retain) and #synthesize? Also, when is declaring IBOutlet someObject enough? How is it that I can set/get UILabel value without #property & #synthesize? Does it depend on UI object type?
And yes, I have read similar questions about these 2 :)
The pair (#property, #synthesize) will create the set/get methods used for accessing your ivars from other objects.
In a usual view controller you don't need to define properties for your IBOutlets since they should normally only be accessed by the view controller they belong to.
Highly recommended read: Using Properties in Objective-C Tutorial
property is needed only when you need the access to the member variables through the objects of that particular class. If you want to change some label's text at run time, that too accessing the object of the View Controller, then only you will need to have property defined for it, else not.
Outlet is just to make connection between an object from xib and a member from the class. If you want to give access to that member though object write property for it, else not.
Have a look at this
Its not needed if you dont want the variables or objects to be accessed outside the class by other objects.

Want to create a container class which will only exist once

I want to make a class that can hold settings for my app. Its pretty basic. I will access it from a few different classes, but only want 1 version of this container class to exist so they all see the same data.
Is there something special i need to flag for this?
Here is what I've done so far:
Filters.h
#import <Foundation/Foundation.h>
#interface Filters : NSObject
{
NSString *fuelType;
}
#property (nonatomic, copy) NSString *fuelType;
#end
Filters.m
#import "Filters.h"
#implementation Filters
#synthesize fuelType;
#end
Is there some flag i need to use when i alloc an instance of this class or how should I work this if I need to access the fuelType value from 2 different classes?
Thanks
-Code
For global application settings a better way would be to use NSUserDefaults or if you want to store data for use you should look up using core data and sqlite.
Lastly, if you want to go for ease of use you could do a core data style app delegate class and grab it by using:
[[[UIApplication sharedApplication] delegate] myClass] that way you'll always have that version of the class.
You need a singleton:
you can create your singleton by your own or use AppDelegate object which is an object that is always alive and never released while your application in the frontground (just put the vars needed there and initialize them dynamically).
Here are some links on how to create a singleton.
Cocoa fundamental Guide: Creating a Singleton
and
CocoaDev Singleton Pattern
What you're looking for is a singleton. Most people advise against using singletons though as it is often considered "dirty". See "Singleton" in this apple doc to learn more about it.

what do mean by "delegate" in iPhone application

I usually read a word 'delegate' in the apple document and their library book. What exactly meaning about this word? Any special meaning in iPhone?
Thank you very much.
Delegates are a design pattern in object-oriented languages that allow an object to "call out" to unknown code to perform activities or calculations that that object cannot effectively do on its own. Let's say you have a class Dog:
#protocol DogDelegate;
#interface Dog : Wolf <Domesticated>
- (void)bark;
- (void)tiltHeadAdorably;
- (void)playWithToy: (Toy *)aToy;
#property (readonly) Toy *favoriteChewToy;
#property (readwrite, assign) id <DogDelegate> delegate; // "DELEGATE" PROPERTY DECLARED HERE
#end
The delegate object is generally supplied by the code that instantiates Dog, and is called upon by that instance to do things that the dog itself can't do. For instance, consider this interface of the DogDelegate protocol, which defines what the Dog's delegate object is expected to do:
#protocol DogDelegate <NSObject>
#required - (void)letDogOut: (Dog *)aDog;
#required - (void)letDogIn: (Dog *)aDog;
#optional - (void)scratchDog: (Dog *)aDog forTimeInterval: (NSTimeInterval)duration;
#end
In this case, an instance of DogDelegate is often the owner of the Dog (and, in Objective-C, a delegate often owns an object, so this lines up nicely with the metaphor.) The dog, when it needs to go out for... dog activities... will ask its delegate to perform the -letDogOut: method, placing the dog in the backyard. When done, it will ask its delegate to perform the -letDogIn: method, bringing the dog back inside. When the dog wants affection, if its delegate is able to, it will ask the delegate to scratch it for some period of time using -scratchDog:forTimeInterval:.
Delegate is a Design Pattern that Apple adopts heavily. In a nutshell, think of it like "I'm responsible for handling ...". Where ... is a notification, event, protocol, etc. For example, your AppDelegate is responsible for your handling your App setup, display, launch.
Please keep in mind I am over simplifying it. But I am sure someone can provide a much more detailed answer if you need.
A delegate is the same thing as a callback function in JS ( the exception being that delegates are type-safe.) For example, if you're doing Ajax in JS, you declare a callback function to be called when the Ajax call is finished. In this same fashion, you would declare a delegate function to be called, for example, when a song selection dialog is closed.
Delegate is a object that controls the whole application. It shows the main window and will tell the app what to do when it is launched and closed. It is basically the command center of your app. The best way to use this is to call other controllers to display some NIBS. There really isn't anything special about a delegate in an iPhone app, but there are special methods that are called.

Objective-C iPhone - Delegating an IBAction from one class to another? Can this be Done?

I have created a class, that makes it easy to enter in the amount for a particular price in the same way an ATM machine allows you to enter in an amount, user does not enter the decimal.
This is a generic class called (AmountPicker), so that it can be used among many other classes. I am using it by invoking the presentModalViewController method, this class uses a nib file. Inside the xib there is a hidden text field and a label along with a done button in the navigation bar. The done button is an IBAction connected to a method named doneButtonPressed.
I am wanting to know if it is possible to delegate this method to another class, so that the logic inside the method can differ from the one already defined inside the amountPicker class.
The reason is, I use the amountPicker Class in two places one for entering a purchase, which takes the label text and assigns the value to a variable inside the appDelegate, this was ok when i just used it in one class, but now there is a detailTableViewController of the purchase and this view will also need to provide a way to change the data in case it was entered wrong.
So in the detail view i am using a custom header, the custom header has a textField for the name of the purchase, a button to activate the AmountPicker, and a label for the amount, when the amount button is pressed then the AmountPicker class pops up and the user enters the amount and presses the done button, NOW, this is where i need to change the logic for the doneButton, instead of using a variable in the appDelegate.
I was thinking that the DetailTableViewController could act as a delegate for the amountPicker Class and override the doneButtonPressed method, so that the amountLabel can be updated with the new amount.
Is this possible, if not what is the right way?
Your thinking is right along with how delegation is normally used. What I would do is create an Objective-C protocol, maybe AmountPickerDelegate. Then, in your interface for the AmountPicker, you would have this:
#interface AmountPicker : NSObject {
NSString *someValue;
...
id <AmountPickerDelegate> delegate;
}
#property (assign) id <AmountPickerDelegate> delegate;
#end
Then, in your delegate class, you would do this:
#interface DetailTableViewController : UITableViewController <AmountPickerDelegate>
Finally, in the original AmountPicker class, you could call something like this:
[delegate amountPickerDidPressDoneButton];
which would execute in the delegate.

Calling one class file from another

I am new to the iPhone development environment so be gentle:
Currently writing an iPhone game app that will also have a high score view. Was wanting to make this view so that it was on its own and I could call it from another class (.m file)
Question is, how do you call another class file? I know I need to include the header from that file, but how do I call a "function/message" from that class...something like updating the high score?
I hope this makes send. Thanks in advance for any and all help.
Geo...
You really should work your way through the introductory documentation on Apple's developer website:
Learning Objective-C: A Primer
and
Your First iPhone Application
If your function is static, call it like this:
[ClassName theFunction:parameter];
If your function is a member of the class, then create an instance of that class and call the function like this:
ClassName obj = [[ClassName alloc] init];
[obj theFunction:parameter];
Don't think of it as calling functions/methods/procedures/whatever. Think of it as one object talking to another. What do they need to do this? You need a reference to the object you want to talk to.
Often, you'll want to create an instance variable that gets assigned to the object you're interested in. If both objects are in a nib together, you can draw connections between them in Interface Builder. Otherwise, you'll need to structure your code so that they can find each other (e.g., give the nib's owner a reference to whatever other object needs to talk to the view).
You might want to try working through one of the many tutorials out there (for instance, on Apple's developer site) to get a feeling for how this works.
The preferred technique for this would be delegation. So that your main view delegates the task of scoring to your HighScore view.
#protocol myDelegate;
#interface myClass : UIView {
id <myDelegate> delegate;
}
#property (nonatomic, assign) id <myDelegate> delegate;
#end
#protocol myDelegate
- (void)myMethod //Method to be implemented by your High Score View
To implement this protocol in your High Score View do:
#interface HighScore : UIview <myDelegate>
The in your HighScore.m implement the method mymethod:
- (void)myMethod {
//update Score etc...
}
To call the method from your other view do:
myHighScoreView.delegate = self // self is your other view
[delegate myMethod] // calls the method in the other view.
I hope that's clear.
-Oscar