2013-04-30 17 views
0

我試圖製作一個從氣象站獲取數據的應用程序。這是一個基於tabbar的應用程序。 我有兩個viewcontrollers:FirstViewController和SecondViewController。將UILabel的值從FirstView傳遞到Tabbar應用程序的SecondView

在FirstViewController中,我正在完成大部分工作。使用來自xml解析並將這些值分配給UILabels的值。這工作正常。 我使用的,如果(),以便根據其角度想要分配的UILabel風的名字:

在我FirstViewController.m

-(void)setWindDirectionName 
{  
    if(windDirectionAngle >= 0 && windDirectionAngle <=11.25f) labelWindDirectionName.text = @"N"; 
    if(windDirectionAngle > 11.25f && windDirectionAngle <=33.75f) labelWindDirectionName.text = @"NNE"; 
    if(windDirectionAngle > 33.75f && windDirectionAngle <= 56.25f) labelWindDirectionName.text = @"NE"; 
    if(windDirectionAngle > 56.25f && windDirectionAngle <=78.75f) labelWindDirectionName.text = @"ENE"; 
    if(windDirectionAngle > 78.75f && windDirectionAngle <=101.25f) labelWindDirectionName.text = @"E"; 
    . 
    . 
    . so on 

在SecondViewController我的UILabel和希望它取值labelWindDirectionName.textFirstViewController

我SecondViewController.h

@interface SecondViewController : UIViewController <CLLocationManagerDelegate> 
{ 
    NSString *windName; 
    } 
    @property (strong,nonatomic) NSString *windName; 
    @property (strong, nonatomic) IBOutlet UILabel *labelCompassViewWindDirectionName; 

    @end 

和SecondViewController.m

FirstViewController *fvc = [[FirstViewController alloc]init]; 
windName = fvc.labelWindDirectionName.text; 
labelCompassViewWindDirectionName.text = windName;//getting null probably making completely new object; 

怎麼可能到數據(UILABEL.TEXT)傳遞到另一個視圖中的TabBar基於應用?我在哪裏犯錯? 感謝

+0

FirstViewController * FVC = [[FirstViewController的alloc]初始化]每次初始化...所以它會使全新的對象.....獲取第一個視圖導航控制器對象,然後分配.....好:) – 2013-04-30 11:11:00

回答

1

在這裏,你的代碼

FirstViewController *fvc = [[FirstViewController alloc]init]; 

你分配FirstViewController的新實例,所以它會返回null。如果您需要訪問這些名稱,則可以將其存儲在NSUserDefaults中,然後可以隨時訪問。

例如:在FirstViewController: 保存名稱和他人:

[[NSUserDefaults standardUserDefaults] setObject:labelWindDirectionName.text forKey:@"windName"];//Note : Here windName is a key so you can save diffrent data using diffrent keys and same way can access those data using these keys whereever you require. 
    [[NSUserDefaults standardUserDefaults] synchronize]; 

,並在SecondViewController使用訪問該

labelCompassViewWindDirectionName.text = [[NSUserDefaults standardUserDefaults] objectForKey:@"windName"]; 

希望它可以幫助你。

+0

謝謝..這工作... – littleblue 2013-04-30 12:50:34

0

嘗試將SecondViewController的NSString屬性設置爲期望的值,然後在SecondViewController的viewDidLoad方法中使用此屬性的值設置標籤文本。

1

讓您viewController對象Correclty和地點

FirstViewController *fvc = (FirstViewController *)[self.navigationController.viewControllers objectAtIndex:([self.navigationController.viewControllers count] -1)]; 
fvc.labelWindDirectionName.text = @"Something"; 

如果您在同一標籤推動一個視圖控制器,然後去這種做法。 既然你說過previousController ..所以,試試這個它會幫助

有關的TabBar如果你想那麼

for(id object in [(UINavigationController *)self.tabBarController.selectedViewController viewControllers]) 
{ 
   if([object isKindOfClass:[FirstViewController class]]) 
    { 
      FirstViewController *obj = (FirstViewController *) object; 
      obj.labelWindDirectionName.text = @"Something"; 
          break; 
     } 
} 
+0

OP有一個TabBarBased應用程序所以這可能不起作用。 – Anupdas 2013-04-30 11:17:16

+0

只需檢查一下我編輯過的答案!!!!! @Anupdas – 2013-04-30 11:35:53

+0

現在你假設selectedViewController是一個NavigationController。如果知道它會工作正常。 – Anupdas 2013-04-30 11:45:01

相關問題