Add ViewControllers to Stack and Pop back When required - ios

I want to navigate to specific Class when Click on Notification in Notification bar and after that in that particular class if I click back button I need to Navigate to previous according to requirement.
Can anyone please help.
I have added ViewController to Stack this way
(BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
UIStoryboard *mainStoryboard = [UIStoryboard storyboardWithName:#"Main" bundle: nil];
SplashViewController *splashViewCon = (SplashViewController *)[mainStoryboard instantiateViewControllerWithIdentifier: #"SplashId"];
AgreeViewController *agreeViewCon = (AgreeViewController *)[mainStoryboard instantiateViewControllerWithIdentifier: #"agreeView"];
ViewController *loginViewCon = (ViewController *)[mainStoryboard instantiateViewControllerWithIdentifier: #"loginView"];
LanguageViewController *languageViewCon = (LanguageViewController *)[mainStoryboard instantiateViewControllerWithIdentifier: #"languageView"];
LandingScreenViewController *landingViewCon = (LandingScreenViewController *)[mainStoryboard instantiateViewControllerWithIdentifier: #"landingScreenId"];
HomeScreenViewController *homeViewCon = (HomeScreenViewController *)[mainStoryboard instantiateViewControllerWithIdentifier: #"homeScreen"];
OrderConfirmationViewController *orderconfirmViewCon = (OrderConfirmationViewController *)[mainStoryboard instantiateViewControllerWithIdentifier: #"orderConfirmationVC"];
ThankYouViewController *thankViewCon = (ThankYouViewController *)[mainStoryboard instantiateViewControllerWithIdentifier: #"ThankyouId"];
UploadQueueScreenViewController *uploadViewCon = (UploadQueueScreenViewController *)[mainStoryboard instantiateViewControllerWithIdentifier: #"uploadQueue"];
NewOrderViewController *newOrderViewCon = (NewOrderViewController *)[mainStoryboard instantiateViewControllerWithIdentifier: #"neworderView"];
MessageViewController *msgViewCon = (MessageViewController *)[mainStoryboard instantiateViewControllerWithIdentifier: #"MessageInboxscreen"];
MessageDetailsViewController *msgdetailsViewCon = (MessageDetailsViewController *)[mainStoryboard instantiateViewControllerWithIdentifier: #"msgDetailsView"];
NSArray *stack = [NSArray arrayWithObjects:splashViewCon,agreeViewCon,loginViewCon,languageViewCon,landingViewCon,homeViewCon,orderconfirmViewCon,thankViewCon,uploadViewCon,newOrderViewCon,msgViewCon,msgdetailsViewCon, nil];
navController.viewControllers = stack;
UILocalNotification *notification = [launchOptions objectForKey:UIApplicationLaunchOptionsLocalNotificationKey];
if (notification)
{
MessageDetailsViewController *msgdetailsViewCon = (MessageDetailsViewController *)[mainStoryboard instantiateViewControllerWithIdentifier: #"msgDetailsView"];
self.window.rootViewController = msgdetailsViewCon;
}
}
So ,I want pop back to specific viewController

Find your viewcontroller in stack and pop it. This code may be help you find it.
for (YourViewController *controller in self.navigationController.viewControllers)
{
if ([controller isKindOfClass:[YourViewController class]])
{
[self.navigationController popViewController:controller animated:YES];
break;
}
}

As far as I know there is no generic class avaialbe. Try using the NSMutableArray, add via addObject and get first/last via objectAtIndex and removeObjectAtIndex.
You might wan to implement your own code to use those as a Stack.
Cheers :)

Related

go to specific post/comment/profile after click push notifications in iOS 9

I have tried many code in my didReceiveRemoteNotification function in appdelegate.m
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler {
//Code
}
but didn`t work as expected.
With this code, doesn`t navigate to my profile view controller after I click the notification:
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"Main" bundle:nil];
ProfileViewController *controller = [storyboard instantiateViewControllerWithIdentifier:#"profileIdentifier"];
controller.userID = #"123";
controller.userName = #"calvinsug";
UINavigationController *navigationController = (UINavigationController *)self.window.rootViewController;
[navigationController pushViewController:controller animated:YES];
I have tried this also but didn`t navigate to the profile controller
[self.window.rootViewController presentViewController:controller animated:YES completion:NULL];
Finally, I try this code, but it is weird because it doesn't have back button (I think the cause is the profile controller become the initial view).
self.window = [[UIWindow alloc]
initWithFrame:UIScreen.mainScreen.bounds];
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"Main" bundle:nil];
ProfileViewController *controller = [storyboard instantiateViewControllerWithIdentifier:#"profileIdentifier"];
controller.userID = #"123";
controller.userName = #"calvinsug";
self.window.rootViewController = controller;
[self.window makeKeyAndVisible];
Any one have solution?
edit:
I think we can not set our target View controller to become a Root View Controller, because when we click notifications in other apps, we go to main screen first, then go to the specific view controller immediately
Try this:
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"Main" bundle:nil];
ProfileViewController *controller = [storyboard instantiateViewControllerWithIdentifier:#"profileIdentifier"];
controller.userID = #"123";
controller.userName = #"calvinsug";
UINavigationController *navController = [[UINavigationController alloc]initWithRootViewController:controller];
self.window.rootViewController = navController;
`

Correct implementation of opening storyboard view controller from UIApplicationShortcutItem in objective-c

This is the code I have got for trying to open a view controller from a storyboard. I am getting error "expected identifier" on
UINavigationController controller = [(UINavigationController)[[mainStoryboard instantiateViewControllerWithIdentifier: #"ARViewController"];
Can someone please help.
- (void)application:(UIApplication *)application performActionForShortcutItem:(UIApplicationShortcutItem *)shortcutItem completionHandler:(void (^)(BOOL))completionHandler {
NSLog(#"%#", shortcutItem.type);
if ([shortcutItem.type isEqualToString:#"ADD OWN STRING HERE"]) {
UINavigationController *navigationController = (UINavigationController *)self.window.rootViewController;
UIStoryboard *mainStoryboard = [UIStoryboard storyboardWithName:#"Main" bundle: nil];
UINavigationController *controller = [(UINavigationController*)[[mainStoryboard instantiateViewControllerWithIdentifier: #"ARViewController"]];
[navigationController pushViewController:controller animated:YES];
}
}
#end
The amount and locations of your [ and ] didn't match (two [ too many, one ] too many). It should be:
UINavigationController *controller = (UINavigationController*)[mainStoryboard instantiateViewControllerWithIdentifier: #"ARViewController"];

How to push viewcontroller from appdelegate in storyboard inside navigation controller

I am using SWRevealViewController in my project, and I want to open a particular controller when the app receives a notification. I have tried so many solutions but nothing works.
I follow this http://www.appcoda.com/ios-programming-sidebar-navigation-menu/ by using storyboard. My storyboard is designed as below:
When the application receives a notification I want to load Photo view controller within its navigation controller. I tried with the following code in the 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];
This works, but creates a new Navigtion controller, and what I need is to use the one already defined in the storyboard, since it has specific properties.
Any idea?
Thanks
actually SwLRevalViewController Taken the ownership of Root, so do like this
in 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;
}
//it automatically redirect to your root controller
on that main view controller ViewDidLoad check / Navigate to your photo view controller
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
}
You can use following method
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];
please try this it may help you. in this when you click on notification this method us called first insted for appdelegate
-(void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
{
UINavigationController *navController = (UINavigationController *)self.window.rootViewController;
NotificationViewController *notificationViewController = [[NotificationViewController alloc] init];
[navController.visibleViewController.navigationController pushViewController:notificationViewController];
}

Pushing view controller from AppDelegate

I am just beginning to learn how to implement Push notifications with iOS and Parse.
At the moment, I can send push notifications from the Parse dashboard to the app,the notification is received, as I can see in the Xcode console, but now my issue is that I am not able to open a view controller from the AppDelegate.
This is my code:
- (void)application:(UIApplication *)application
didReceiveRemoteNotification:(NSDictionary *)userInfo
fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))handler {
// Create empty photo object
NSString *photoId = [userInfo objectForKey:#"p"];
NSLog(#"P=%#",photoId);
UIStoryboard *mainStoryboard = [UIStoryboard storyboardWithName:#"Main" bundle: nil];
Aceptado *targetViewController = (Aceptado*)[mainStoryboard instantiateViewControllerWithIdentifier:#"servicio_aceptado"];
UINavigationController *controlador = [[UINavigationController alloc]init];
[controlador pushViewController:targetViewController animated:YES];
}
As I said, the console show the log NSLog(#"P=%#",photoId) , the notification is received, but the view controller is not shown, any warning or error is shown.
Any help is welcome.
I have solved my problem like this:
UIStoryboard *mainStoryboard = [UIStoryboard storyboardWithName:#"Main"
bundle: nil];
Aceptado *controller = (Aceptado*)[mainStoryboard
instantiateViewControllerWithIdentifier: #"servicio_aceptado"];
controller.id_not =_id_not;
controller.idemployee = _idemployee;
controller.fullname = _fullname;
controller.phone = _phone;
controller.photo = _photo;
controller.dni = _dni;
controller.licenseplate = _licenseplate;
controller.model = _model;
controller.color = _color;
controller.latitude = _latitude;
controller.longitude = _longitude;
self.window.rootViewController = controller;
Now I can pass values to the target view controller from the AppDelegate file, keeping the SWRevealViewController structure.
Thank to all of you for your proposals.
try this code.
UIStoryboard *mainStoryboard = [UIStoryboard storyboardWithName:#"Main" bundle: nil];
Aceptado *targetViewController = (Aceptado*)[mainStoryboard instantiateViewControllerWithIdentifier:#"servicio_aceptado"];
UINavigationController *navVc=(UINavigationController *) self.window.rootViewController;
[navVc pushViewController: targetViewController animated:YES];
may help you.
Try this once.
UIStoryboard *storyBoard = [UIStoryboard storyboardWithName:#"Main" bundle:nil];
Slider1ViewController *viewcontroller = [storyBoard instantiateViewControllerWithIdentifier:#"Slider1ViewController"];
UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:viewcontroller];
[navController setViewControllers: #[viewcontroller] animated: YES];
[self.revealViewController setFrontViewController:navController];
[self.revealViewController setFrontViewPosition: FrontViewPositionLeft animated: YES];
You should not create new NavigationController, but use one existing. Usually it is rootViewController property of your key window.
Try this:
UINavigationController *controlador = self.window.rootViewController;
If you want to display your targetViewController in SWRevealViewController you may change your code as this:
SWRevealViewController *controlador = (SWRevealViewController)self.window.rootViewController;
[controloador pushFrontViewController:targetViewController animated:YES];
If you need display targetViewController over SWRevealViewController, use this:
WRevealViewController *controlador = (SWRevealViewController)self.window.rootViewController;
[controloador presentViewController:targetViewController
animated:YES
completion:nil];
If you need just replace SWRevealViewController with targetViewController use this:
self.window.rootviewController=targetViewController;
But don't forget save your SWRevealViewController in some another property.
Try this:
SomePageViewController *someVC = [self.storyboard instantiateViewControllerWithIdentifier:#"SomeVC"];
self.navController = [[UINavigationController alloc] initWithRootViewController:someVC];
self.window.rootViewController = self.navController;
[self.window makeKeyAndVisible];
If you want to push a view controller into navigation stack from appDelegate you need to cast your rootView controller as UINavigation Controller

Cannot push viewcontroller from appdelegate

In my window base application I need to navigate to editorview from my appdelegate when i click on the second item of my tabbarcontroller it will shows an actionsheet with three option.
actionsheet works fine, it will shows an actionsheet if you choose the second item from tabbar.
But i need to push to the other view when i choose the first option of my action sheet
i declare my actionsheet in appdelegate.
i already implement the actionsheetdelegate
(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
the method is trigerred and it can shows if i do nslog. but it cannot push to the viewcontroller that i want..
here's my code:
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
if (buttonIndex == 0)
{
DetailViewController *v2 = [[DetailViewController alloc] init];
NSLog(#"PUSH TRIGER");
[self.window.rootViewController.navigationController pushViewController:v2 animated:YES];
}
}
NOTE: I already embed my tabbarcontroller and navigation controller in my storyboard
It is full of bad answer related to this issue.
I hope mine fits your requirements.
1.- Inside of
- (void)application:(UIApplication*)application didReceiveRemoteNotification:(NSDictionary*)userInfo
// 1.- First: instantiate the navigationController. Normally the rootviewcontroller
UINavigationController *navigationController = (UINavigationController *)self.window.rootViewController;
// 2.- Second: Instantiate Storyboard
// It might be tagged MainStoryBoard. Be careful if you've changed it
UIStoryboard *mainStoryboard = [UIStoryboard storyboardWithName:#"MainStoryboard" bundle: nil];
// 3.- Third instantiate the VC you want to navigate
// In my particular case it is called IniciarSliderViewController . Don't forget to import it in the appdelegate. Also pay attention to tagged correctly. In my case MenuSlider
IniciarSliderViewController *controller = (IniciarSliderViewController*)[mainStoryboard instantiateViewControllerWithIdentifier: #"MenuSlider"];
//4.- And last one, you can now push as you usually do in the VC
[navigationController pushViewController:controller animated:YES];
Don't hesitate to ask if you still have problems
Do like this
What did you assign VC for UITabBarController ? I bet UIViewController? Try assign UINavigationController instead of UIViewController
synthesize UINavigationController *navController;
self.navController = [[UINavigationController alloc] init];
SomeViewController *viewController = [[SomeViewController alloc] initWithNibName:#"SomeViewController" bundle:nil];
self.navController.viewControllers = [NSArray arrayWithObject:viewController];
UITabBarController *tabBarController = [[UITabBarController alloc] init];
[tabBarController setViewControllers:[NSArray arrayWithObjects:
self.navController, nil]];
Then on your actionsheet
[self.navController pushViewController:v2 animated:YES];
EDIT for storyboard
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"MainStoryboard" bundle:nil];
self.navController = (UINavigationController*) [storyboard instantiateViewControllerWithIdentifier:#"MyNavController"];
Hope that helps
instead of this self.window.rootViewController.navigationController
use UINavigationController object

Resources