Program quits randomly

Discussion in 'iOS Development' started by Old_Man_Willow, Jan 18, 2010.

  1. Old_Man_Willow

    Old_Man_Willow New Member

    Joined:
    Aug 7, 2009
    Messages:
    9
    Likes Received:
    0
    Device:
    iPod touch
    I'm writing just a little calculator app with a label as the output and a few buttons for input. Here's my problem: When I launch the simulator, it works fine, but after clicking 2-3 buttons, it just quits. Here's my (incomplete) code:

    [OBJC]
    @interface CalculatorViewController : UIViewController {
    UILabel *calcDisplay;
    BOOL isFirstNumber;
    NSString *firstNumber;
    NSString *secondNumber;
    NSString *operation;
    NSString *answer;
    }

    @property (nonatomic, retain) IBOutlet UILabel *calcDisplay;

    -(IBAction) button1;
    -(IBAction) button2;
    -(IBAction) button3;
    -(IBAction) button4;
    -(IBAction) button5;
    -(IBAction) button6;
    -(IBAction) button7;
    -(IBAction) button8;
    -(IBAction) button9;

    @implementation CalculatorViewController

    -(IBAction) button1{
    if (isFirstNumber == YES)
    {
    if (calcDisplay.text == @" 0")
    {
    calcDisplay.text = @" 1";
    firstNumber = @"1";
    }
    else
    {
    calcDisplay.text = [calcDisplay.text stringByAppendingString

    Please Register or Log in to view images

    "1"];
    firstNumber = [firstNumber stringByAppendingString

    Please Register or Log in to view images

    "1"];
    }
    }
    else
    {
    ;
    }
    }
    // All of the other buttons are the same, but with different numbers

    - (void)viewDidLoad {
    [super viewDidLoad];
    calcDisplay.text = @" 0";
    isFirstNumber = YES;
    }
    [/OBJC]
  2. Axis

    Axis Super Moderator Staff Member

    Joined:
    Dec 2, 2007
    Messages:
    6,288
    Likes Received:
    133
    Device:
    iPhone 4S (White)
    Please post the error message or crash log.

    Also, you should have a better means of handling UI events than 9 separate methods. Why not have one method that acts dependent on the button clicked.

    [OBJC]
    enum ButtonTypes {
    kButton1 = 0,
    kButton2,
    kButton3
    //etc.
    };

    - (IBAction)buttonClicked

    Please Register or Log in to view images

    id)sender {

    switch ([(UIButton *)sender tag]) {

    case kButton1 : /* handle button 1 clicked event here */ break;
    case kButton2: /* handle button 2 clicked event here */ break;

    // etc.

    }

    }
    [/OBJC]

    You should set the 'tag' property of each button to reflect some int constants (like I did), or just keep them in order.
  3. Old_Man_Willow

    Old_Man_Willow New Member

    Joined:
    Aug 7, 2009
    Messages:
    9
    Likes Received:
    0
    Device:
    iPod touch
    It doesn't print an error message, it just quits. I cant explain it.
    ------------------double post merged------------------
    Okay, I fixed it. I needed to retain my firstNumber string
  4. lauNchD

    lauNchD Well-Known Member

    Joined:
    Jan 27, 2008
    Messages:
    1,844
    Likes Received:
    261
    Device:
    iPhone 5 (Black)
    It looks like you didn't retain your strings. If you just use -stringByAppendingString it returns an autoreleased object. Synthesized properties would make your life easier.
    (BTW: You could extend Axis' "flexibility" even further by taking the number directly from the button's tag, and making the "dot" tag 128 or something)

    EDIT: To elaborate, the first time your app won't crash since you used constant @"strings" which never get deallocated. I think firstNumber is getting your app to crash.

    EDIT 2: Self fail. Sorry, I didn't realize you already posted the answer!

Share This Page