2014-10-08 69 views
-1

我正在製作一個非常簡單的應用程序。我有一個計時器,當計時器超過5秒時,我想要顯示一個圖像。如果計時器不超過5秒,則圖像應保持隱藏狀態。問題是,我的圖像出現在我的main.storyboard上,一旦定時器超過5秒,我似乎無法使其消失。任何幫助,將不勝感激!這裏是我的代碼:在Xcode中隱藏和顯示圖像

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是說它「預期的方法體」。我一直在環顧四周,無法找到解決這個問題,任何人都可以幫我解決這個錯誤?

+1

即使代碼不能編譯,圖像仍然出現?這是令人驚訝的。與c中一樣,objective-c代碼使用{花括號}分隔。這是viewDidLoad的問題。 – danh 2014-10-08 02:12:55

+0

您的代碼質量確實很差 - 您的分號會導致不需要的行爲。你也不應該重複相同的路線。 – Szu 2014-10-08 08:32:40

回答

1

首先更改下面的代碼在方法「重新啓動」和「UIImage的」

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

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

當我想你是編程新手這裏提示:約定是方法名稱總是寫得很小,所以在你的代碼中應該是「重啓」/「開始」而不是「重啓」/「開始」。第二,如果我理解你的代碼是正確的,那麼你在概念上犯了一個錯誤。由您的計時器調用的方法是TimerCount。您必須將每個UI功能放在那裏或從那裏調用方法。這是每秒鐘被調用的唯一方法。所以你應該把

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

檢查那裏。

+0

謝謝!是的,我是編程新手,我14歲,上週開始學習。謝謝您的幫助。 – Pearl 2014-10-13 22:59:39

0

您收到[super viewDidLoad];的錯誤,因爲您忘記了護腕。