我正在开发单一视图应用程序。点击按钮我想切换到下一页(让菜单控制器说)。请指定我必须在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];
}
答案 0 :(得分:3)
在根视图控制器的app委托中添加它:
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];
答案 1 :(得分:2)
您必须将UINavigationController
设置为Window.rootViewController
,如下所示。
FirstViewController *viewController = [[FirstViewController alloc] initWithNibName:@"FirstViewController" bundle:nil];
UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:viewController];
self.window.rootViewController = navigationController;
答案 2 :(得分:1)
你应该继承UIViewController并实现你想要的视图。可以通过编程方式或在界面构建器中构建视图。
然后,您可以使用segues,storyboard标识符或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文件,但由于故事板更容易使用,我不会为此烦恼。
没有故事板: 在app代理中:
- (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];
}
答案 3 :(得分:0)
很简单:
-(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];
}