在 xcode 中从一个页面导航到另一个页面

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5375518/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 20:35:42  来源:igfitidea点击:

navigate from one page to another in xcode

iphoneobjective-cxcode

提问by Aravindhan

How can I navigate from one page to another page in xcode? remember without using the interface builder... I want the answer programmatically?

如何在 xcode 中从一个页面导航到另一个页面?记得不使用界面生成器...我想以编程方式得到答案?

回答by visakh7

Pls be more precise if you want to get what you want.

如果你想得到你想要的,请更精确。

If you are using view controllers within navigationcontroller you can make use of its pushViewController:or presentModalViewController:. Or if you just want to show another view you can just add the next view to existing view as subview.

如果您在 navigationcontroller 中使用视图控制器,您可以使用它的pushViewController:presentModalViewController:。或者,如果您只想显示另一个视图,您可以将下一个视图作为子视图添加到现有视图中。

回答by devsri

Although your question is not much clear but still I would like to give a try...

虽然你的问题不是很清楚,但我还是想试一试......

You can use

您可以使用

UIViewController *yourViewController = [[YourViewControllerClass alloc] initWithNibName:@"<name of xib>" bundle:nil];
[self presentModalViewController:yourViewController animated:YES];
[yourViewController release];

In case the new view is also to be created programmatically, you can do that in the viewDidLoad method of YourViewControllerClass and change the initialization to

如果还要以编程方式创建新视图,您可以在 YourViewControllerClass 的 viewDidLoad 方法中执行此操作并将初始化更改为

UIViewController *yourViewController = [[YourViewControllerClass alloc] init];

In YourViewController when you wish to come back to previous view on some button action you can use

在 YourViewController 中,当您希望返回到某些按钮操作的上一个视图时,您可以使用

[self dismissModalViewControllerAnimated:YES];

Another way that you can do is

你可以做的另一种方法是

UIViewController *yourViewController = [[YourViewControllerClass alloc] init];
[self addSubview:[yourViewController view]];

and to remove the view you can use

并删除您可以使用的视图

[self.view removeFromSuperview];

Hope this works for you, if yes please communicate....:)

希望这对你有用,如果是,请交流....:)

回答by Avinash651

//Appdelegate.m

//Appdelegate.m

-(BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
self.viewController = [[ViewController alloc] initWithNibName:@"ViewController" bundle:nil];
UINavigationController *navigation = [[UINavigationController alloc]initWithRootViewController:self.viewController];
self.window.rootViewController = navigation;
[self.window makeKeyAndVisible];
return YES;
}

//In viewcontroller1.M
- (IBAction)GoToNext:(id)sender 
{
ViewController2 *vc2 = [[ViewController2 alloc] init]; 
[self.navigationController pushViewController:vc2 animated:YES];
}