为什么这个子视图没有删除?

时间:2012-06-12 19:19:35

标签: objective-c ios cocoa-touch

IBAction什么都不做。将“Back”记录到控制台,以便连接正常。调用IBAction时,self.topView也不执行任何操作

-(IBAction)loadSettingsView:(id)sender;

{

    if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) {
        [[NSBundle mainBundle] loadNibNamed:@"settingsView_iphone" owner:self options:nil]; 
    } else {
        [[NSBundle mainBundle] loadNibNamed:@"settingsView_ipad" owner:self options:nil];
    }
    [self.view addSubview:topView];
}

-(IBAction)loadMainView:(id)sender;
{
    [topView removeFromSuperview];
    NSLog(@"back");

}

2 个答案:

答案 0 :(得分:3)

我希望我在这里没有做太多的假设,但这应该可以解决你的问题。我假设topView是当前班级的成员:

-(IBAction)loadSettingsView:(id)sender;

{

    if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) {
        topView = [[[NSBundle mainBundle] loadNibNamed:@"settingsView_iphone" owner:self options:nil] objectAtIndex:0]; 
    } else {
        topView = [[[NSBundle mainBundle] loadNibNamed:@"settingsView_ipad" owner:self options:nil] objectAtIndex:0];
    }
    [self.view addSubview:topView];
}

-(IBAction)loadMainView:(id)sender;
{
    [topView removeFromSuperview];
    NSLog(@"back");

}

基本上,您使用的loadNibNamed方法返回一个包含nib中所有顶级视图的数组。如果你想要引用这些视图(这里我假设在nib中有一个视图),你需要实际分配你的topView变量。目前topView可能为零,因此您的removeFromSuperview来电无效。

答案 1 :(得分:0)

资助一个更容易的解决方案。只需在主视图的笔尖中创建一个新视图,并将IBOutlet附加到它上面。像梦一样工作。

-(IBAction)loadSettingsView:(id)sender;
{
        [self.view addSubview:settingsView];
}

-(IBAction)loadMainView:(id)sender;
{
    [settingsView removeFromSuperview];
}