简体   繁体   English

非常基本的Objective-C问题-如何使不同的观点共享信息?

[英]Very basic objective-c question-how do I make different views share information?

So, basically, I have four different views that are switched via a tab bar at the bottom of the screen. 因此,基本上,我有四个不同的视图,这些视图通过屏幕底部的选项卡栏进行切换。 I have a view controller set up for each one, and they all work fine. 我为每一个设置了一个视图控制器,它们都工作正常。 The only problem is that I have no idea how to share information between the different views-for example, I have an IBAction on one that takes the output of a UISegmentedControl and stores the selection as an integer (1,2,3,4). 唯一的问题是我不知道如何在不同的视图之间共享信息,例如,我在一个视图上有一个IBAction,它接受UISegmentedControl的输出并将所选内容存储为整数(1,2,3,4) 。 The problem is, I don't know how to access that integer from the first view controller (the one that actually does stuff with that information). 问题是,我不知道如何从第一个视图控制器(实际使用该信息填充内容的控制器)访问该整数。 I'm sure this is a very basic problem but my google-fu doesn't seem to be working here. 我敢肯定这是一个非常基本的问题,但是我的google-fu似乎在这里没有工作。 Thanks! 谢谢!

Using the MVC method, information should not be stored in the view controllers. 使用MVC方法,信息不应存储在视图控制器中。 You should have a separate object which stores the information, the view controllers load it, and the views display it. 您应该有一个单独的对象来存储信息,视图控制器将其加载,然后视图将其显示。 The easiest way to do this would be to store the information in your application's delegate. 最简单的方法是将信息存储在应用程序的委托中。 Then, whenever the view controller needs to find/change some information, it can use [[UIApplication sharedApplication] delegate] to get the delegate, and load/change the information as needed. 然后,每当视图控制器需要查找/更改某些信息时,它都可以使用[[UIApplication sharedApplication] delegate]获取委托,并根据需要加载/更改信息。 Load the information to update the display in viewDidLoad or viewWillDisplay:, and change it in action methods. 加载信息以在viewDidLoad或viewWillDisplay:中更新显示,并在操作方法中进行更改。

Edit Example: 编辑示例:

DelegateClass.h DelegateClass.h

@interface DelegateClass : NSObject {
    //ivars
    float number1;
}
@property (assign) float number1;
...
@end

DelegateClass.m 代表类

#import "DelegateClass.h"
@implementation DelegateClass
@synthesize number1;
...
@end

MyViewController.m MyViewController.m

#import "DelegateClass.h"
@implementation MyViewController
- (void)viewDidLoad {
    [super viewDidLoad];
    DelegateClass *delegate = [[UIApplication sharedApplication] delegate];
    float number1 = delegate.number1;
    ...
}
- (IBAction)actionMethod:(id)sender {
    float number1 = sender.floatValue;//get new value
    ((DelegateClass*)[[UIApplication sharedApplication] delegate]).number1 = number1;
}
...
@end

You could have your views expose their variables to the UITabBarController . 您可以让视图将其变量公开给UITabBarController The UITabBarController can have methods that these views can call to see these variables. UITabBarController可以具有这些视图可以调用以查看这些变量的方法。

Or, you could have your views directly set the values of variables in the UITabBarController . 或者,您可以让视图直接在UITabBarController设置变量的值。 Not recommended (though a lot less typing :) 不推荐(尽管打字少很多:)

Just did a quick search on Google, btw, and came across this: Accessing parent view controller (custom) properties . 顺便说一句,刚在Google上进行了快速搜索,并发现了这一点: 访问父视图控制器(自定义)属性 Looks like there's sample code to be had there. 看起来那里有示例代码。 :) :)

Hope this helps! 希望这可以帮助!

I use a general theme in all my apps where I have certain data that needs to be accessed practically in all main classes. 我在所有应用程序中都使用通用主题,在该应用程序中,某些数据实际上需要在所有主类中进行访问。 I find it really easy to use and practically error free. 我发现它非常易于使用,几乎没有错误。 (Thanks again to all the folks here in the forum for the super idea!) (再次感谢论坛中所有与会人员,以提供超级创意!)

I declare this variable in all the classes where I need it... 我在需要的所有类中声明此变量...

  @class WebiDataManager;

  @interface FirstViewController : UIViewController<MFMessageComposeViewControllerDelegate, SetupDelegate>  {
        WebiDataManager         *webiDataManager;
    ...

In this code I get it from the AppDelegate... 在这段代码中,我从AppDelegate获得了它。

#import "WebiAppDelegate.h"

 - (void)viewWillAppear:(BOOL)animated {
        //  D_IN;
        [super viewWillAppear:animated];


        //get the dataManager global Object, so we always have a structured accesss to the data!
        WebiAppDelegate *mainDelegate = (WebiAppDelegate *)[[UIApplication sharedApplication]delegate];
        self.webiDataManager = mainDelegate.webiDataManager;

By the way, this also works great for coredata, since through this global variable I always have access to all the main coredata. 顺便说一句,这对于coredata也很有效,因为通过此全局变量,我始终可以访问所有主要coredata。

Hope I could explain it clearly enough... 希望我能解释得足够清楚...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM