如果声明不起作用
我有一个应用程序,其中有一个计时器。它从10变为0.使用NSTimer。我曾经说过: .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
我没有用我所有的网点和ibactions呢。我还没有使用avfoundation 我已经连接了一切!我已经检查了一千次!当它达到零时,没有任何事情发生,计时器只是到了底片。我究竟做错了什么?
这里有两个主要的问题,第一个是你使用赋值运算符,而不是比较运算符,我想你会有一个警告。
第二个是==
比较在这里没用,固定代码就是了。
if ([label3.text isEqualToString:@"0"])
我会第二(三?)什么@BoltClock和@Joshua温伯格写道,但补充说,检查在标签中显示的文本是一个贫穷的方法来确定一个定时器是否已经到期。在某个时间点,在您的程序中某处,您将设置为标签的文本为@“0”,因此您已经知道该条件为真。同样与得分...你的控制器应该知道得分是什么,它绝对不应该依靠一个标签来得到它。
不要在您的视图对象中存储数据。数据应该从您的模型通过您的控制器流向您的视图。它只在接受用户输入时以相反的方向流动。
这是真的,但有一些基本问题要处理第一... – 2011-06-05 02:11:18
我不undrstand .... – user722566 2011-06-05 02:11:19
我该怎么做,我应该把它连接到一个nsobject? – user722566 2011-06-05 02:12:29
我对此没有任何警告 – user722566 2011-06-05 02:02:46
生病检查它是否有效 – user722566 2011-06-05 02:02:53
您使用的编译器是什么? – 2011-06-05 02:02:56