在Xcode中隐藏和显示图像

时间:2014-10-08 01:28:01

标签: ios objective-c

我正在制作一个非常简单的应用程序。我有一个计时器,当计时器超过5秒时,我想要显示一个图像。如果计时器不超过5秒,图像应保持隐藏状态。问题是,我的图像出现在我的main.storyboard上,一旦计时器超过5秒,我似乎无法让它消失。任何帮助,将不胜感激!这是我的代码:

ViewController.h:

#import <UIKit/UIKit.h>
int CountNumber;
@interface ViewController : UIViewController
{
    IBOutlet UILabel *TimerDisplay;
    IBOutlet UIImageView *Images;
    NSTimer *Timer;
}
-(void)TimerCount;
-(IBAction)Start:(id)sender;
-(IBAction)Stop:(id)sender;
-(IBAction)Restart:(id)sender;

@property (strong, nonatomic) IBOutlet UILabel *FirstiPhoneapp;
@property (strong, nonatomic) IBOutlet UILabel *ByJ;
@property (strong, nonatomic) IBOutlet UIButton *ClickToStart;
@property (strong, nonatomic) IBOutlet UIButton *Reset;
@property (strong, nonatomic) IBOutlet UIButton *Terminate;
@property (strong, nonatomic) IBOutlet UIButton *Restart;

@end

ViewController.m:

#import "ViewController.h"

@interface ViewController ()
@end

@implementation ViewController

-(IBAction)Start:(id)sender
{
    Timer = [NSTimer scheduledTimerWithTimeInterval:1 target: self selector:@selector(TimerCount) userInfo: nil repeats:YES];
}

- (void)TimerCount {
    CountNumber = CountNumber + 1;
    TimerDisplay.text=[NSString stringWithFormat:@"%i", CountNumber];
}

- (IBAction)UIImage:(id)sender {
    self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage    imageNamed:@"Images.jpg"]];
    if (CountNumber < 6);
    Images.hidden = YES;
}

- (IBAction)Stop:(id)sender{
    [Timer invalidate];
    TimerDisplay.text=[NSString stringWithFormat:@"%i", CountNumber]; 
}

- (IBAction)Reset:(id)sender{
    CountNumber = 0;
    [Timer invalidate];
    CountNumber = 0;
    [Timer invalidate];
    TimerDisplay.text = [NSString stringWithFormat:@"%i", CountNumber];

    Timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(TimerCount) userInfo:nil repeats:YES];              
    [Timer invalidate];       
}

- (IBAction)Restart:(id)sender{
    CountNumber = 0;
    TimerDisplay.text = [NSString stringWithFormat:@"%i", CountNumber];
    CountNumber = 0;
    Timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(TimerCount) userInfo:nil repeats: YES];

    if (CountNumber < 6);
    Images.hidden = YES;
}

- (void)viewDidLoad
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
@end

另外,出于某种原因,对此有任何帮助会有所帮助,我在[super viewDidLoad];上收到错误Xcode称它是“预期的方法体”。我一直在环顾四周,无法找到解决方案,任何人都可以帮我解决这个错误吗?

2 个答案:

答案 0 :(得分:1)

首先在方法&#34;重启&#34;中更改以下代码和&#34; UIImage&#34;

if (CountNumber < 6);
Images.hidden = YES;

if (CountNumber < 6){
   Images.hidden = YES;
}

而且我认为你在这里编程的新手一个提示:惯例是方法名称总是写得很小,所以在你的代码中它应该是&#34; restart&#34; /&#34;开始&#34;而不是&#34;重新启动&#34; /&#34;开始&#34;。

第二,如果我理解你的代码是正确的,你在你的概念中犯了一个错误。您的计时器调用的方法是TimerCount。您必须将每个UI功能放在那里或从那里调用方法。这是每秒调用的唯一方法。

所以你应该这样做
if (CountNumber < 6){
       Images.hidden = YES;
}
检查那里。

答案 1 :(得分:0)

您在[super viewDidLoad];收到错误,因为您忘记了护腕。