>我正在研究单视图应用程序。单击按钮后,我想切换到下一页(假设菜单控制器)。请指定我必须在appdelegate中进行哪些更改才能添加导航控制器,因为我是iOS的新手。
[button addTarget:select action:@selector(buttonClick) forControlEvents:UIControlEventTouchUpInside]; and implement the selector as
-(void)buttonClick{
UIViewController *menu = [[UIViewController alloc] init];
[self.navigationController pushViewController:menu animated:YES];
}
在根视图控制器的应用委托中添加以下内容:
FirstViewController *firstViewController = [[FirstViewController alloc] initWithNibName:@"view name" bundle:nil];
UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:firstViewController];
self.window.rootViewController = navigationController;
在按钮内单击:
SecondViewController *secondViewController = [[SecondViewController alloc] initWithNibName:@"view name" bundle:nil];
[self.navigationController pushViewController:secondViewController animated:YES];
Window.rootViewController
设置UINavigationController
,如下所示。
FirstViewController *viewController = [[FirstViewController alloc] initWithNibName:@"FirstViewController" bundle:nil];
UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:viewController];
self.window.rootViewController = navigationController;
你应该子类UIViewController并实现你想要的视图。视图可以通过编程方式或在界面生成器中构建。
然后,您可以使用 segue、情节提要标识符或 xib 文件来加载视图。
可能看起来像这样:(假设您在故事板中设置了视图控制器,并将它们与适当的 segue 和下面的标识符连接起来)
-(void)buttonClick{
[self performSegueWithIdentifier:@"MySegueIdentifier"];
}
-(void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([segue.identifier isEqualToString: @"MySegueIdentifier"]) {
MyCustomViewController* vc = (MyCustomViewController*)[segue destinationViewController];
//do something with your view controller, like set a property
}
}
或者也许像这样
-(void) buttonClick {
MyCustomViewController* vc = (MyCustomViewController*)[[UIStoryboard storyboardWithName: @"MyStoryboard"] instantiateViewControllerWithIdentifier: @"MyStoryboardIdentifier"];
[self.navigationController pushViewController: vc animated: YES]; //assuming current view controller is a navigation stack. Or could also do presentViewController:animated:
}
您将向项目添加一个类子类化UIViewController
,并将其导入(#import "MyCustomViewController.h"
在要从中推送的视图控制器的 .m 文件中)。
也可以使用 xib 文件,但我不会打扰这些,因为故事板更容易使用。
没有故事板:在应用委托中:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
navigationController = [[UINavigationController alloc] init]; //navigation controller is a property on the app delegate
// Override point for customization after application launch.
FirstViewController *firstViewController = [[FirstViewController alloc] init];
[navigationController pushViewController: firstViewController animated:NO];
[window addSubview:navigationController.view];
[window makeKeyAndVisible];
return YES;
}
在您的 FirstViewController 中:
-(void) buttonClick {
MyCustomViewController* vc = [[MyCustomViewController alloc] init]; // or maybe you have a custom init method
[self.navigationController pushViewController: vc animated: YES];
}
非常简单:
-(void)ButtonClickActionMethod
{
[button addTarget:select action:@selector(buttonClick) forControlEvents:UIControlEventTouchUpInside];
}
点击的操作:
-(void)buttonClick{
YourViewController *view = [[YourViewController alloc] initWithNibName:@"YourViewController" bundle:nil];
[self.navigationController pushViewController:view animated:YES];
}