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;
// I开发者_StackOverflow社区mplement 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];
}
}
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?
精彩评论