iOS:在视图之间传递数据

发布于 2025-01-02 02:08:27 字数 1497 浏览 3 评论 0原文

我有两个以编程方式创建的视图。我们将它们命名为 view1 和 view2。在 view1 中有一个选择器和一个按钮。这个想法是当用户选择值并按下按钮时,所选值可以从 view2 访问。为此,我使用 NSNotificationCenter。这是一些代码。

view1.m

-(void)registerNotification
{
    NSDictionary *dict = [NSDictionary dictionaryWithObject:self.selectedOption forKey:@"data"];

    [[NSNotificationCenter defaultCenter]
     postNotificationName:@"pickerdata" 
     object:self
     userInfo:dict];
}

-(void)loadSecondView
{
    self.secondView = [[secondViewController alloc]init];
    [self.view addSubview:self.secondView.view];
    [self registerNotification];
    [self.secondView release];
}

view2.m

-(id)init
{
   if(self = [super init])
   {
    [[NSNotificationCenter defaultCenter] 
         addObserver:self 
         selector:@selector(reciveNotification:) 
         name:@"pickerdata" object:nil];
   }
   return self;
}


-(void)reciveNotification:(NSNotification *)notification
{
    if([[notification name] isEqualToString:@"pickerdata"])
    {
        NSLog(@"%@", [NSString stringWithFormat:@"%@", [[notification userInfo] objectForKey:@"data"]]); // The output of NSLog print selected value

       // Here is assignment to ivar
        self.selectedValue = [NSString stringWithFormat:@"%@", [[notification userInfo] objectForKey:@"data"]];
    }
}

问题从这里开始。对该值感兴趣的逻辑在 loadView 方法中实现。问题在于 loadView 在 receiveNotification 方法之前执行,并且 selectedValue 尚未包含所需信息。 如何才能从 NSNotificationCenter 提供的信息可以通过 loadView 方法访问?

I have two views which are created programmatically. Let's name them view1 and view2. In view1 there is a picker and a button. The idea is when the user choose value and press the button selected value to be accessable from view2. For this I use NSNotificationCenter. Here is some code.

view1.m

-(void)registerNotification
{
    NSDictionary *dict = [NSDictionary dictionaryWithObject:self.selectedOption forKey:@"data"];

    [[NSNotificationCenter defaultCenter]
     postNotificationName:@"pickerdata" 
     object:self
     userInfo:dict];
}

-(void)loadSecondView
{
    self.secondView = [[secondViewController alloc]init];
    [self.view addSubview:self.secondView.view];
    [self registerNotification];
    [self.secondView release];
}

view2.m

-(id)init
{
   if(self = [super init])
   {
    [[NSNotificationCenter defaultCenter] 
         addObserver:self 
         selector:@selector(reciveNotification:) 
         name:@"pickerdata" object:nil];
   }
   return self;
}


-(void)reciveNotification:(NSNotification *)notification
{
    if([[notification name] isEqualToString:@"pickerdata"])
    {
        NSLog(@"%@", [NSString stringWithFormat:@"%@", [[notification userInfo] objectForKey:@"data"]]); // The output of NSLog print selected value

       // Here is assignment to ivar
        self.selectedValue = [NSString stringWithFormat:@"%@", [[notification userInfo] objectForKey:@"data"]];
    }
}

The problem starts here. The logic which is interested of that value is implemented in loadView method. The problems is that loadView is executed before reciveNotification method and selectedValue does not contain needed information yet.
What to do so the information provided from NSNotificationCenter to be accessible from loadView method ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

信仰 2025-01-09 02:08:27

我不知道我是否完全理解你的问题,但是将值直接传递给 viewController 而不是处理通知不是更容易吗?

-(void)loadSecondView
{
    self.secondView = [[secondViewController alloc]init];
    self.secondView.selectedValue = self.selectedOption;
    [self.view addSubview:self.secondView.view];
    [self.secondView release];
}

I don't know if I fully understand your question, but wouldn't it be easier to pass the value directly to the viewController instead of dealing with notifications?

-(void)loadSecondView
{
    self.secondView = [[secondViewController alloc]init];
    self.secondView.selectedValue = self.selectedOption;
    [self.view addSubview:self.secondView.view];
    [self.secondView release];
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文