Accessing parent view of current view - iOS

IosObjective CUiviewSuperview

Ios Problem Overview


When I want to access the parent UIView of current UIView I declare object of parent UIView in current UIView and access it by assigning the parent UIView object to current view's object property.

Is there any way to get rid of this and directly call the parent view's methods or properties?

I also tried (parentView *) self.view.superview but didn't help.

It gives error while calling function for this object as

[UIVIew unrecognized selector......

Ios Solutions


Solution 1 - Ios

If you're calling this directly from a UIView (and not a UIViewController), it should be self.superview instead of self.view.superview.

Solution 2 - Ios

@ Naveed: This is the common code u can use to any view whether it is parent or child view, Just change button name which u want to press and the view name on which u want to go. For example on back button press u want to go on library view then write this code -

-(IBAction)backButtonPressed:(id) sender
 {
        llibraryView *libraryview=[[libraryView alloc] initWithNibName:nil bundle:nil];
	libraryview.modalTransitionStyle=UIModalTransitionStyleCoverVertical;
	[self presentModalViewController:libraryview animated:YES];	
	[libraryview release];
 }

Let me know whether ur problem is solved or not.

Solution 3 - Ios

What I can think is that you want to call the viewcontroller's method from your view, which you added to the viewcontroller's view.

Now you have two options from here, either you set your view controller your view's delegate and then call your viewcontroller's method by [delegate performSelector:] approach.

The other approach is you access your view's superview, and then get it's controller. You can not do that directly, coz if you could do that it would defeat the entire purpose of MVC.

But, still there is a way out, here you go:-

https://stackoverflow.com/questions/1340434/get-to-uiviewcontroller-from-uiview-on-iphone

Hope, this helps you.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionNaveed RafiView Question on Stackoverflow
Solution 1 - IosomzView Answer on Stackoverflow
Solution 2 - IosArchana ChaurasiaView Answer on Stackoverflow
Solution 3 - Iosc4codeView Answer on Stackoverflow