[英]How to push Viewcontroller from rootViewController in iOS
I have 2 ViewController. 我有2个ViewController。 loginViewControl which sets to rootViewControoler :
设置为rootViewControoler的loginViewControl:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
loginViewControl = [[LoginTab alloc] init];
self.window.rootViewController = loginViewControl;
[self.window makeKeyAndVisible];
return YES;
}
And I create StatusViewController : *.h 然后我创建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
Now I want to push StatusViewController from loginViewControl(rootviewcontroller).I used below code but it not work. 现在我想从loginViewControl(rootviewcontroller)推送StatusViewController。我使用了以下代码,但它不起作用。
- (IBAction)statusButtonClick:(id)sender;
{
StatusViewController *statusView = [[StatusViewController alloc]init];
[self.navigationController pushViewController:statusView animated:YES];
[statusView release];
}
Do you have any suggestions? 你有什么建议吗? Thanks in advance
提前致谢
You not setting UINavigationViewcontroller at the didFinishLaunchingWithOptions
of Window's RootViewcontroller, We can't Push a UIViewController
without UINavigationViewcontroller
在你不设置UINavigationViewcontroller
didFinishLaunchingWithOptions
窗口的RootViewController的,我们不能把一个UIViewController
没有UINavigationViewcontroller
So First Set LoginViewController as a RootViewController of UINavigationViewcontroller
like Bellow:- 因此,首先将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;
}
EDIT 编辑
Connect your StatusViewcontroller's
view with File owner like bellow:- 将您的
StatusViewcontroller's
视图与文件所有者连接,例如波纹管:-
your push method like this:- 你的推送方法是这样的:
- (IBAction)statusButtonClick:(id)sender;
{
StatusViewController *statusView = [[StatusViewController alloc]initWithNibName:@"StatusViewController" bundle:nil];
[self.navigationController pushViewController:statusView animated:YES];
}
Unless your login view controller is a navigation controller, self.navigationController in statusButtonClick will return nil and so this won't work. 除非您的登录视图控制器是导航控制器,否则statusButtonClick中的self.navigationController将返回nil,因此这将不起作用。 Maybe you can try:
也许您可以尝试:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
loginViewControl = [[LoginTab alloc] init];
self.window.rootViewController = [[UINavigationController alloc] initWithRootViewController:loginViewControl];
[self.window makeKeyAndVisible];
return YES;
}
This will enclose a loginViewController in a navigationController which will permit you to push another viewController in the navigationController's stack. 这会将loginViewController封装在navigationController中,这将允许您将另一个viewController推入navigationController的堆栈中。
- (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]; }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.