我想使用pushViewController,但我不能。我记得我可以在xcode3中使用这段代码。
这是IBAction按钮中的代码
-(IBAction)changtoCal:(id)sender
{
NSLog(@"get in");
UINavigationController *cal_nav = [[UINavigationController alloc] initWithNibName:@"calculator" bundle:nil];
[self.navigationController pushViewController:cal_nav animated:YES];
}
它不推。为什么???你能告诉我怎么推它?
这是我的AppDelegate .H
@class ViewController;
@interface AppDelegate : UIResponder <UIApplicationDelegate>
{
UINavigationController *navigationController;
}
@property (strong, nonatomic) UIWindow *window;
@property (strong, nonatomic) ViewController *viewController;
@property (strong, nonatomic) IBOutlet UINavigationController *navigationController;
@end
的.m
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
self.viewController = [[ViewController alloc] initWithNibName:@"ViewController" bundle:nil];
self.window.rootViewController = self.viewController;
[_window addSubview:[navigationController view]];
[self.window makeKeyAndVisible];
return YES;
}
答案 0 :(得分:2)
您应该在IBAction中创建自定义UIViewController的实例,而不是新的UINavigationController
// change UICalculatorViewController to your view controller
UICalculatorViewController *cal_nav = [[UICalculatorViewController alloc] initWithNibName:@"calculator" bundle:nil];
[self.navigationController pushViewController:cal_nav animated:YES];
答案 1 :(得分:1)
我猜我UIViewController
未正确嵌入UINavigationController
。这意味着self.navigationController = nil
。
我不知道XIB,但您可以通过执行以下操作来修复它:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
self.viewController = [[ViewController alloc] initWithNibName:@"ViewController" bundle:nil];
UINavigationController *navc = [[UINavigationController alloc] initWithRootViewController:self.viewController];
self.window.rootViewController = navc;
[_window addSubview:navc.view];
[self.window makeKeyAndVisible];
return YES;
}