2015-02-23 21 views
0

我想在循環中動態更新UITextField中的文本,並且文本不會實時顯示。當循環完成時,它將顯示最終值。每次在循環中更新時如何顯示文本?我在桌面視圖中發現了許多線程更新,但這個文本字段只是在主應用程序。 這是一個帶有文本框和按鈕的簡單應用程序。如何在循環中以編程方式更新UITextField中的文本

在ViewController.h

#進口

@interface ViewController : UIViewController<UITextFieldDelegate> 
@property (weak, nonatomic) IBOutlet UITextField *myText; 
@property (weak, nonatomic) IBOutlet UIButton *myButton; 
- (IBAction)pressMe:(UIButton *)sender; 
@end 
在ViewController.m #進口 「ViewController.h」

@interface ViewController() 

@end 

@implementation ViewController 
@synthesize myButton,myText; 

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

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

- (IBAction)pressMe:(UIButton *)sender { 

    for (int i=0;i<10;i++){ 
     NSString *m = [[NSString alloc] initWithFormat:@"%d",i]; 
     NSLog(@"%@",m); 
     [myText setText:m]; 
     [NSThread sleepForTimeInterval:.05]; 
    } 
} 
@end 

的texfield代表連接到視圖控制器。這是一個簡單的應用程序來說明我正在嘗試做什麼。我的實際應用程序正在嘗試從gps到屏幕持續更新顯示緯度。 任何幫助表示讚賞。

+0

基本上發生的事情是,你正在運行在主線程的循環,同樣的一個負責更新用戶界面。因爲線程忙於運行循環,所以無法更新UI。一旦你的pressMe:消息完成,UI將被更新。如果要在循環的每一步更新UI,它需要在單獨的線程上運行並調用UI重新加載到主線程。 – 2015-02-23 13:41:41

+0

用示例代碼編輯的答案。 – n00bProgrammer 2015-02-24 06:42:48

回答

0

您應該在不同的線程中使用dispatch_async(dispatch_get_main_queue(), ^{ });進行UI更新。 [文本框setNeedsDisplay]也可以工作,如果你已經在主線程

1

如果你的目的是要更新與GPS座標的字段中的文本,我建議設置在CLLocationManagerDelegate方法locationManager:didUpdateLocations:文本(假設你使用CLLocationManager跟蹤用戶位置)。此方法在更新位置時調用。這裏不需要GCD。

示例代碼:

- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations { 

    CLLocation *mostRecentLocation = [locations lastObject]; 

    latitudeTextField.text = [NSString stringWithFormat:@"%f", mostRecentLocation.coordinate.latitude]; 
    longitudeTextField.text = [NSString stringWithFormat:@"%f", mostRecentLocation.coordinate.longitude]; 
} 
0

嘗試這一次..

-(IBAction)btnPress:(id)sender 
{ 

    for (int i=0;i<10;i++){ 
     NSString *m = [[NSString alloc] initWithFormat:@"%d",i]; 
     NSLog(@"%@",m); 

     [NSThread detachNewThreadSelector:@selector(call:) toTarget:self withObject:m]; 
     sleep(1); 
    } 
} 

-(void)call:(NSString *)str 
{ 
    _myText.text = str; 
} 
+0

這應該工作! – 2015-02-23 13:44:25

+0

在主線程上調用按鈕按鈕,並且您正在更新輔助線程上的UI。取而代之的是按下按鈕,你應該在後臺線程和睡眠上運行循環,並更新主線程上的UI。 – gagarwal 2015-02-24 06:58:58

+0

爲什麼此評論被選中投票?可以有解釋嗎? – gagarwal 2015-02-24 16:18:22

0
@interface ViewController() 
{ 
    NSTimer *timer; 
    NSInteger i; 
} 
@end 

- (void)viewDidLoad { 
    [super viewDidLoad]; 

    i=0;  

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


    myText.delegate = self; 
} 

- (IBAction)StopUpdate:(UIButton *)sender { 
    [timerinvalidate] 
} 
-(void)targetMethod{ 
    NSString *m = [[NSString alloc] initWithFormat:@"%d",i]; 
    i++; 
    [myText setText:m]; 
} 
+0

Plz。創建一個按鈕並設置他們的動作「StopUpdate」。此方法用於停止更新。 – 2015-02-23 17:07:32

0
-(IBAction)btnPressed:(id)sender 
{ 
dispatch_queue_t Queue = dispatch_queue_create("",0); 
    dispatch_async(Queue, ^{ 
     for (int i=1 ; i<=10;i++) { 
      sleep(1); 
      dispatch_async(dispatch_get_main_queue(),^{ 

       myTxtFld.text = [NSString stringWithFormat:@"%d",i]; 
      } 
      ); 
     } 
    }); 
} 
相關問題