【问题标题】:Sharing data between classes在类之间共享数据
【发布时间】:2011-06-03 20:55:06
【问题描述】:

我正在构建一个尝试使用 Web 服务获取数据的 iphone 应用程序。单击按钮的用户将导航到新视图。 登录操作的代码是

- (IBAction)btnLoginAction:(id)sender 
{

 [UIApplication sharedApplication].networkActivityIndicatorVisible=YES;
 activityIndicator=[[UIActivityIndicatorView alloc] initWithFrame:CGRectMake(50.0, 50.0, 50, 50)];
 [activityIndicator setCenter:CGPointMake(156, 208)];
 [activityIndicator startAnimating];

 [self.view addSubview:activityIndicator];
 soapMessenger=[[SoapMessenger alloc] init];
 parser=[[XML_Parsing alloc] init];
 [soapMessenger buildSoap:@"CURRENT"];
 [soapMessenger setConnection];

soapMessenger 和 xml_parsing 是用于创建连接和解析数据的类 ....我能够解析 xml。但问题是将数据传递给新的视图控制器....如何传递结果数据到一个新的班级?

【问题讨论】:

  • 嘿,这个答案有没有帮助?

标签: iphone web-services web-applications mobile-website


【解决方案1】:

这是一个模型视图控制器问题。 MVC。您的数据类应该将数据存储在您的模型中,然后可以将模型的相关部分传递给新类 - 通常通过在新类中合成正确类型的属性,然后分配/初始化新类,并设置属性。

【讨论】:

    【解决方案2】:

    您需要定义一个模型类来保存从 XML 解析的数据,然后通过属性将其传递给新的视图控制器。

    【讨论】:

    • 我确实做到了。我已经定义了一个检索解析结果的方法。我在 ConnectionDidFinishLoadig 方法中得到解析结果....我正在使用属性来设置此方法中的变量...但是当我导航到下一个视图时,该值没有得到更新。
    【解决方案3】:

    如果要将数据从一个类传递到另一个类,则需要在传递数据的类中创建要传递的数据类型的属性。

    例子

    FirstVC - 您想要将值传递给下一个视图控制器的类。

    SecondVC - 您要将值传递给的第二类。

    @interface FirstVC : UIViewController{
    NSInteger *testInteger;
    }
    
    @implementation FirstVC{
    
    - (IBAction)btnLoginAction:(id)sender 
    {
    SecondVC *second = [[SecondVC alloc] initWithNibName:@"SecondVC" buddle:[NSBundle mainbundle]];
    second.receivingInteger=testInteger;
    [second release];
    }
    
    }
    
    @interface SecondVC{
    NSInteger receivingInteger;
    }
    @property(nonatomic) NSInteger receivingInteger;
    

    【讨论】:

      【解决方案4】:

      我已经想通了.....我使用协议来实现它。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2020-04-08
        • 1970-01-01
        • 2019-07-02
        • 2011-01-13
        • 1970-01-01
        • 1970-01-01
        • 2013-02-25
        • 2023-03-19
        相关资源
        最近更新 更多