简体   繁体   中英

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?

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)];

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, eg, 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)];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM