【问题标题】:How can my main view controller know when the web service call finished我的主视图控制器如何知道 Web 服务调用何时完成
【发布时间】:2012-06-15 19:11:05
【问题描述】:

您好,我有一个基于 Web 服务的应用程序,可以与我们的服务器交换数据。因为我有一个专门的班级来做我的工作,所以主视图控制器实际上每次都会调用工人。工作人员自己知道连接何时完成,因为它是一个 NSURLConnectionDelegate。但是,每当工作完成时,我都需要通知主视图控制器。 worker 是主视图控制器的代表,因此它知道何时需要开始工作。请帮帮我。

【问题讨论】:

    标签: iphone ios web-services ipad delegates


    【解决方案1】:

    你应该反其道而行之。

    首先将您的工作对象声明为您的主视图控制器的成员(当然,使其成为一个属性),然后从您的主视图控制器中,您可以调用[self.worker sendRequstToServer] 来发送请求。

    其次,在您的主视图控制器中,无论您在哪里初始化您的工作对象,都不要忘记输入self = worker.delegate

    第三,在你的worker类中声明一个委托方法,比如-(void)webServiceCallFinished(),并在你的worker的-connectionDidFinishLoading()中调用[self.delegate webServiceCallFinished](如果你使用NSXMLParer来解析xml,你可能想在-parserDidEndDocument()中做)

    最后,你想将你的主视图控制器声明为你的工人类的代表并实现-webServiceCallFinished()

    希望这会有所帮助。

    【讨论】:

      【解决方案2】:

      您可以通过两种方式做到这一点:

      方法一:

      用户本地通知。 在主类中,通过以下方式将观察者添加到 LocalNotification Center 中。

      [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(jobDone) name:@"WORKERJOBOVER" object:nil];
      

      当工作完成后,在工人类中发布通知以触发选择器:

      [[NSNotificationCenter defaultCenter] postNotificationName:@"WORKERJOBOVER" object:nil];
      

      方法 #2:

      您可以为您的工作程序类创建一个协议,并在协议中添加一个方法,当您在工作程序中完成工作时,您可以在委托上调用该方法。

      WorkerClass.h

      //WorkerClass.h
      
      @protocol WorkerDelegate
      
      @interface WorkerClass: NSObject
      
      @property (nonatomic, assign) id<WorkerDelegate> delegate
      
      - (void)JobInProcess;
      @end
      
      
      @protocol WorkerDelegate
      - (void)MyJobIsDone;
      @end
      

      WorkerClass.m

      //WorkerClass.m
      
      @implementation WorkerClass
      @synthesize delegate = _delegate;
      
      - (void)JobInProcess
      {
          //When job over this will fire callback method in main class
          [self.delegate MyJobIsDone];
      }
      

      MainClass.h

          //MainClass.h
      #import WorkerClass.h
      
      @interface MainClass: NSObject <WorkerDelegate>
      
      
      @end
      

      MainClass.m

      //MainClass.m
      
      @implementation MainClass
      
      - (void)MyJobIsDone
      {
          //Do whatever you like
      }
      @end
      

      【讨论】:

      • 你的委托属性不应该是 id 类型吗?
      【解决方案3】:

      在主视图控制器的viewDidLoad: 中,使用[NSNotificationCenter defaultCenter] 将其设置为通知观察者:

      [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(webServiceContacted:) name:@"webServiceConnectionSuccessful" object:nil];
      

      然后,当您的数据与服务器成功交换后,在网络完成块中发布通知:

      [[NSNotificationCenter defaultCenter] postNotificationName:@"webServiceConnectionSuccessful" object:nil];
      

      webServiceContacted: 可能看起来像这样:

      -(void)webServiceContacted:(NSNotification *)note
      {
          //do stuff here
      }
      

      【讨论】:

        【解决方案4】:

        我会从后台工作人员发出一个通知,您可以从主视图控制器中收听该通知

        当你想收听时,在你的视图中添加以下内容。

        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(operationComplete:) name:@"operationComplete" object:nil];
        

        在您的后台视图中通知事件已完成。

        [[NSNotificationCenter defaultCenter] postNotificationName:@"operationComplete" object:nil];
        

        最后别忘了移除观察者 [[NSNotificationCenter defaultCenter] removeObserver:self];

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2019-07-14
          • 2014-05-07
          • 2013-06-12
          • 2015-12-10
          • 2012-04-04
          相关资源
          最近更新 更多