views:

120

answers:

3

If I have something I want to do repeatedly in an Xcode iPhone app, how do I set it up and call it?

For example, if I wanted to do this simple line lots of times from lots of different places in an app: label1.text = @"Hello";

And then what if I wanted to call or perform it from lots of different apps: how would I convert it into my own library, so I can import all my common code?

(I'm a COBOL programmer trying to learn how to program for the iPhone in Xcode. COBOL seems so easy compared to Objective-C and XCode!)

+1  A: 

You can split off the reusable code into small functions and classes, and put them in their own separate header and source files. This is a simple step that will greatly help you clean up your code.

Eventually you can put these files into a separate Xcode library project. This will enable you link with that project from different applications. However, this is somewhat advanced, and it looks that you are very new to all this so you may want to focus on the basics first.

StackedCrooked
Thank you all so much for your helpful answers. I am trying to focus on the basics first - and am struggling!I have got this code working:- (IBAction)buttonPressed { [self performSelector:@selector(doThis) withObject:nil afterDelay:0.0];}- (void)doThis { l1.text = @"This has been done.";}It works - but is there not an easier way? Is my void a method or a class? What if I want to pass info to it, or get back?I love the idea of putting common code into separate header and source files, or a separate project - but need to get this first stage sorted first.Thanks for your help.
philh
It's kinda hard to respond in the comment section, you better ask it as a new question.
StackedCrooked
A: 

The thing to search for is "Static Library", that's what you want to create that would let people use your code as a library.

This site has a pretty good walkthrough:

http://blog.stormyprods.com/2008/11/using-static-libraries-with-iphone-sdk.html

If your code is simple enough, just publishing source files that other people can include in a project suffices.

Kendall Helmstetter Gelner
A: 

For example, if I wanted to do this simple line lots of times from lots of different places in an app: label1.text = @"Hello";

Firstly, in every class where you want to run the line, you would have to have a reference to the label1 object. Since in this case, this is a interface element, the easiest way would be to set an outlet in your header and then connect it up in interface builder.

@interface MyController : UIViewController
{
    IBOutlet UILabel *label1;
}
@property(nonatomic, retain)  IBOutlet UILabel *label1;
@end

Every class instance that refers to the label instance must have an outlet pointing to it and then you have to wire the outlet to the proper label in interface builder. (You can also create the relationship programmatically but that is more complex.) This way when you set the text of the label in your code, the runtime knows what object to send the message to.

If you have a background in a procedural language like Cobol (shudder), you're going to have to change your thinking to one based on object-oriented program. Objective-C design is even more object dependent than other common OOP languages.

TechZen
I don't have experience with Objective-C, but couldn't you create a method that accepts a UILabel object by reference, and modifies the object's Text property?
Jeremy Seghi
Yes but you would end up with same basic issue of having to obtain a reference to the label. Then unless you want any label passed changed, you would have to filter for the right label. This method would also to be parked in a class someone (as a practical matter). Since you have to create the reference and you most likely need to treat each label different, an outlet is the easiest way to go. The good thing about the delegate and controller design patterns is that you can customize without subclassing.
TechZen