can't open view controller

I ran my game. When I wanna go to the game store view controller in simulator, the app crashes. See the code:

.h

@property (weak, nonatomic) IBOutlet UILabel *coinLabel;
@property (weak, nonatomic) IBOutlet UILabel *itemLabel;

.m

int coin;
int itemQty;

- (IBAction)buy:(id)sender {
    coin = coin - 50;
    itemQty = itemQty + 1;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    UILabel* itemLabel = [[UILabel alloc] init];
    itemLabel.text = [NSString stringWithFormat:@"%d", itemQty];

    UILabel* coinLabel = [[UILabel alloc] init];
    coinLabel.text = [NSString stringWithFormat:@"%d", coin];
}

... //I didn't edit anything here

Please help. Thanks

Hi, can you please add crash log here. So developers will get idea what exactly is going wrong ?


And one thing is you have already declare property and outlets for both the labels, then why are you creating references and alloc init in viewdidload. Just use those property by self.

What is the value of "itemQty" when you are asking the ViewDidLoad to execute this:

[NSString stringWithFormat:@"%d", itemQty]

I've sent the crash log but "Currenly being moderated" shown

The itemQty is a "int" variable. And it's the quantity of the "item". I did it like this

itemLabel.text = [NSString stringWithFormat:@"%d", itemQty];

so that the "coinLabel" to show the variable "itemQty".

Yes, you have defined it as an int variable - and then you refer to it as a double, don't you!. But what value does it have? When the view controller is first loaded could it has an undefined value perhaps leading to the crash? Also, within your .m file you want to define your variables within the "interface" like this:


@interface myViewController (){
     int itemQty;
      int coin;
}
@end

@implementation myViewController

- (void)viewDidLoad {  
    [super viewDidLoad];


    itemQty=0:
    coin=0;

    UILabel* itemLabel = [[UILabel alloc] init];  
    itemLabel.text = [NSString stringWithFormat:@"%i", itemQty];  

    UILabel* coinLabel = [[UILabel alloc] init];  
    coinLabel.text = [NSString stringWithFormat:@"%i", coin];  
} 


@end

You're right but I "extern"ed the "int"s from another class. Should I set the value of the variables in that class?

I set the value of the "int"s in another class but app still crashes when opening the view controller. Do you have other solutions? Please help

To debug a program you have to isolate the problem. So first, set the values in view did load ti eliminate that possibility. Then place NSLog statements after each step that could be causing the crash - so set an nslog as the first line in viewdidload to determine if it is crashing before loading. then set an nslog after itemLabel.text= to see if that is causing the crash. Please, do not post each and every step on this forum - do a little work yourself. Thanks.

I understand but do you have sample code? Thanks.

For more information, I got "signal SIGABRT" in main.m

int main(int argc, char * argv[]) {
    @autoreleasepool {
        return UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class]));  // This line
    }
}

Anyone please help?

See this for more description. Please help. Thanks

2017-02-26 16:51:09.363 myApp[15167:1518796] -[UILabel intValue]: unrecognized selector sent to instance 0x7fa63bca56e0
2017-02-26 16:51:09.371 myApp[15167:1518796] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UILabel intValue]: unrecognized selector sent to instance 0x7fa63bca56e0'
*** First throw call stack:
(
  0   CoreFoundation                      0x000000010babdd85 __exceptionPreprocess + 165
  1   libobjc.A.dylib                     0x000000010b11cdeb objc_exception_throw + 48
  2   CoreFoundation                      0x000000010bac6d3d -[NSObject(NSObject) doesNotRecognizeSelector:] + 205
  3   CoreFoundation                      0x000000010ba0ccfa ___forwarding___ + 970
  4   CoreFoundation                      0x000000010ba0c8a8 _CF_forwarding_prep_0 + 120
  5   Foundation                          0x000000010ad78f78 _NSSetIntValueForKeyInIvar + 37
  6   Foundation                          0x000000010ace719b -[NSObject(NSKeyValueCoding) setValue:forKey:] + 288
  7   UIKit                               0x000000010c8acd0c -[UIViewController setValue:forKey:] + 88
  8   UIKit                               0x000000010cae37fb -[UIRuntimeOutletConnection connect] + 109
  9   CoreFoundation                      0x000000010b9f7890 -[NSArray makeObjectsPerformSelector:] + 224
  10  UIKit                               0x000000010cae21de -[UINib instantiateWithOwner:options:] + 1864
  11  UIKit                               0x000000010c8b38d6 -[UIViewController _loadViewFromNibNamed:bundle:] + 381
  12  UIKit                               0x000000010c8b4202 -[UIViewController loadView] + 178
  13  UIKit                               0x000000010c8b4560 -[UIViewController loadViewIfRequired] + 138
  14  UIKit                               0x000000010c8b4cd3 -[UIViewController view] + 27
  15  UIKit                               0x000000010d080024 -[_UIFullscreenPresentationController _setPresentedViewController:] + 87
  16  UIKit                               0x000000010c8845ca -[UIPresentationController initWithPresentedViewController:presentingViewController:] + 133
  17  UIKit                               0x000000010c8c75bb -[UIViewController _presentViewController:withAnimationController:completion:] + 4002
  18  UIKit                               0x000000010c8ca85c -[UIViewController _performCoordinatedPresentOrDismiss:animated:] + 489
  19  UIKit                               0x000000010c8ca36b -[UIViewController presentViewController:animated:completion:] + 179
  20  UIKit                               0x000000010c8cf9ea -[UIViewController _showViewController:withAction:sender:] + 280
  21  UIKit                               0x000000010cd2ec70 __66-[UIStoryboardShowSegueTemplate newDefaultPerformHandlerForSegue:]_block_invoke + 147
  22  UIKit                               0x000000010ce61630 -[UIStoryboardSegueTemplate _performWithDestinationViewController:sender:] + 460
  23  UIKit                               0x000000010ce61433 -[UIStoryboardSegueTemplate _perform:] + 82
  24  UIKit                               0x000000010ce616f7 -[UIStoryboardSegueTemplate perform:] + 156
  25  UIKit                               0x000000010c71ba8d -[UIApplication sendAction:to:from:forEvent:] + 92
  26  UIKit                               0x000000010c88ee67 -[UIControl sendAction:to:forEvent:] + 67
  27  UIKit                               0x000000010c88f143 -[UIControl _sendActionsForEvents:withEvent:] + 327
  28  UIKit                               0x000000010c88e263 -[UIControl touchesEnded:withEvent:] + 601
  29  UIKit                               0x000000010c78e99f -[UIWindow _sendTouchesForEvent:] + 835
  30  UIKit                               0x000000010c78f6d4 -[UIWindow sendEvent:] + 865
  31  UIKit                               0x000000010c73adc6 -[UIApplication sendEvent:] + 263
  32  UIKit                               0x000000010c714553 _UIApplicationHandleEventQueue + 6660
  33  CoreFoundation                      0x000000010b9e3301 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 17
  34  CoreFoundation                      0x000000010b9d922c __CFRunLoopDoSources0 + 556
  35  CoreFoundation                      0x000000010b9d86e3 __CFRunLoopRun + 867
  36  CoreFoundation                      0x000000010b9d80f8 CFRunLoopRunSpecific + 488
  37  GraphicsServices                    0x0000000110085ad2 GSEventRunModal + 161
  38  UIKit                               0x000000010c719f09 UIApplicationMain + 171
  39  myApp                   0x000000010ac0f4bf main + 111
  40  libdyld.dylib                       0x000000010eb3d92d start + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException
(lldb)

I found out in internet that maybe I don't outlet is not connected well or not connected. Can anyone explain how the outlet will be connected unwell and why it will crash when trying to open that view controller if outlet is not connected well. How to connect it well? If nyone think the reason is not because of the button not connect well and has another reason, please let me know. And give me the solutions. I've tried to fix it in more than a week. Please help. Thanks

I've deleted the alloc init of the UILabels but my app still crashes. It is because of signal SIGABRT. You have any idea? Please help me. Thank you very much.

Lots of things could be wrong. You referred to a "button". Are you connecting a button in Storyblard to a UILabel?

can't open view controller
 
 
Q