fadd
fadd

Reputation: 584

Call a function from a subview UIViewController Xcode

I have ADMINviewController that contains a UITableView and contains a function called Request that loads the devices from the server and fill the UITableView, when I select a cell, which is a device name, VehicleInfoViewController as a subview opens to let me change the device name.

So what I want is to call the Request function after showing a message that the device name has changed successfully, in order to reload the data from the server and update the UITableView with the new device name.

How to call the Request function from the subview?

Upvotes: 0

Views: 2266

Answers (2)

DoctorE
DoctorE

Reputation: 31

The approach above (by Rafael) can also be used by a subView to call a method located in that subView's viewController. Just make sure to add the necessary methods to avoid compiler warnings, e.g., use

@synthesize adminController

in the implementation of the VehicleInfoViewController class for the above example. This can also be called from within any object that has a VehicleInfoViewController instance. For example, assuming parentObject contains a vehicleInfoViewController member variable, calling Request within the parentObject would look like:

[parentObject.adminController performSelector:@selector(Request)];

Upvotes: 2

Rafael Kayumov
Rafael Kayumov

Reputation: 226

If you want to call Request method which is in ADMINviewController class from VehicleInfoViewController class you should pass a pointer of ADMINviewController instance to VehicleInfoViewController.

Add id property to VehicleInfoViewController class like this:

@property(nonatomic,assign)id adminController;

Then after you create VehicleInfoViewController instance, pass self pointer to it like this:

vehicleInfoViewController = [[VehicleInfoViewController alloc] initWithNibName:@"vehicleInfoViewController" bundle:nil];
vehicleInfoViewController.adminController = self;

When you need to call Request method being inside vehicleInfoViewController do this:

[self.adminController performSelector:@selector(Request)];

Upvotes: 2

Related Questions