我尝试使用iOS 5.0触摸按钮,从一个视图导航到另一个视图。
我正在使用的代码
- (IBAction)Actionlogin:(id)sender {
NSLog(@" login button has been pressed");
NSLog(@"In init");
test_details *aSecondPageController=[[test_details alloc]initWithNibName:@"test_details" bundle:nil];
[self.navigationController pushViewController:aSecondPageController animated:NO];
}
我有两个xib文件test_details_iPhone.xib
和test_details_iPad.xib
在我的testdetails.m
中- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
NSLog(@"it is coming here in second view");
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
NSLog(@"it has been returned ");
return self;
}
日志
2013-04-25 11:06:17.191 app_gototest[3067:207] login button has been pressed
2013-04-25 11:06:17.194 app_gototest[3067:207] In init
2013-04-25 11:06:17.195 app_gototest[3067:207] it is coming here in second view
2013-04-25 11:06:17.196 app_gototest[3067:207] it has been returned
视图未加载到视图上。我想我错过了什么。
申请:appDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) {
self.viewController = [[ViewController alloc] initWithNibName:@"ViewController_iPhone" bundle:nil];
} else {
self.viewController = [[ViewController alloc] initWithNibName:@"ViewController_iPad" bundle:nil];
}
self.window.rootViewController = self.viewController;
[self.window makeKeyAndVisible];
return YES;
}
**我正在尝试Jasper Blue的方法**
argc int 1
argv char ** 0xbfffed2c
*argv char * 0xbfffee44
在appDeleagte.m
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
UINavigationController* navigationController;
// Override point for customization after application launch.
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) {
self.viewController = [[ViewController alloc] initWithNibName:@"ViewController_iPhone" bundle:nil];
navigationController = [[UINavigationController alloc] initWithRootViewController:_viewController];
} else {
self.viewController = [[ViewController alloc] initWithNibName:@"ViewController_iPad" bundle:nil];
navigationController = [[UINavigationController alloc] initWithRootViewController:_viewController];
}
self.window.rootViewController = navigationController;
[self.window makeKeyAndVisible];
return YES;
}
答案 0 :(得分:2)
你的代码看起来没问题,所以必须是self.navigationController是nil。 。 。你有没有设置导航控制器?
要使代码正常工作,您当前的UIViewController需要包含在UINavigationController中。 。 。您可以将UINavigationController设置为应用程序委托中的根视图控制器,如下所示:
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
UINavigationController* navigationController;
// Override point for customization after application launch.
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) {
self.viewController = [[ViewController alloc] initWithNibName:@"ViewController_iPhone" bundle:nil];
navigationController = [UINavigationController alloc] initWithRootViewController:viewController];
} else {
self.viewController = [[ViewController alloc] initWithNibName:@"ViewController_iPad" bundle:nil];
navigationController = [UINavigationController alloc] initWithRootViewController:viewController];
}
self.window.rootViewController = navigationController;
[self.window makeKeyAndVisible];
return YES;
}
注意:您可以稍微清理上面的代码,但是您得到的图片就是您必须将根视图控制器设置为导航控制器,如下所示:
self.window.rootViewController = navigationController;
如果你愿意,你也可以制作一个自定义的根视图控制器,但上面的内容将让你实现你想要做的事情 - UINavigationController将在你的应用程序中用作导航系统。
答案 1 :(得分:0)
您的导航控制器为零。所以用
替换你的didFinishLaunchingWithOptions方法- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions: (NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) {
self.viewController = [[ViewController alloc] initWithNibName:@"ViewController_iPhone" bundle:nil];
} else {
self.viewController = [[ViewController alloc] initWithNibName:@"ViewController_iPad" bundle:nil];
}
UINavigationController *navcontroller = navigationController = [UINavigationController alloc] initWithRootViewController:self.viewController];
self.window.rootViewController = navcontroller;
[self.window makeKeyAndVisible];
return YES;
}
现在你有一个导航控制器,所以对self.navigationController的push方法的调用应该推送第二个控制器。
答案 2 :(得分:0)
请尝试
- (IBAction)Actionlogin:(id)sender {
NSLog(@" login button has been pressed");
NSLog(@"In init");
test_details *aSecondPageController=[[test_details alloc]initWithNibName:@"test_details_iPhone" bunndle:nil];
[self.navigationController pushViewController:aSecondPageController animated:NO];
}
// test_details_iPhone.xib and test_details_iPad.xib
//Change initWithNibName@"test_details" with test_details_iPhone or test_details_iPad