2013-03-20 147 views
2

我有一點問題,因爲我無法將數據傳遞給另一個視圖控制器。無法將數據傳遞給其他viewController ios

-(void)barcodeData:(NSString *)barcode type:(int)type 
{ 
    mainTabBarController.selectedViewController=self; 

    [status setString:@""]; 

    [status appendFormat:@"Type: %d\n",type]; 
    [status appendFormat:@"Type text: %@\n",[dtdev barcodeType2Text:type]]; 
    [status appendFormat:@"Barcode: %@",barcode]; 
    [displayText setText:status]; 
    codede1String = barcode; 
    NSLog(@"%@",codede1String); 


    [self updateBattery]; 
} 

在上面我的代碼把條形碼在String codeString1。到目前爲止沒有問題,但是當我將這個數據發送給另一個viewController時,barCodeString突然變爲NULL。

我將數據傳遞給視圖控制器是這樣的:

-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{ 
    if ([segue.identifier isEqualToString:@"barcode"]) { 
     main =[ segue destinationViewController]; 
     main.code1.text = codede1String; 


    } 

} 

那麼,如何解決這個問題?

+1

試着在調試器的幫助下理解你的代碼。 – dasdom 2013-03-20 15:26:31

回答

0

在視圖控制器的viewDidLoad中做到這一點,那麼它工作正常

- (void)viewDidLoad 
{ 

codedeString=[[NSString alloc] init]; 
} 
0

我假設code1是一個文本字段,文本視圖或標籤。如果是這樣,這是行不通的原因是主要的網點尚未在prepareForSegue時間設置。您需要在main中創建一個字符串屬性,並將其值設置爲prepareForSegue中的code1String。然後,在main的viewDidLoad或viewDidAppear中,將code1的文本值設置爲該字符串。

0

請注意,目標視圖控制器的IBOutlets尚未在prepareForSegue中設置。因此,如果main.code1代表UILabel,則它將爲零,因爲視圖尚未加載。您應該使用setter方法直接傳遞字符串:[main setCodeString:codede1String],然後在viewDidLoadself.code1.text = [self codeString]中配置用戶界面。

+0

code1.text是一個文本框 我試過這個[main setBarcodeString:codede1String];並在viewDidLoad中的其他視圖控制器中,我做到了這一點self.code1.text = [self barcodeString];但問題是理智的 – Thymen 2013-03-20 16:17:58

0

嘗試這樣做,幫我一次,同樣有點問題(但xibs)

再添加一個線,就在任何作業之前

[main.view setAlpha:1.0f]; 
main.code1.text = codede1String; 

希望這對你也有幫助。

相關問題