It looks like you're new here. If you want to get involved, click one of these buttons!
IBOutlet UILabel *anumber;
IBOutlet UILabel *bnumber;
IBOutlet UILabel *cnumber;
IBOutlet UILabel *dnumber;
NSMutableArray *randomB;
NSMutableArray *numberPicker;
}
-(IBAction)Calculatenow;
float VarA = 0.00;
float VarB = 0.00;
float VarC = 0.00;
float VarD = 0.00;
float result = 0.00;
@implementation DataViewController
@synthesize dataObject = _dataObject;
-(IBAction) Calculatenow{
result = [[numberPicker objectAtIndex:arc4random() % [numberPicker count]] floatValue];
VarA = VarC;
VarB = result;
VarC = VarA + VarB;
anumber.text = [[NSNumber numberWithFloat:VarA] stringValue];
bnumber.text = [[NSNumber numberWithFloat:VarB] stringValue];
cnumber.text = [[NSNumber numberWithFloat:VarC] stringValue];
dnumber.text = [[NSNumber numberWithFloat:VarD] stringValue];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}
#pragma mark - View lifecycle
- (void)viewDidLoad
{
anumber.text = @\"0.00\";
bnumber.text = @\"0,00\";
cnumber.text = @\"0.00\";
dnumber.text = @\"0.00\";
[super viewDidLoad];
NSMutableArray *numberPicker = [NSArray arrayWithObjects:
[NSNumber numberWithFloat:10.00],
[NSNumber numberWithFloat:2.56],
[NSNumber numberWithFloat:4.25],
[NSNumber numberWithFloat:1.95],
nil];
}
Replies
You need to read up on variable scope, and on Cocoa memory management.
You've defined an instance variable numberPicker. Then in your viewDidLoad method, you create a local variable which is also called numberPicker. That local variable gets forgotten when you exit your viewDidLoad method (the local variable "goes out of scope".)
You should change your instance variable numberPicker to a retained property (or a strong property, if you're using ARC):
or, for ARC applications:
You need to change that line to assign a value to your property instead of creating a local variable.
Then you need to set the numberPicker property to nil in your viewDidUnload method:
There may be other problems with your code as well. Like I said, you need to stop and learn about memory management and the fundamental differences between local variables, instance variables, and properties, before you do anything else. Until you understand those concepts, you are going to be lost, and your code will crash all over the place.
Duncan C
WareTo
Animated GIF created with Face Dancer, available for free in the app store.
- Spam
- Abuse
- Troll
0 • Off Topic Insightful Disagree Dislike Like AwesomeI think you right on the memory issue. It doesn't always produce
a result... maybe 3 out of 10 button clicks.
Does this have to do with the arc4random? Does that make it sluggish?
Thank you for your response.
- Spam
- Abuse
- Troll
0 • Off Topic Insightful Disagree Dislike Like AwesomePost your code. It sounds like you still have problems with it.
Duncan C
WareTo
Animated GIF created with Face Dancer, available for free in the app store.
- Spam
- Abuse
- Troll
0 • Off Topic Insightful Disagree Dislike Like AwesomeIn the .h
In the .m
- Spam
- Abuse
- Troll
0 • Off Topic Insightful Disagree Dislike Like AwesomeIf you don't know how to step through your code in the debugger, you should learn. In the meantime, add log statements that display the new random value as well as the values of the variables that you are displaying.
Duncan C
WareTo
Animated GIF created with Face Dancer, available for free in the app store.
- Spam
- Abuse
- Troll
0 • Off Topic Insightful Disagree Dislike Like AwesomeSpent some hours trying to find the problem without any success...
In simulator, after clicking the button 5 times without any result, the sixth time it produces six results. On my device it doesn't produce anything.
I uploaded the project here in case you want to have a look.
http://wtrns.fr/hdRAAah6Iqc_ag
Thanks
- Spam
- Abuse
- Troll
0 • Off Topic Insightful Disagree Dislike Like Awesome