2014-05-06 58 views
0

目標是枚舉字符串中的每個字符。這就是'w'出現後1秒後會被'h'替換爲'e',然後是'n'。 問題我的標籤只顯示最後一個字母。UILabel不會顯示前面的字符,只顯示最後一個字母

我做了什麼: 1.在PlayViewController上拖動一個標籤和一個按鈕。 2.創建屬性標籤 3.按鈕

在我PlayViewController.h創建行動:

#import <UIKit/UIKit.h> 

@interface PlayViewController : UIViewController 

@property (strong, nonatomic) IBOutlet UILabel *listChar; 

- (IBAction)startGame:(id)sender; 

@end 

在我PlayViewController.m:

#import "PlayViewController.h" 

@interface PlayViewController() 

@end 

@implementation PlayViewController 

NSString *word; 
NSTimer *myTimer; 

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    // Do any additional setup after loading the view. 
    self.listChar.text = @" "; 
    word = @"when"; 

} 

- (IBAction)startGame:(id)sender { 
    myTimer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(listLetter:) userInfo:nil repeats:YES]; 
} 


-(void) listLetter:(NSTimer*) myTimer { 
    for(int i=0; i<[word length]; i++){ 
     unichar letter = [word characterAtIndex: i]; 
     self.listChar.text = [NSString stringWithFormat:@"%C", letter]; 
    } 
} 

@end 

回答

0

如果你想知道關於爲什麼這個問題,然後參考@尼廷的答案,如果你想以其他方式解決,那麼只需使用以下代碼

-(void) listLetter:(NSTimer*) myTimer 
    static int pos = 1; 
    self.listChar.text = [word substringToIndex:pos]; 
    if (++pos > word.length) { 
     [myTimer invalidate]; 
    } 
} 
+0

再次感謝。無論如何,我做了一些調整。我的目標是隻有一個字母會閃爍在屏幕上。用戶只會看到w,然後w將被h替換,等等。 - (void)listLetter:(NSTimer *)timer { static int i = 0; unichar letter = [單詞characterAtIndex:i]; self.listChar.text = [NSString stringWithFormat:@「%C」,letter];如果(++ i == word.length){ [timer invalidate]; } } – user3266210

+0

那麼你的問題是什麼?你的代碼中的 – iPatel

+0

,整個單詞將被拼寫。 w會出現然後wh然後當時。只是做了一些調整,再次tnx。 – user3266210