Hi I have a little a problem. My program doesn’t remember random value which is generated by clicking first button. But I want use that value in another button and there is the problem. If I try to return that value, program says that this methond shouldn’t return value. Here’s how my code looks like:
int randomprocess;
- (IBAction)button:(id)sender {
randomprocess = rand() % 3;
// Do something }
- (IBAction)b1:(id)sender {
if (randomprocess == 0) {
// Do something
} else {
// Do something else
}
If I won’t write that very first line, the second button won’t recognize ‘randomprocess’. And now when I declare it it’s still zero or whatever number I set it to equal.
Probably, you have declared an iVar / property with the very same name, that covers your global variable. Use only a global variable or an iVar.
An declaration
int randomprocess;outside a method or object head makes it to a ‘normal’ global C variable.An iVar is a local variable in relation to your object. An property is (usually) an iVar with certain accessors.
If you have declared both, a global variable and a local one (resp, an iVar), the global one is not visible, but covered by the local one.
In general it is a bad idea to use global variables. If you have to, make it static. Better is to use an iVar.
EDIT
To make an property, your header should look like:
And for the implementation: