访问当前视图的父视图 - iOS

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4813201/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 18:41:11  来源:igfitidea点击:

Accessing parent view of current view - iOS

iosobjective-cuiviewsuperview

提问by Naveed Rafi

When I want to access the parent UIViewof current UIViewI declare object of parent UIViewin current UIViewand access it by assigning the parent UIViewobject 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.superviewbut didn't help.

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

It gives error while calling function for this object as

它在为此对象调用函数时出错

[UIVIew unrecognized selector......

回答by omz

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

如果您直接从 a UIView(而不是 a UIViewController)调用它,则它应该self.superview代替self.view.superview

回答by Archana Chaurasia

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

让我知道您的问题是否已解决。

回答by c4code

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.

现在你有两个选择,要么设置你的视图控制器你的视图委托,然后通过 [delegate performSelector:] 方法调用你的视图控制器的方法。

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.

希望,这对你有帮助。