如何将第一个简单的单视图viewController更改为导航viewController?

时间:2012-04-25 08:29:55

标签: iphone ios uiviewcontroller uinavigationcontroller uitabbarcontroller

我首先想加载一个简单的viewController,它显示一些选项,然后点击一些按钮我想加载navigationController或tabbarController,具体取决于按钮点击。我怎么能这样做?

4 个答案:

答案 0 :(得分:2)

当我想切换视图时,我会替换窗口上的根视图控制器。

例如,在我的应用程序中,我首先显示加载屏幕,然后将视图切换到登录屏幕。

要执行此操作,您需要对应用委托的引用,然后您可以访问窗口属性并替换根视图控制器:

AppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];
LoginViewController *loginVC = [[LoginViewController alloc] init];              
appDelegate.window.rootViewController = loginVC; 

答案 1 :(得分:1)

在你的simpleViewController中:

- (IBAction) yourButtonAction:(id)sender
{
   UIViewController *Vc = [[theViewControllerYouWantToShow alloc]init];
  UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:Vc];
  [self  presentModalViewController:nav animated:YES];
}

修改:

您有三个选项来显示您的viewController内容:

  1. 使用presentModalViewController:

  2. 作为上述示例
  3. 将viewController视图作为子视图添加到当前viewController。 在您的情况下:[simpleViewController.view addSubView:nav.view];

  4. 3.如果您的简单ViewController是导航根viewController,您可以将其他viewControllers推送到其导航堆栈。

答案 2 :(得分:1)

在appdelegate.h中

@property (strong, nonatomic) id<UIApplicationDelegate>delegate;

在appdelgate.m

@synthesize delegate;

在我的第一个viewController的.h文件

AppDelegate *myappDelegate;
-(IBAction)start:(id)sender;

在我的第一个viewController的.m文件

-(IBAction)start:(id)sender
{
    NSLog(@"Start Button is clicked");
    mvc = [[MasterViewController alloc]initWithNibName:@"MasterViewController" bundle:nil];
    myappDelegate = [[UIApplication sharedApplication]delegate];
    myappDelegate.navigationController = [[UINavigationController alloc]initWithRootViewController:mvc];
    myappDelegate.window.rootViewController = myappDelegate.navigationController;
    [myappDelegate.window makeKeyAndVisible]; 
}

答案 3 :(得分:0)

点击here。这个链接用于如何创建您想要的导航控制器。我已在此链接中编写代码。我希望它会对你有所帮助。