开发者

if statement not working

开发者 https://www.devze.com 2023-03-10 17:15 出处:网络
I have an app where there is a timer. It goes from 10 to 0. Using NSTimer. I have said: .m: // //Skin.m //PopThatPimple

I have an app where there is a timer. It goes from 10 to 0. Using NSTimer. I have said: .m:

//
//  Skin.m
//  PopThatPimple
//
//  Created by Rohan Kapur on 6/2/11.
//  Copyright 2011 UWCSEA. All rights reserved.
//

#import "Skin.h"


@implementation Skin
@synthesize theAudio;
@synthesize label;


-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    UITouch *myTouch = [touches开发者_如何转开发 anyObject];

    CGPoint point = [myTouch locationInView:pimple];
    if ( CGRectContainsPoint(pimple.bounds, point) ) {
        [self checkcollision];
    }


}




-(void)checkcollision {


    pimple.center = CGPointMake(
                                random() % (unsigned int)theview.bounds.size.width, 
                                random() % (unsigned int)theview.bounds.size.height

                                );

score.text = [NSString stringWithFormat:@"%d", ([score.text intValue] + 1)];



    sad.hidden = NO;

    NSURL* popURL = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"pop" ofType:@"mp3"]];
    AudioServicesCreateSystemSoundID((CFURLRef) popURL, &popID);

    AudioServicesPlaySystemSound(popID);




}




// The designated initializer.  Override if you create the controller programmatically and want to perform customization that is not appropriate for viewDidLoad.
/*
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization.
    }
    return self;
}
*/




-(void)showActivity{

    int currentTime = [time.text intValue];
    int newTime = currentTime + -1;
    time.text = [NSString stringWithFormat:@"%d", newTime];
}

// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
-(void)viewDidLoad {

    myTicker = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector (showActivity) userInfo:nil repeats:YES];

        [label2 setHidden:YES];
    [super viewDidLoad];


}


-(void)void2 {



if ([label3.text isEqualToString:@"0"]) { 
        [[self view] addSubview:timeup];
        score.text = label2.text;
        label2.hidden = NO;

    }

}
/*
// 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 {
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}


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


@end

.h:

#import <UIKit/UIKit.h>
#import <AVFoundation/AVFoundation.h>
#import <AudioToolbox/AudioToolbox.h>


@interface Skin : UIViewController {

    IBOutlet UIImageView *pimple;


IBOutlet UIImageView *smile;
IBOutlet UIImageView *sad;
AVAudioPlayer *theAudio;
IBOutlet UIView *theview;
SystemSoundID popID;
IBOutlet UIImageView *face;
IBOutlet UIImageView *face2;
IBOutlet UIImageView *face3;
IBOutlet UIImageView *face4;
IBOutlet UIImageView *face5;
IBOutlet UIImageView *face6;
IBOutlet UIImageView *face7;
IBOutlet UIImageView *face8;
IBOutlet UIImageView *face9;
IBOutlet UIImageView *face10;
IBOutlet UIImageView *face11;
IBOutlet UIImageView *face12;
IBOutlet UIImageView *face13;
IBOutlet UIImageView *face14;
IBOutlet UIImageView *face15;
    IBOutlet UILabel *label2;
IBOutlet UILabel *score;
IBOutlet UIImageView *skin;

    IBOutlet UILabel *time;
    NSTimer * myTicker;
    IBOutlet UILabel *label3;
    IBOutlet UIImageView * timeup;
}
-(IBAction)start;
-(void)void2;
-(void)showActivity;
@property (nonatomic, retain) UILabel *label;
@property (nonatomic, retain)AVAudioPlayer *theAudio;
-(void)checkcollision;



@end

I have not used all of my outlets and ibactions yet. And i have not used avfoundation yet I have CONNECTED EVERYTHING! I HAVE CHECKEED A THOUSAND TIMES! When it reaches zero nothing happens and the timer just goes to the negatives. WHat am I doing wrong?


There are two major problems here, the first is you're using an assignment operator, not a comparison operator, I'd imagine you have a warning on that.

The second is an == comparison is useless here, the fixed code would be.

if ([label3.text isEqualToString:@"0"])


I'll second (third?) what @BoltClock and @Joshua Weinberg wrote, but add that checking the text displayed in a label is a poor way to determine whether a timer has expired. At some point, somewhere in your program, you're setting the label's text to @"0", so you already know that that condition is true. Likewise with the score... your controller should know what the score is, and it definitely shouldn't be relying on a label to get it.

Don't store data in your view objects. Data should flow from your model through your controller to your view. It only flows in the opposite direction when you're accepting user input.

0

精彩评论

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