xcode 如何访问父视图?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11612194/
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-09-15 00:59:42  来源:igfitidea点击:

How do I get acces to the parent view?

objective-cxcodeuiviewuiviewcontrollerstoryboard

提问by Spyral

I'm trying to acces the storyboard from code to be able to use this line:

我正在尝试从代码访问故事板以便能够使用这一行:

DetailViewController *detail = [self.storyboard instantiateViewControllerWithIdentifier:@"detail"];

I use that in my mapview and listview, but want to use it somewhere else to. The problem is that this view, is a subview of another view.

我在我的地图视图和列表视图中使用它,但想在其他地方使用它。问题是这个视图是另一个视图的子视图。

It's set up as followed:

它的设置如下:

thisBigView is a view I added in the storyboard and it's is ThisBigViewControllerIn storyboard I added another view to that view, let's call it thisSmallView. The class is set to ThisSmallView.

thisBigView 是我在情节提要中添加的一个视图,它是ThisBigViewController在情节提要中我向该视图添加了另一个视图,我们称之为 thisSmallView。该类设置为ThisSmallView.

ThisSmallView is a custom view where I generate buttons dynamically in on the view. These buttons call the following action:

ThisSmallView 是一个自定义视图,我在该视图中动态生成按钮。这些按钮调用以下操作:

-(void) radarEventClick:(UIButton *)sender{
    SingletonManager *sharedManager = [SingletonManager sharedManager];

    DetailViewController *detail = [self.storyboard instantiateViewControllerWithIdentifier:@"detail"]; // PROBLEM 1

    Event *a;
    for(int i = 0; i < [sharedManager.eventsManager count]; i++){
        if(sender.tag == ((Event*)([sharedManager.eventsManager objectAtIndex:i])).id_nr){
            a = [sharedManager.eventsManager objectAtIndex:i];
            break;
        }
    }

    [detail setEvent:a];

    [self.navigationController pushViewController:detail animated:YES];    // PROBLEM 2

}

This is code I'm using in my mapviewcontroller to respond to annotationdisclosure clicks, and want to use it here to, but I have 2 problems!

这是我在 mapviewcontroller 中使用的代码来响应 annotationdisclosure 点击,并想在这里使用它,但我有两个问题!

PROBLEM 1: Because thisSmallView is a subview of anotherview, it doesn't have direct access to the storyboard and don't know how to get that access.

问题 1:因为 thisSmallView 是另一个视图的子视图,它不能直接访问故事板,也不知道如何获得该访问权限。

PROBLEM 2: thisBigView is embed in a navigationcontroller, but again, I don't know who to access thisBigView, so I can't access the navigationcontroller.

问题2:thisBigView被嵌入的navigationcontroller,但同样,我不知道是谁访问thisBigView,所以我不能访问navigationcontroller

(I think if I could solve problem 2, I would automatically be solving problem 1 to?)

(我想如果我能解决问题 2,我会自动解决问题 1 到?)

-- EDIT: what I tried --

-- 编辑:我试过的 --

DetailViewController *detail = [self.superview.storyboard instantiateViewControllerWithIdentifier:@"detail"];   

But then I just get 'property storyboard not found on object of type UIView*'

但后来我只是得到“在 UIView* 类型的对象上找不到属性故事板”

采纳答案by Dustin

Add the subview as a property of the superview in interface builder (control-drag to the header file). Then add a UIViewControllerproperty to the subview. In the superview's code then do

在界面构建器中将子视图添加为超级视图的属性(控制拖动到头文件)。然后UIViewController在子视图中添加一个属性。在超级视图的代码中然后做

nameOfSubView.superViewPropertyName = self;