0

我正在尝试制作一个从气象站获取数据的应用程序。它是一个基于标签栏的应用程序。我有两个视图控制器:FirstViewController 和 SecondViewController。

在 FirstViewController 中,我完成了大部分工作。使用来自 xml 解析的值并将这些值分配给 UILabel。那工作正常。我正在使用 if() 来根据风的角度为 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 并希望它从FirstViewController获取labelWindDirectionName.text的值。

我的 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)传递到基于标签栏的应用程序中的另一个视图?我在哪里犯错?谢谢

4

3 回答 3

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"];

希望它可以帮助你。

于 2013-04-30T11:11:50.973 回答
1

获取您的viewController对象正确并放置

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

If you are pushing one View Controller in the same tab then go for that approach. Since you said the previousController.. So, try this it will help

For TabBar if you want then

for(id object in [(UINavigationController *)self.tabBarController.selectedViewController viewControllers])
{
   if([object isKindOfClass:[FirstViewController class]])
    {
          FirstViewController *obj = (FirstViewController * ) object;
          obj.labelWindDirectionName.text = @"Something";
          break;
     } 
}
于 2013-04-30T11:14:30.147 回答
0

尝试将 SecondViewController 的 NSString 属性设置为所需的值,然后在 SecondViewController 的 viewDidLoad 方法中使用该属性的值设置标签文本。

于 2013-04-30T11:12:41.753 回答