ios - 在 Tabbar 应用程序上将 UILabel 的值从 FirstView 传递到 SecondView

标签 ios uilabel uitabbar tabbarcontroller

我正在尝试制作一个从气象站获取数据的应用程序。它是一个基于标签栏的应用程序。 我有两个 View Controller :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) 传递到基于标签栏的应用程序中的另一个 View ?我在哪里犯错? 谢谢

最佳答案

在你的代码中

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

希望对你有帮助。

关于ios - 在 Tabbar 应用程序上将 UILabel 的值从 FirstView 传递到 SecondView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16298260/

相关文章:

iOS 7 - 游戏中心排行榜整合

ios - 如何找到 UILabel 的现有 UIFontTextStyle?

ios - Swift 4 - 标签栏项目上的按钮

IOS:如何将UITabbar的位置更改为 View 顶部

captureOutput 中的 iOS 应用相机方向

ios - 如何在 pagecontrol 的 ScrollView 中将图像应用到 imageView。

ios - 提高 objective-c 中的搜索性能

swift - Segue 在传递数据时移动得太快?

ios - 如何阻止 UILabel 将 "..."替换为省略号

ios - 在 iOS 6 中隐藏 TabBar