views:

56

answers:

1

When i move the slider it crashes the app. i know it is something to do with the argument i am passing to maxProcess method. it works fine if i take this out. but i need it to update the newEvent objects instance variable. Any ideas. This is what i have been told in another forum but i don;t know how to do this

"this code declares a new object newEvent that is never allocated and initialized. If you want this method to know about the newEvent instance variable in you MainViewController class you need to pass a reference to it into your FlipSideViewController class and use that reference in your maxProcess: method."

Flipsideviewcontroller.m

#import "FlipsideViewController.h"


@implementation FlipsideViewController

@synthesize delegate, sliderLabel;


- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor viewFlipsideBackgroundColor];
}

-(IBAction) sliderChanged:(id) sender
{
 UISlider *slider = (UISlider *) sender;
 int progressAsInt = (int)(slider.value +0.5f); 
 [self maxProcess: progressAsInt];  
 NSString *newText = [[NSString alloc] initWithFormat:@"Max: %d", progressAsInt];
 sliderLabel.text = newText;

 [newText release];
}   

-(void) maxProcess: (int) n
{
 Headcount *newEvent;
 newEvent.maxCapacity = n;   //  This is the area that appears to be the problem, makes i phone simulator crash
} 

/*
-(void) maxProcess: (int) n
{
 Headcount *newEvent;
 newEvent.maxCapacity = n;   //  This is the area that appears to be the problem, makes i phone simulator crash
}*/

- (IBAction)done:(id)sender {
 [self.delegate flipsideViewControllerDidFinish:self]; 
}


- (void)didReceiveMemoryWarning {
 // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];

 // Release any cached data, images, etc that aren't in use.
}


- (void)viewDidUnload {
 // Release any retained subviews of the main view.
 // e.g. self.myOutlet = nil;
}

mainviewcontroller.m

#import "MainViewController.h"


@implementation MainViewController

@synthesize displayString, displayStringOut, display, displayOut;


// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {

 self.displayString = [NSMutableString stringWithCapacity:40]; // Initializes string
    self.displayStringOut = [NSMutableString stringWithCapacity:40];
 [super viewDidLoad];
}


- (void)flipsideViewControllerDidFinish:(FlipsideViewController *)controller {

 [self dismissModalViewControllerAnimated:YES];
}


- (IBAction)showInfo:(id)sender {    

 FlipsideViewController *controller = [[FlipsideViewController alloc] initWithNibName:@"FlipsideView" bundle:nil];
 controller.delegate = self;

 controller.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
 [self presentModalViewController:controller animated:YES];

 [controller release];
}

-(void) processDigit:(int)digit
{
 [displayString setString:[NSString stringWithFormat: @"Total Number Inside: %i",digit]];
 [display setText: displayString];
}

-(void) processDigitOut:(int)digit
{
 [displayStringOut setString:[NSString stringWithFormat: @"Total Number Exited: %i",digit]];
 [displayOut setText: displayStringOut];
}


-(IBAction) clickClear: (id) sender
{
 newEvent.totalIn = 0;
 newEvent.totalOut = 0;
 [displayString setString:[NSString stringWithFormat: @"Total Number Inside: %i",newEvent.totalIn]];
 [display setText: displayString];
 [displayStringOut setString:[NSString stringWithFormat: @"Total Number Exited: %i",newEvent.totalOut]];
 [displayOut setText: displayStringOut];
}


-(IBAction) clickIn: (id) sender
{ 
 int x = newEvent.maxCapacity;

 if (newEvent.totalIn < x) {
  int digit = [sender tag];
  [self calculateTotalIn: digit];
 }
 else {
  [displayString setString:[NSString stringWithFormat: @"Total Capacity Reached: %i",newEvent.totalIn]];
  [display setText: displayString];

 }
}  

-(IBAction) clickOut: (id) sender
{
 if (newEvent.totalIn <= 0 ) {
  [displayString setString:[NSString stringWithFormat: @"There is no one left, you may as well go home"]];
  [display setText: displayString];

 }
 else {  
  int digit = [sender tag];
  [self calculateTotalIn:  digit];
  [self calculateTotalOut: digit];
 }
}

-(void) calculateTotalIn: (int) n
{
 int x = newEvent.maxCapacity;
 newEvent.totalIn = newEvent.totalIn + n;
 if (newEvent.totalIn == x) {
  [displayString setString:[NSString stringWithFormat: @"Total Capacity Reached: %i",newEvent.totalIn]];
  [display setText: displayString];
 }
 else {
  [self processDigit: newEvent.totalIn];
 }
}




-(void) calculateTotalOut:(int)n
{
 if (newEvent.totalIn >= 0) {
  newEvent.totalOut = newEvent.totalOut - n;
  [self processDigitOut:newEvent.totalOut];
 }
 else {
  [displayString setString:[NSString stringWithFormat: @"There is no one left, you may as well go home"]];
  [display setText: displayString];
 }
}
A: 

Let's step through your code here:

Headcount *newEvent;
newEvent.maxCapacity = n;   //  This is the area that appears to be the problem, makes i phone simulator crash

The first line defines a pointer to a Headcount object. The pointer is currently nil since it doesn't actually point to anything.

The second line attempts to dereference this nil pointer to set a value. You can't do that.

What is your intent of the 2nd line -- what are you trying to achieve?

Shaggy Frog
i want the value of the slider(when moved by the user) to be sent to the newEvent Object's instance variable maxCapacity. The reason for this is that i use this value in a method in the mainviewcontroller class to put a limit on the number of times a button press is counted.
Rob Hartley
this whole code worked great when it was all on the mainviewcontroller but i have split it as i want the slider to be on the flipside
Rob Hartley
You're telling me the low-level details -- the "how" -- I'm interested in the high-level details -- the "what". *What* do you want to happen?
Shaggy Frog
It's not nil — it's stack garbage. And the second line would not try to dereference nil if the variable were nil — it would call `[nil setMaxCapacity:n]`, which is a no-op.
Chuck
when someone moves the slider i want to use this value in my click in action method. i compare this value with totalin variable.
Rob Hartley
I am very new to programming, and i am learning as i go, so i'm sorry if my responses aren't very clear, i appreciate your help though
Rob Hartley