кто-нибудь может понять, почему мое приложение падает, когда я перемещаю ползунок - PullRequest
0 голосов
/ 17 сентября 2010

Когда я перемещаю ползунок, приложение вылетает.я знаю, что это как-то связано с аргументом, который я передаю методу maxProcess.это работает нормально, если я возьму это.но мне нужно, чтобы обновить переменную экземпляра объектов newEvent.Есть идеи.Это то, что мне сказали на другом форуме, но я не знаю, как это сделать

", этот код объявляет новый объект newEvent, который никогда не выделяется и не инициализируется. Если вы хотите, чтобы этот метод знал оПеременная экземпляра newEvent в вашем классе MainViewController вам необходимо передать ссылку на него в ваш класс FlipSideViewController и использовать эту ссылку в вашем maxProcess: метод. "

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.м

#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];
 }
}

1 Ответ

0 голосов
/ 17 сентября 2010

Давайте пройдемся по вашему коду здесь:

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

Первая строка определяет указатель на Headcount объект. Указатель в настоящее время nil, так как он фактически ни на что не указывает.

Вторая строка пытается разыменовать этот указатель nil для установки значения. Вы не можете этого сделать.

Каково ваше намерение во 2-й строке - чего вы пытаетесь достичь?

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...