推送ViewController不起作用

时间:2013-05-24 11:56:56

标签: iphone ios objective-c

我尝试从一个viewcontroller导航到另一个viewcontroller以传递一个对象,我用它

[self.navigationcontroller pushviewcontroller:VC2 animated : YES];

但没有奏效。 请我必须做什么

4 个答案:

答案 0 :(得分:2)

试试这个

在App Delegate.m

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

并在View Controller.m中

-(IBAction)buttonTapped:(id)sender{
   UIViewController vc2 = [[UIViewController alloc]initWithNibName:@"UCPAccuracyTableView" bundle:nil];
   [self.navigationController pushViewController:vc2 animated:YES];
   [vc2 release]; //if u r nt using ARC
}

答案 1 :(得分:2)

iF你想推送一个viewcontroller,它不是导航控制器使用下面的代码

    UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:[NSBundle mainBundle]];
yourviewcontroller *cdvc = [storyboard instantiateViewControllerWithIdentifier:@"StoryboardIdentifierName Of the viewcontroller you have to push"];
[self.navigationController pushViewController:cdvc animated:YES];

答案 2 :(得分:0)

在Delegate.m中您需要初始化导航控制器。

RootViewController *rootController = [[RootViewController alloc] initWithNibName:@"Your nib name" bundle:nil];

UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:rootController];

[self.window setRootViewController:navigationController];

之后,您可以从一个控制器导航到另一个控制器。由

[self.navigationcontroller pushviewcontroller:VC2 animated : YES];

答案 3 :(得分:0)

制作导航控制器的对象然后按

UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:vc];

[navController pushviewcontroller:VC2 animated : YES];