Question

I have created a method that calls a UIViewController and loads it as a subview like this

cameraViewController = [[CameraViewController alloc] init];
        cameraViewController.view.frame = CGRectMake(0.0, 0.0, screenHeight, screenWidth);
        [cameraViewController drawCameraView:htmlProjID ProjectGUID:selectedProjGUID DoorGUID:cell.currentItem.doorGUID Info:nil doorName:cell.currentItem.doorDesc ViewName:@"Finishing"];
        [self.view addSubview:cameraViewController.view];

I would like to know if there is an easy way to then access a method from camerViewController(subview) from the original UIViewController?

Was it helpful?

Solution

May be this will return what you want to get:

 id mainViewController = [self.view.superview nextResponder];

Apple's documentation of -[UIResponder nextResponder]:

UIView implements this method by returning the UIViewController object that manages it (if it has one) or its superview (if it doesn’t)

Other than this you can have idea from here.

OTHER TIPS

Define a protocol and use a delegate as per this answer...

How do I create delegates in Objective-C?

NSNotifications are always handy in this situation. You simply define a observer in viewDidLoad of camerViewController:

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

and then you just post an NSNotication of that name from the original view:

 [[NSNotificationCenter defaultCenter] postNotificationName:@"YourNSNotificaitionName" object:nil userInfo:Nil];

Which will call the appropriate method on the subview.

You can access Public methods of the Subview class from UIViewController. But it sometimes might not work due some limitations.

You can use Protocol to implement such things. For that follow this :

  • Declare the protocol from which ViewController you want call the method
  • Call the method of subview by using delegate protocol

Other way to perform it is to create that method into AppDelegate file & call from it.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top