Jimmy Larsson, CISSP, CEH
asked on
xcode: How do I create a subroutine?
Hello
I am new to xcode and Objective-C. Also I am pretty new to object oriented programming in general. So pardon if I am not using the correct wordings here... :-)
I have created a simple game for Iphone using xcode. In 3-4 different places in the code I need to reuse the same code. Instead of just copy/paste the code-snippets I want to make a subroutine (or whatever it is called) because sooner or later (probably sooner) I will do changes in one of the copies and I will screw things up.
The "subroutine" doesn´t need to take any arguments, and does not need to return any arguments. It will just modify global variables and do changes to the gui.
How is this solved most easily?
Thanks in advance!
Best regards
Kvistofta
I am new to xcode and Objective-C. Also I am pretty new to object oriented programming in general. So pardon if I am not using the correct wordings here... :-)
I have created a simple game for Iphone using xcode. In 3-4 different places in the code I need to reuse the same code. Instead of just copy/paste the code-snippets I want to make a subroutine (or whatever it is called) because sooner or later (probably sooner) I will do changes in one of the copies and I will screw things up.
The "subroutine" doesn´t need to take any arguments, and does not need to return any arguments. It will just modify global variables and do changes to the gui.
How is this solved most easily?
Thanks in advance!
Best regards
Kvistofta
ASKER
I try to understand the AppDelegate-thing and I think I need to the MVC-documentations a few times before I do that. And I certanly will...
In your first section you write about "method/function". Can you give an example that explains it to me?
Thanks in advance!
Best regards
Kvistofta
In your first section you write about "method/function". Can you give an example that explains it to me?
Thanks in advance!
Best regards
Kvistofta
- (void)doSomething
is a method called "doSomething". When you call that method it will run the code inside the brackets. This way you specify the code once and everywhere, where you need to run that code you call the method...
http://en.wikipedia.org/wiki/Method_(computer_science)
best
Chris
is a method called "doSomething". When you call that method it will run the code inside the brackets. This way you specify the code once and everywhere, where you need to run that code you call the method...
http://en.wikipedia.org/wiki/Method_(computer_science)
best
Chris
ASKER
Ok. So I declare my own method like this:
-(void)doSomething {
NSLog(@"I did something");
}
How do I call it? For example I have several IBAction:s like this:
-(IBAction)button1Pressed: (id)sender {
//Here I do stuff...
// Here I need to call "doSomething"
// Here I do other stuff...
}
-(IBAction)button2Pressed: (id)sender {
//Here I do stuff...
// Here I need to call "doSomething"
// Here I do other stuff...
}
Thanks in advance!
/Kvistofta
-(void)doSomething {
NSLog(@"I did something");
}
How do I call it? For example I have several IBAction:s like this:
-(IBAction)button1Pressed:
//Here I do stuff...
// Here I need to call "doSomething"
// Here I do other stuff...
}
-(IBAction)button2Pressed:
//Here I do stuff...
// Here I need to call "doSomething"
// Here I do other stuff...
}
Thanks in advance!
/Kvistofta
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thanks! That was exactly what I was looking for!
/Kvistofta
/Kvistofta
http://www.cocoalab.com/?q=node/24
http://en.wikipedia.org/wiki/Model_view_controller
Basically if the code is reused multiple times we use "methods / functions" to solve this. You write a new class with that methods and include the class where you need it.
You could also add a method to your AppDelegateController
like
Open in new window
and from another class you could call that method. (replace AppDelegate with your AppDelegate Class name... it has to be included as well)
Open in new window