Objective-C – Sending data from NSObject subclass to an UIViewController

后端 未结 2 1396
情书的邮戳
情书的邮戳 2021-01-27 06:30

I have an UIViewController which has a UITableView inside. In this view controller I want to display some data that I have downloaded from the internet

相关标签:
2条回答
  • 2021-01-27 07:01

    Your code:

    OfficesViewController *ovc = [[OfficesViewController alloc] init];
    

    Creates new instance property of OfficesViewController. Since it's a new instance it does not have a connection to the OfficesViewController you triggered after downloading and parsing process. To be able to comunicate b/w OfficesViewController and OfficesParser create a modified init method for OfficesParser that allows week pointer to the OfficesViewController.

    @interface OfficesParser ()
    
    @property(nonatomic,assign)OfficesViewController *ovc;
    
    @end
    
    @implementation OfficesParser
    
    @synthesize ovc;
    
    -(id)initWithDelegate:(OfficesViewController*)delegate{
        ovc = delegate;
        return [self init];
    }
    

    Now you can access your ovc delegate.

    - (void)queueFinished:(ASINetworkQueue *)queue {
        NSArray *offices = [self offices];
        [ovc setOffices:offices];
    }
    

    Finally create your OfficesParser like that

    self.officesParser = [[OfficesParser alloc] initWithDelegate: self];
    
    0 讨论(0)
  • 2021-01-27 07:18

    You need to take a look at delegates and protocols. They're exactly what you're looking for, as they let classes communicate without having to persist a reference. Here is another explanation on them.

    0 讨论(0)
提交回复
热议问题