Amazon.com Widgets Help!!!!!!! I think i am doing something stupid
Welcome, Guest. Please login or register.
Did you miss your activation email?
May 20, 2013, 11:02:55 PM
Home Help Search chat Login Register   
News: Read this please.The Great Kangaroo Escape Looking for reviews of the 4th ed on Amazon!   Twitter:  @skochan
                     

+  Official Forum for Programming in Objective-C (the iPhone Programming Language) - Stephen Kochan
|-+  Old Stuff
| |-+  Help!!!
| | |-+  Help!!!!!!! I think i am doing something stupid
Pages: [1]   Go Down
Print
Author Topic: Help!!!!!!! I think i am doing something stupid  (Read 1049 times)
RobHartley01
Newbie
*
Posts: 4






« on: September 15, 2010, 11:00:42 AM »

Hi, I am new to programming and have set myself the challenge for learning Objective C so i can program i phone, i pads etc. I have put together a simple program that counts clicks from the user. It all works great until i try to incorporate the flip view/ switch view  screen. i want to have a slider on the back page which sends the result to a object instance variable which i have initialized on the MianViewController. I am probably doing something very stupid because i don't understand the concepts fully. Could someone out there look through my code and throw me a lifeline. i feel that if this concept is explained it will be a breakthrough i have been needing!  Here is my code:  oh, it builds fine but crashes when i try to move the slider! Do'ah

FlipsideViewController.h

#import <UIKit/UIKit.h>
#import "Headcount.h"

@protocol FlipsideViewControllerDelegate;


@interface FlipsideViewController : UIViewController {
   
   id <FlipsideViewControllerDelegate> delegate;
   UILabel *sliderLabel;
}

@property (nonatomic, assign) id <FlipsideViewControllerDelegate> delegate;
@property (nonatomic, retain) IBOutlet UILabel *sliderLabel;

- (IBAction)done:(id)sender;
-(IBAction) sliderChanged: (id) sender;
-(void) maxProcess: (int) n;

@end


@protocol FlipsideViewControllerDelegate
- (void)flipsideViewControllerDidFinish:(FlipsideViewController *)controller;
@end

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
}

- (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;
}


/*
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
   // Return YES for supported orientations
   return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
*/


- (void)dealloc {
   [sliderLabel release];
    [super dealloc];
}


@end

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];
   newEvent = [[Headcount alloc] init];
   newEvent.maxCapacity = 16;
   [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];
   }
}


/*
 // Override to allow orientations other than the default portrait orientation.
 - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
 // Return YES for supported orientations
 return (interfaceOrientation == UIInterfaceOrientationPortrait);
 }
 */

- (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;
}


- (void)dealloc {
   // release all the ones lisited in retain
   [displayString release];
   [displayStringOut release];
   [display release];
   [displayOut release];
   
   [newEvent dealloc];
    [super dealloc];
}

@end

HeadCount.h

#import <Foundation/Foundation.h>


@interface Headcount : NSObject {
   int      totalIn;
   int      totalOut;
   int      maxCapacity;
   
}

@property int totalIn, totalOut, maxCapacity;


@end

Headcount.m

#import "Headcount.h"


@implementation Headcount

@synthesize totalIn, totalOut, maxCapacity;


@end
Logged
skochan
Administrator
Hero Member
*****
Posts: 3103







« Reply #1 on: September 15, 2010, 12:33:02 PM »

Here's an analysis after a cursory look:

This code

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

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.

Or you can create an instance variable in your FliipSideViewController class, set it, and then access its value in your MainViewController class.  Communication can be done through your showInfo: method and needs to be done while the FlipSideViewController is instantiated.

Cheers,

Steve
Logged
RobHartley01
Newbie
*
Posts: 4






« Reply #2 on: September 15, 2010, 10:24:26 PM »

Hi Steve,

Thank you so much for your reply, i really enjoyed your book. I am new to programming and find some concepts hard to understand. However, with the examples and then trying them out on my projects, i find i start to get the 'ahhh'  effect.

However, the process of passing a reference to a method has passed me by. Is there a section in your book (kindle edition) that you would point me to (no pun intended) or could you explain this process a little more for me.

Any help you could give me would be amazing

Thank you

Rob
Logged
RobHartley01
Newbie
*
Posts: 4






« Reply #3 on: September 15, 2010, 10:51:44 PM »

I tried this, but get this error. i know i am doing something basics wrong. I googled passing a reference and it looked liked this.

i added this to my main view controller

-(void) maxProcess: (Headcount*) n
   {
      newEvent.maxCapacity =  n;
   }

on my flipsideviewcontroller i changed it to

-(IBAction) sliderChanged: (id) sender
{
   UISlider *slider = (UISlider *) sender;
   int progressAsInt = (int)(slider.value +0.5f);
   NSString *newText = [[NSString alloc] initWithFormat:@"Max: %d", progressAsInt];
   sliderLabel.text = newText;
   [self maxProcess: progressAsInt];
   [newText release];
}   
 i get the error  flipsideviewcontroller may not respond to maxProcess.

i feel like i am close.... but so far away   Smiley


Logged
skochan
Administrator
Hero Member
*****
Posts: 3103







« Reply #4 on: September 18, 2010, 10:52:10 AM »

So one way to do this is to set an instance variable in your flipside controller (I called it maxValue here) and then access that variable after the controller has been presented but before it is released.  Here would be an example:

Code: (Objective-C)
- (IBAction)showInfo:(id)sender {    
   
   FlipsideViewController *controller = [[FlipsideViewController alloc] initWithNibName:@"FlipsideView" bundle:nil];
   controller.delegate = self;
   
   controller.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;

   [self presentModalViewController:controller animated:YES];

   newEvent.maxCapacity =  controller.maxValue;   // Assumes you've added maxValue as an instance variable to FlipSideViewController
   
   [controller release];
}

Cheers,

Steve
Logged
RobHartley01
Newbie
*
Posts: 4






« Reply #5 on: September 21, 2010, 11:17:36 AM »

Thanks steve, i will make the change. Really appreciate your help. It makes sense now.... until the next error   Smiley
Logged
Pages: [1]   Go Up
Print
Jump to:  



Login with username, password and session length

Powered by MySQL Powered by PHP Powered by SMF 1.1.11 | SMF © 2006-2009, Simple Machines LLC Valid XHTML 1.0! Valid CSS!
Entire forum contents (c) 2009 classroomM.com. All rights reserved.