How to access the calling class in Objective-C

Can you contact the sender of the message without passing the sender as a parameter? This is the simplified code to discuss:

// mainTableViewController.m
    [dataModel loadData];    //Table is requesting data based on user input

// dataModel.m
  -(void) loadData{

    // I want to store the sender for later reference
    sendingTableViewController = ???? ;  
  }

  - (void) connectionDidFinishLoading:(NSURLConnection *)connection {
        // Web data is loaded. Ask the sending tableViewController to 
        //  reload it data.
    [sendingTableViewController.tableView reloadData];
  } 

I'm still used to how to access methods and properties that are responsible for another object. I want to send a message dataModelto download some data using NSURLConnection. But I do not want the data returned , because I do not want to sit waiting for the data to load. I want to send a message to mainTableViewControllerwhen called connectionDidFinishLoading.

Since the method loadDatacan be called from any number tableViewControllers, I can’t just say [mainTableViewController reloadData].

Subsequent question
Excellent information! I love the carefree nature of StackOverflow.

, mainTableViewController dataModel?
, dataModel ?
dataModel mainTableViewController. :

// mainTableViewController.m
    dataModel *myDataModel = [[dataModel alloc] initWithDelegate:self ];

    // Does this method need to be defined in the mainTableViewController header file
    //  since I will already have defined it in the dataModel header file?
  -(void) dataDidFinishLoading {
    [self.tableView reloadData];
  }

// dataModel.m
  -(id) initWithDelegate:(id)aDelegate{
    self.delegate = aDelegate;
  }

  -(void) connectionDidFinishLoading:(NSURLConnection *)connection {
    [self.delegate dataDidFinishLoading];
  }  

, TableViewController myModel, dataModel TableViewController? dataModel AppDelegate ?

!

+3
2

, . , , coupling . - .

Cocoa . - - , , , (, ). , , mainTableViewController ( , ), . , , self.delegate. [tableView reloadData].

, , () .

Edit

, , . Cocoa NSNotificationCenter. ( , , ), . - , , , , - . , , "" .

; . .h, , . @optional , , .

. , . , (), , , . (, , ) . , !

+8

self?

-(void)canHazCheeseburger:(BOOL)canHaz
{
    if (canHaz) {
        self.cheeseBurger = [[[CheeseBurger alloc] init] autorelease];
        [cheeseBurger onNomNom];
    }       
}
0

Source: https://habr.com/ru/post/1728395/


All Articles