51

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......
Cœur
  • 37,241
  • 25
  • 195
  • 267
Naveed Rafi
  • 2,503
  • 5
  • 32
  • 40

3 Answers3

95

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

omz
  • 53,243
  • 5
  • 129
  • 141
0

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.

Community
  • 1
  • 1
c4code
  • 51
  • 2
0

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

Hiren
  • 12,720
  • 7
  • 52
  • 72
Archana Chaurasia
  • 1,396
  • 3
  • 19
  • 35
  • Thank you very much for your help Archana. Actually my problem is I don't want to View name. I wanted to make it dynamic. Thanks. – Naveed Rafi Feb 24 '11 at 13:32