Iphone 通过选择按钮删除viewcontroller

Iphone 通过选择按钮删除viewcontroller,iphone,xcode,ios5,uiview,uiviewcontroller,Iphone,Xcode,Ios5,Uiview,Uiviewcontroller,我创建了一个包含4个viewcontroller及其.h、.m文件的应用程序…在我的firstviewcontroller中,按下一个按钮,它将转到secondviewcontroller,在secondviewcontroller中,它有两个按钮,用于切换回firstviewcontroller,另一个按钮将转到thirdviewcontroller。这是我的firstviewcontroller.m代码 [[NSBundle mainBundle] loadNibNamed:@"Second

我创建了一个包含4个viewcontroller及其.h、.m文件的应用程序…在我的firstviewcontroller中,按下一个按钮,它将转到secondviewcontroller,在secondviewcontroller中,它有两个按钮,用于切换回firstviewcontroller,另一个按钮将转到thirdviewcontroller。这是我的firstviewcontroller.m代码

[[NSBundle mainBundle] loadNibNamed:@"SecondViewController" owner:self options:nil];
在我的第二个视图控制器中,第一个按钮

[[NSBundle mainBundle] loadNibNamed:@"FirstViewController" owner:self options:nil];
还有一个按钮

[[NSBundle mainBundle] loadNibNamed:@"ThirdViewController" owner:self options:nil];
当我在firstviewcontroller中选择按钮时,它会加载secondviewcontroller,但在second view controller中,如果我选择任何按钮,我会收到Sigabart警告


有人能想到这一点吗?我已经尝试了很多方法。

您可以使用以下方法来执行这些任务:

方法1:

在FirstViewController.m中,单击按钮编写以下代码:

SecondViewController *secondVC = [[SecondViewController alloc]initwithNibName:@"SecondViewController" bundle:nil];
[self.view addSubView:secondVC.view];
这将把secondviewcontroller添加到当前视图中

在SecondViewController.m中添加第三个视图,您可以编写

ThirdViewController *thirdVC = [[ThirdViewController alloc]initwithNibName:@"ThirdViewController" bundle:nil];
[self.view addSubView:thirdVC.view];
要删除第二个视图,您可以这样写:

[self.view removeFromSuperview];
方法2:

使用导航控制器。

使用如下代码

FirstViewController *FVC = [[FirstViewController alloc] initWithNibName:@"FirstViewController" bundle:nil];

//For Push    
[self.navigationController pushViewController:FVC animated:YES];

//For Pop
[self.navigationController popViewControllerAnimated:YES];

:)

在iPhone应用程序编程中,当我们将一个视图控制器推送到另一个新的视图控制器时,视图控制器通常一个接一个地存储在堆栈中。并以相同的反向格式查看和删除这些控制器。因此,如果要推送新的视图控制器,请使用以下命令:

SecondViewController*second=[[SecondViewController alloc] initWithNibName:@“SecondViewController”捆绑包:nil]

[self.navigationController-pushViewController:second-animated:YES]

如果要删除当前视图控制器,请使用以下命令:

`[self.navigationController PopViewControllerInitiated:是]

如果你想从任何一个视图控制器跳转到你的第一个视图控制器

[self.navigationController-popToRootViewControllerAnimated:是]

您可以通过这行代码获得此堆栈中显示的ViewController

NSArray*viewArr=[self.navigationController viewControllers]

NSLog(@“%@”,viewArr)

[self.navigationController popToViewController:[viewArr objectAtIndex:1]已设置动画:是]

`


希望这将有助于您使用导航控制器

这是否有特定的要求以这种方式导航,或者您可以按照安全案例建议使用UINavigationController。使用导航控制器就像您的要求一样。感谢很多人…它很有效…如果代码对您有效,请接受答案并喜欢它。
//For going in forword direction you can use this block
{
FirstViewController *firstVC = [[FirstViewController alloc] initWithNibName:@"FirstViewController" bundle:nil];
[self.navigationController pushViewController:firstVC animated:YES];
[firstVC relese];
}

//For returning back you can use this block
{ 
   [self.navigationController popViewControllerAnimated:YES];
}


//But before using this line of code you have to alloc and make the property of    navigationController in your appDelegate