无法使用removeFromSuperview方法删除视图

发布于 2024-11-30 16:38:26 字数 437 浏览 2 评论 0原文

我无法删除视图。我使用 addsubview 方法添加视图,但是当我使用 RemoveFromSuperView 时。

// 这里我从登录按钮添加视图

medicalViewObject=[[MedicalInfoViewController alloc] initWithNibName:@"MedicalInfoViewController" bundle:nil];
    [self.view addSubview:medicalViewObject.view];

//这里我在另一个 ViewController 中编写RemoveFromSuperView

-(void)BackToMainView
{
    [self.view removeFromSuperview];
}

I am not able remove view. I am adding view using addsubview method but when I use RemoveFromSuperView.

// Here I am adding view from login button

medicalViewObject=[[MedicalInfoViewController alloc] initWithNibName:@"MedicalInfoViewController" bundle:nil];
    [self.view addSubview:medicalViewObject.view];

//here I am writing RemoveFromSuperView in another ViewController

-(void)BackToMainView
{
    [self.view removeFromSuperview];
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

睫毛溺水了 2024-12-07 16:38:26

它不起作用,因为您将removeFromSuperview发送到错误的视图对象。这就是您所需要的:

[medicalViewObject.view removeFromSuperview];

编辑:

您的问题表明您有一个视图(因为您没有在问题中包含该视图的名称,我将简单地将其称为MainView),然后将一个名为medicalViewObject的子视图添加到您的MainView中。您还有一个名为“BackToMainView”的方法,您想要执行removeFromSuperview 函数。您的问题表明,medicalViewObject 中的某些用户操作(例如按下按钮)应该调用“BackToMainView”方法。

如果这一切都是正确的,那么我上面的答案就是正确的。但根据您的评论,听起来您还需要在 MedicalViewObject 中实现委托协议,然后让您的“MainView”采用该协议。

在 MedicalViewObject 中的委托声明中,您需要有一个如下所示的方法调用:

-(void)backButtonWasPressed;

在 MainView 的实现中,它应该如下所示:

-(void)backButtonWasPressed
{
    [medicalViewObject.view removeFromSuperview];
}

所以现在,无论您在 MedicalViewObject 中使用什么用户操作,都可以返回到主视图(无论是按钮还是其他对象)需要调用以下内容:

[delegate backButtonWasPressed];

根据您的情况,它可能看起来有点不同,但这是完成您想要做的事情的相当常见的方法。我希望这有帮助。

It is not working because you are sending removeFromSuperview to the wrong view object. This is what you need:

[medicalViewObject.view removeFromSuperview];

EDIT:

Your question suggests that you have a view (because you did not include the name of this view in your question, I will simply call it MainView) and then you add a subview called medicalViewObject to your MainView. You also have a method call "BackToMainView" which you want to perform the removeFromSuperview function. Your question suggests that some user action in your medicalViewObject (such as a button press) is supposed to call the "BackToMainView" method.

If this is all correct, then my answer above is correct. But based upon your comment, it sounds like you will also need to implement a delegate protocol in your medicalViewObject, and then have your "MainView" adopt the protocol.

In your declaration of the delegate in your medicalViewObject, you need to have a method call like this:

-(void)backButtonWasPressed;

and in the implementation of your MainView it should look something like this:

-(void)backButtonWasPressed
{
    [medicalViewObject.view removeFromSuperview];
}

So now, whatever user action you are using in your medicalViewObject to go back to the main view (whether a button or some other object) it needs to call the following:

[delegate backButtonWasPressed];

Depending on your situation it may look a little different, but this a fairly common way to accomplish what you are trying to do. I hope this helps.

美胚控场 2024-12-07 16:38:26

当您调用 [self.view removeFromSuperview]; 时,您正在告诉您所在的任何视图控制器删除其自己的视图。您应该从完全相同的 MedicalInfoViewController 中调用该行,或从外部告诉medicalViewObject删除其视图,如 [medicalViewObject.view removeFromSuperview];

when you call [self.view removeFromSuperview]; you are telling whatever view controller you are in to remove its own view. You should be calling that line from within that exact same MedicalInfoViewController or telling medicalViewObject from the outside to remove its view like [medicalViewObject.view removeFromSuperview];

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文