访问当前视图的父视图 - iphone sdk

时间:2022-10-09 10:42:03

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.

当我想访问当前UIView的父UIView时,我在当前UIView中声明父UIView的对象,并通过将父UIView对象分配给当前视图的对象属性来访问它。

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.

我也试过(parentView *)self.view.superview但没有帮助。

It gives error while calling function for this object as

调用此对象的函数时会出错

[UIVIew unrecognized selector......

Many Thanks,
Nav

非常感谢,导航

3 个解决方案

#1


72  

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

如果你直接从UIView(而不是UIViewController)调用它,它应该是self.superview而不是self.view.superview。

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

@ Naveed:这是您可以用于任何视图的常用代码,无论是父视图还是子视图,只需更改您想要按的按钮名称以及您想要去的视图名称。例如,在后退按钮按下你想要进入库视图然后编写此代码 -

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

让我知道你的问题是否得到解决。

#3


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.

我能想到的是你想从视图中调用viewcontroller的方法,你将它添加到viewcontroller的视图中。

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.

现在你有两个选项,你可以将视图控制器设置为视图的委托,然后通过[delegate performSelector:]方法调用viewcontroller的方法。

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.

另一种方法是访问视图的超级视图,然后获取它的控制器。你不能直接这样做,因为如果你能做到这一点就会破坏MVC的整个目的。

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

但是,仍然有一条出路,你走了: -

Get to UIViewController from UIView?

从UIView到UIViewController?

Hope, this helps you.

希望,这对你有所帮助。

#1


72  

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

如果你直接从UIView(而不是UIViewController)调用它,它应该是self.superview而不是self.view.superview。

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

@ Naveed:这是您可以用于任何视图的常用代码,无论是父视图还是子视图,只需更改您想要按的按钮名称以及您想要去的视图名称。例如,在后退按钮按下你想要进入库视图然后编写此代码 -

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

让我知道你的问题是否得到解决。

#3


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.

我能想到的是你想从视图中调用viewcontroller的方法,你将它添加到viewcontroller的视图中。

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.

现在你有两个选项,你可以将视图控制器设置为视图的委托,然后通过[delegate performSelector:]方法调用viewcontroller的方法。

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.

另一种方法是访问视图的超级视图,然后获取它的控制器。你不能直接这样做,因为如果你能做到这一点就会破坏MVC的整个目的。

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

但是,仍然有一条出路,你走了: -

Get to UIViewController from UIView?

从UIView到UIViewController?

Hope, this helps you.

希望,这对你有所帮助。