[英]How to push viewcontroller from appdelegate in storyboard inside navigation controller
我在我的項目中使用SWRevealViewController,我想在應用程序收到通知時打開一個特定的控制器。 我嘗試了很多解決方案,但沒有任何效果。
我通過使用故事板來關注這個http://www.appcoda.com/ios-programming-sidebar-navigation-menu/ 。 我的故事板設計如下:
當應用程序收到通知時,我想在其導航控制器中加載照片視圖控制器。 我嘗試使用AppDelegate中的以下代碼:
UIStoryboard *st = [UIStoryboard storyboardWithName:@"Main" bundle: nil];
photoViewController *descController = (PhotoViewController*)[st instantiateViewControllerWithIdentifier: @"photoView"];
UINavigationController *frontNavigationController = [[UINavigationController alloc] initWithRootViewController:descController];
SidebarTableViewController *rearViewController = (SidebarTableViewController*)[st instantiateViewControllerWithIdentifier: @"menuController"];
SWRevealViewController *mainRevealController = [[SWRevealViewController alloc] init];
mainRevealController.rearViewController = rearViewController;
mainRevealController.frontViewController= frontNavigationController;
self.window.rootViewController =nil;
self.window.rootViewController = mainRevealController;
[self.window makeKeyAndVisible];
這可行,但創建了一個新的Navigtion控制器,我需要的是使用故事板中已經定義的控制器,因為它具有特定的屬性。
任何想法?
謝謝
實際上SwLRevalViewController
取得了Root
的所有權,所以這樣做
在Appdelegate.m
//initially navigate to your Main controller on SWL
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult result))completionHandler
{
NSString *alert = userInfo[@"aps"][@"redirect_url"];
[[NSUserDefaults standardUserDefaults]setObject:alert forKey:@"itemType"];
[[NSUserDefaults standardUserDefaults]synchronize];
UIStoryboard *mainStoryboard = [UIStoryboard storyboardWithName:@"Main" bundle: nil];
SWRevealViewController *main = (SWRevealViewController *)[mainStoryboard instantiateViewControllerWithIdentifier:@"SWRevealViewController"];
self.window.rootViewController = main;
}
//它會自動重定向到根控制器
在該主視圖控制器上ViewDidLoad
檢查/導航到您的照片視圖控制器
NSString *getType=[[NSUserDefaults standardUserDefaults]objectForKey:@"itemType"];
if ([gettypeofItem isEqualToString:@"something"])
{
yourPhotoViewController *ivc=[self.storyboard instantiateViewControllerWithIdentifier:@"yourPhotoViewController"];
[self.navigationController pushViewController:ivc animated:YES];
}
else
{
// do nothing
}
您可以使用以下方法
NSString *storyBoardName=@"Main_iPad";
if (isPhone)
storyBoardName=@"Main_iPhone";
UIStoryboard *storyBoard=[UIStoryboard storyboardWithName:storyBoardName bundle:nil];
PlayVideoBySocketsScene *controller=[storyBoard instantiateViewControllerWithIdentifier:@"playVideoUsingSockets"];
float disptachTime=2.0;
if (value)
{
disptachTime=1.0;
}
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(disptachTime * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
[[[[UIApplication sharedApplication] keyWindow] rootViewController] presentViewController:controller animated:YES completion:nil];
請嘗試這可能會對你有所幫助。 在這個當你點擊通知這個方法時我們先調用appdelegate
-(void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
{
UINavigationController *navController = (UINavigationController *)self.window.rootViewController;
NotificationViewController *notificationViewController = [[NotificationViewController alloc] init];
[navController.visibleViewController.navigationController pushViewController:notificationViewController];
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.