开发者

Whats wrong with this line?

开发者 https://www.devze.com 2023-03-25 13:49 出处:网络
My app crashes when I press the \"own\" button. I don\'t know what\'s wrong with my code. I\'m new to this, so if I totally screwed up, don\'t be to hard please :). EDIT: This is what Xcode says:

My app crashes when I press the "own" button. I don't know what's wrong with my code. I'm new to this, so if I totally screwed up, don't be to hard please :). EDIT: This is what Xcode says:

    #1  0x000144fd in -[UIApplication sendAction:to:from:forEvent:] ()

The app crashes on the line:

   if (thing.hidden == NO) {

This is the code that belongs t开发者_如何学JAVAo "own":

    - (IBAction)own {

if (thing.hidden == NO) {

int rNumber = rand() % 4;

switch (rNumber) {
    case 0:
        result.text  = @"A";
        break;
    case 1:
        result.text = @"B";
        break;
    case 2:
        result.text = @"C";
        break;
    case 3:
        result.text = @"D";
        break;
    default:
        break;
}

}
if (thing.hidden == YES) {
    int rNumber = rand() % 3;


There may be several rason

IBAction is actually just a hint that tells interface builder where to find the methods in your objects so you can link controls to methods.

In iOS, actions can take zero, one or 2 parameters.

If one parameter, that parameter is the object sending the message:

-(IBAction) someAction: (id) sender;

If two parameters, it takes the form:

- (IBAction) someAction:(id) sender forEvent: (UIEvent*) event;

you can use -(void)own and connect to your button in nib file


Nothing seems wrong in your code, assuming that the objects thing and result are retained properly.


- (IBAction)own {

if (thing.hidden == NO) {

int rNumber = rand() % 4;
NSString *myText = @""; // 
switch (rNumber) {
    case 0:
        myText  = @"A";
        break;
    case 1:
        myText = @"B";
        break;
    case 2:
       myText = @"C";
       break;
    case 3:
       myText = @"D";
       break;
   default:
       break;
}
result.text = myText;
}
if (thing.hidden == YES) {
    int rNumber = rand() % 3;


I just met this question. It's because of the memory management, I send the button related action to a released viewController. Hope this help.(BTW,I use the arc mode)

0

精彩评论

暂无评论...
验证码 换一张
取 消