Accessing parent view of current view - iOS

53k views Asked by At

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......
3

There are 3 answers

2
omz On BEST ANSWER

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

1
Archana Chaurasia On

@ 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.

0
c4code On

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:-

Get to UIViewController from UIView?

Hope, this helps you.