在容器视图中使用UIButton连接到父视图

时间:2021-07-23 09:02:35

I have a Main VC with a custom tab bar and a container view. I have three buttons in the tab bar that link to 3 different views using a custom segue that switches the views perfectly.

我有一个带有自定义标签栏和容器视图的主VC。我在标签栏中有三个按钮,使用自定义segue链接到3个不同的视图,可以完美地切换视图。

In one of my child views I have a button. I want that button to connect to a parent view however the app crashes when the button is pressed.

在我的一个孩子视图中,我有一个按钮。我希望该按钮连接到父视图,但按下按钮时应用程序崩溃。

I have looked at several answers and only found answers to questions regarding switching childviews using a button in the parent view such as: Linking child view controllers to a parent view controller within storyboard

我查看了几个答案,只找到了有关使用父视图中的按钮切换子视图的问题的答案,例如:将子视图控制器链接到故事板中的父视图控制器

My structure is as follows:

我的结构如下:

Main VC --> Child VC within Main VC --> Button

主VC - >主VC内的子VC - >按钮

Button --> Custom Segue --> New Main VC embedded in Nav VC

按钮 - >自定义Segue - > Nav VC中嵌入的新主VC

When I press the button within the child VC to link to a new main VC it crashes.

当我按下子VC中的按钮链接到新的主VC时,它会崩溃。

This is my prepareForSegue code:

这是我的prepareForSegue代码:

H file:

H文件:

//Name of the parent class linking to
@property (weak, nonatomic) FillUpEntryViewController * fueVC;

M file:

M文件:

-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    if ([segue.identifier isEqualToString:@"NewEntrySegue"]) {
        self.fueVC = segue.destinationViewController;
    }
}

Any help would be much appreciated. I don't think it should be as difficult as it is but I am relatively new to this.

任何帮助将非常感激。我不认为它应该像现在这样困难,但我对此相对较新。

EDIT: The child view has it's own class which contains the segue code above. Just to eliminate any confusion.

编辑:子视图有自己的类,其中包含上面的segue代码。只是为了消除任何困惑。

在容器视图中使用UIButton连接到父视图

1 个解决方案

#1


0  

I don't know if I undestand you, you can create a dismiss segue and join the two views in your story board by it, or just use the code in your action:

我不知道如果我不理解你,你可以创建一个dismiss segue并通过它加入你的故事板中的两个视图,或者只是在你的动作中使用代码:

import "DismissSegue.h"

.h file

.h文件

#import <UIKit/UIKit.h>

@interface DismissSegue : UIStoryboardSegue


@end

.m file

.m文件

@implementation DismissSegue

-(void)perform{
    UIViewController *sourceViewCOntroller = self.sourceViewController;
    [sourceViewCOntroller.view endEditing:YES];
    [sourceViewCOntroller.presentingViewController dismissViewControllerAnimated:YES completion:nil];
}

@end

I hope it help you.

我希望它可以帮到你。

#1


0  

I don't know if I undestand you, you can create a dismiss segue and join the two views in your story board by it, or just use the code in your action:

我不知道如果我不理解你,你可以创建一个dismiss segue并通过它加入你的故事板中的两个视图,或者只是在你的动作中使用代码:

import "DismissSegue.h"

.h file

.h文件

#import <UIKit/UIKit.h>

@interface DismissSegue : UIStoryboardSegue


@end

.m file

.m文件

@implementation DismissSegue

-(void)perform{
    UIViewController *sourceViewCOntroller = self.sourceViewController;
    [sourceViewCOntroller.view endEditing:YES];
    [sourceViewCOntroller.presentingViewController dismissViewControllerAnimated:YES completion:nil];
}

@end

I hope it help you.

我希望它可以帮到你。