如何从iOS中的rootViewController推送Viewcontroller

时间:2013-08-09 07:50:01

标签: ios objective-c uiviewcontroller

我有2个ViewController。 loginViewControl设置为rootViewControoler:

    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{

    loginViewControl = [[LoginTab alloc] init];
    self.window.rootViewController = loginViewControl;
    [self.window makeKeyAndVisible];

    return YES;
}

我创建了StatusViewController: * .H

 @interface StatusViewController : UIViewController<UITabBarControllerDelegate>
{
    IBOutlet UITabBarController *tabBarController;

    IBOutlet UIButton *UploadButton;
    IBOutlet UIButton *ConvertorButton;
    IBOutlet UIButton *CompletedButton;
}
@property (nonatomic, retain) IBOutlet UITabBarController *tabBarController;
@end

现在我想从loginViewControl(rootviewcontroller)推送StatusViewController。我使用下面的代码,但它不起作用。

 - (IBAction)statusButtonClick:(id)sender;
{
    StatusViewController *statusView = [[StatusViewController alloc]init];
    [self.navigationController pushViewController:statusView animated:YES];

    [statusView release];   

}

你有什么建议吗?提前致谢

3 个答案:

答案 0 :(得分:0)

你没有在Window的RootViewcontroller的didFinishLaunchingWithOptions设置UINavigationViewcontroller,我们不能在没有UIViewController的情况下推送UINavigationViewcontroller

首先将LoginViewController设置为UINavigationViewcontroller的RootViewController,如Bellow: -

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{

    loginViewControl = [[YourLoinViewcontroller alloc] initWithNibName:@"YourLoinViewcontroller" bundle:nil];
    UINavigationController *objNavigationController=[[UINavigationController alloc]initWithRootViewController:loginViewControl];

    self.window.rootViewController = objNavigationController;
    [self.window makeKeyAndVisible];

    return YES;
}

修改

将您的StatusViewcontroller's视图与文件所有者联系,如下: -

enter image description here

你的推送方式如下: -

- (IBAction)statusButtonClick:(id)sender;
{
    StatusViewController *statusView = [[StatusViewController alloc]initWithNibName:@"StatusViewController" bundle:nil];
    [self.navigationController pushViewController:statusView animated:YES];

}

答案 1 :(得分:0)

除非您的登录视图控制器是导航控制器,否则statusButtonClick中的self.navigationController将返回nil,因此这将不起作用。 也许你可以试试:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{

    loginViewControl = [[LoginTab alloc] init];
    self.window.rootViewController = [[UINavigationController alloc] initWithRootViewController:loginViewControl];
    [self.window makeKeyAndVisible];

    return YES;
}

这将在navigationController中包含一个loginViewController,它允许你在navigationController的堆栈中推送另一个viewController。

答案 2 :(得分:0)

- (IBAction)statusButtonClick:(id)sender; {

    // If used StoryBoard
    UIStoryboard *story=[UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil];
    StatusViewController *statusView = [story instantiateViewControllerWithIdentifier:@"StatusViewController"];
    [self.navigationController pushViewController:statusView animated:YES];

    // If used XIB
     StatusViewController *statusView = [[StatusViewController alloc]initWithNibName:@"StatusViewController" bundle:nil];
    [self.navigationController pushViewController:statusView animated:YES]; }