5

我确信这是一个愚蠢的错误,但我在过去的一个小时里试图从我的超级视图中删除一个子视图,但没有任何成功。

在我的第一个视图中,我有

UIViewController *helpView = [[[UIViewController alloc] initWithNibName:@"HelpView" bundle:nil] autorelease];
[self.view addSubview:helpView.view];

然后在 helpView 中,我有一个按钮,它连接到一个名为“closeHelp”的 IBAction,它只执行以下操作:

- (IBAction) closeHelp{
    [self.view removeFromSuperview];
}

但这会导致我的应用程序由于某种奇怪的原因而崩溃,EXC_BAS_ACCESS,即使是在 HelpView 内的那些,这意味着 self.view 应该指向正确的子视图。

感谢您的帮助

谢谢你。
谢。

4

5 回答 5

5

正如安德烈亚斯回答的那样,您正在尝试从其超级/父视图中删除 self.view 。您基本上需要从其父视图中删除 helpView 。

所以应该是

- (IBAction) closeHelp{
    [helpView removeFromSuperview];
}

但是我们不知道上述方法中的“helpView”是什么。因为我们没有任何处理它。

所以我们的代码最终应该是这样的。

#define HELP_VIEW_TAG 101 // Give tag of your choice

HelpView *helpView = [[HelpView alloc] initWithNibName:@"HelpView" bundle:nil];
helpView.view.tag = HELP_VIEW_TAG;
[self.view addSubview:helpView.view];
[helpView release];

- (IBAction) closeHelp{
    UIView *helpView = [self.view viewWithTag:HELP_VIEW_TAG];
    [helpView removeFromSuperview];
}
于 2011-07-09T08:34:36.383 回答
2

self.view 不指向您的子视图,而是指向您的 uiviewcontroller 管理的根视图。您可能应该只删除子视图堆栈中的最后一个对象,而不是整个视图,因为现在您正在删除整个帮助视图。

无论如何,您为什么不以模态方式呈现视图控制器而不是这样做呢?

[self presentModalViewController:helpView animated:NO/YES];

helpView. modalTransitionStyle = //One of the constants below

UIModalTransitionStyleCoverVertical
UIModalTransitionStyleFlipHorizontal
UIModalTransitionStyleCrossDissolve
UIModalTransitionStylePartialCurl

通常我在视图控制器中编写self.modalTransitionStyle = // One of the constants ,它将以模态方式呈现,而不是传播代码。

于 2011-07-09T08:20:12.333 回答
1

您正在初始化helpViewUIViewController.
确保您在初始化它的视图控制器#import "HelpView.h"的文件中有(或任何调用了 helpView .h 文件) 。.h

然后,使用以下代码:

HelpView *helpView = [[HelpView alloc] initWithNibName:@"HelpView" bundle:nil];
[self.view addSubview:helpView.view];

那应该解决它。

于 2011-07-09T08:23:12.267 回答
0

最终对我来说最简单的解决方案是将我的 XIB 的文件所有者定义为与父控制器相同的类,这意味着父控制器将同时控制父视图和子视图,这样会更容易。:)

于 2011-07-09T20:21:14.603 回答
-1
Declare the help view on calss level.

in.h file 

@class HelpView;
..
@interface
{
HelpView *helpView;
}
@property(nonatomic,retain)HelpView*  helpView;


In.m file 
#import "HelpView"
@synthensize helpView;



now add this Code where you want 

helpView = [[HelpView alloc] initWithNibName:@"HelpView" bundle:nil];
helpView.view.tag = HELP_VIEW_TAG;
[self.view addSubview:helpView.view];


- (IBAction) closeHelp{
    //UIView *helpView = [self.view viewWithTag:HELP_VIEW_TAG];
    [helpView removeFromSuperview];
}

-(void)dealloc { [帮助查看发布]; }

于 2011-07-09T09:33:09.377 回答