iOS standalone UIViewcontroller , possible? - ios

I'm using AppDelegate method. Why?
When the user click the okay Button, it will force the phone to redirect to a UIControllerView, which is naked to the user eyes.
- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification
{
UIApplicationState state = [application applicationState];
if (state == UIApplicationStateActive) {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Daily Vibes"
message:notification.alertBody
delegate:self cancelButtonTitle:#"Wokay"
otherButtonTitles:nil];
[alert show];
}
// Request to reload table view data
[[NSNotificationCenter defaultCenter] postNotificationName:#"reloadData" object:self];
// Set icon badge number to zero
application.applicationIconBadgeNumber = 0;
}
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
NSString *title = [alertView buttonTitleAtIndex:buttonIndex];
if([title isEqualToString:#"Wokay"])
{
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"Main" bundle:nil];
UIViewController *vc = [storyboard instantiateViewControllerWithIdentifier:#"Vibes"];
[self.window makeKeyAndVisible];
[self.window.rootViewController presentViewController:vc animated:YES completion:nil];
}
}
I'm getting error of the following:
Warning: Attempt to present <UIViewController: 0x109721840> on <UINavigationController: 0x10921abe0> whose view is not in the window hierarchy!
Is it possible to accomplish it?
Scenario:
User set time via "DatePicker" then when alarm pop via AppDelegate, when the user click Okay. Then the user will be redirected to a page where a harmony message is displayed via UILabel. But the user has only one button on that page "Back". He has to set another time just to view the message via redirecting.
Example pic:

its because window.rootViewController has type UINavigationController, not UIViewController.
you should initialise a NavigationController.
UINavigationController *mainNavigationController = [[UINavigationController alloc] initWithRootViewController:yourUIViewController];
self.window.rootViewController = mainNavigationController;

UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"Main" bundle:nil];
UIViewController *vc = [storyboard instantiateViewControllerWithIdentifier:#"Vibes"];
self.window.rootViewController = vc;
[self.window makeKeyAndVisible];
That's the answer...

Related

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];
}

Push View Controller From AppDelegate and Tab Bar

My app is setup with a Tab Bar Controller as the RootViewController, and each Tab has a NavigationController in it. When certain actions are performed, I want the app to push a ViewController onto the screen. The flow of this is that when the app starts, or opens from background, it checks a stored NSDate and compares it to the current date. If the right condition is met, it shows a UIAlertView. If the button I named "Push" is selected, it runs the code to push the new view. This is the reason I need it to be ran from the AppDelegate, as there is no guarantee what tab may be open if the app is being used in the background. Since every tab contains a NavigationController, I thought I could run this from the AppDelegate:
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
if (alertView.tag == 100) {
if (buttonIndex == 0) {
//Cancel
NSLog(#"Cancel");
}
if (buttonIndex == 1) {
NSLog(#"OK");
[self.tabBarController.selectedViewController pushViewController:self.newView animated:YES];
}
}
}
I get a warning message that says UIViewController may not respond to -pushViewController:animated. Any suggestions as to what else I could do?
The return type for selectedViewController is UIViewController, so you need to tell the compiler that it's actually a navigation controller. You do that with a cast,
[(UINavigationController *)self.tabBarController.selectedViewController pushViewController:self.newView animated:YES];
Try this!!
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
if (buttonIndex == 0) {
NSLog(#"Cancel");
}else{
NSLog(#"Push");
[self loadTabbar];
}
}
-(void)loadTabbar{
UITabBarController *tabbarController = [[UITabBarController alloc]init];
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"Main" bundle:[NSBundle mainBundle]];
ViewControllerOne *tab1 = [storyboard instantiateViewControllerWithIdentifier:#"ViewControllerOne"];
UINavigationController *navi1 = [[UINavigationController alloc]initWithRootViewController:tab1];
tab1.tabBarItem.title = #"Tab1";
tab1.tabBarItem.image = [UIImage imageNamed:#"1.png"];
ViewControllerTwo *tab2 = [storyboard instantiateViewControllerWithIdentifier:#"ViewControllerTwo"];
UINavigationController *navi2 = [[UINavigationController alloc]initWithRootViewController:tab2];
tab2.tabBarItem.title = #"Tab2";
tab2.tabBarItem.image = [UIImage imageNamed:#"2.png"];
NSArray *tabArrays = [[NSArray alloc]initWithObjects:navi1,navi2, nil];
tabbarController.viewControllers = tabArrays;
tabbarController.tabBar.selectionIndicatorImage = [UIImage imageNamed:#"tabbar_selected.png"];
[self.window setRootViewController:tabbarController];
[self.window makeKeyAndVisible];
}
Comment here if this is the one you are looking forward!!

iOS redirect back to apps

I need help , googling also put me at no clue thus i'll post here for guidance and assist.
My situation is that I want the app delegate.m after when you press Ok, I want a simple redirect back to another view . Is that possible?
- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification
{
UIApplicationState state = [application applicationState];
if (state == UIApplicationStateActive) {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Daily Vibes"
message:notification.alertBody
delegate:self cancelButtonTitle:#"Okay"
otherButtonTitles:nil];
[alert show];
}
Is it possible to do ?? After they click the cancel button Okay then they'll just be redirect back to the apps a specific page???
Try this:
Make your app delegate conforms to the UIAlertViewDelegate protocol, and add this code:
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
NSString *title = [alertView buttonTitleAtIndex:buttonIndex];
if([title isEqualToString:#"Okay"])
{
NSLog(#"Okay was selected.");
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"MainStoryboard" bundle:nil];
MyViewController *vc = [storyboard instantiateViewControllerWithIdentifier:#"MyViewController"];
[(UINavigationController*)self.window.rootViewController pushViewController:vc animated:NO];
}
else if([title isEqualToString:#"Hello"])
{
NSLog(#"Hello was selected.");
}
}
The delegate captures the selected button, checks the title, and acts accordingly. In this case, since you are in the app delegate, you have to do a manual push of the view controller (you aren't in a view controller, so theres no pushing allowed)

Code not in window hierarchy root view [duplicate]

This question already has answers here:
Attempt to present UIViewController on UIViewController whose view is not in the window hierarchy
(38 answers)
Closed 8 years ago.
I'm facing a problem trying to link my UIViewController but I got my final error.
Attempt to present ViewController whose view is not in the window hierarchy
Here's my code :
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
NSString *title = [alertView buttonTitleAtIndex:buttonIndex];
if([title isEqualToString:#"Wokay"])
{
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"MainStoryboard" bundle:nil];
UIViewController *vc = [storyboard instantiateViewControllerWithIdentifier:#"Vibes"];
[self.window.rootViewController presentViewController:vc animated:YES completion:nil];
}
}
Code Error:
Warning: Attempt to present <ViewController: 0x110634bc0> on <Login: 0x10951e7f0> whose view is not in the window hierarchy!
It seems like that your UIViewController(Login) is not in Window Hierarchy.
You may be adding your LoginViewController as a subView in UIWindow.
If so, set it as the UIWindow's rootViewController
AppDelegate.m
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
//Other code parts
[self.window setRootViewController:loginViewController];
return YES;
}
OR
If you are adding LoginViewController's view as subView in any UIViewController(say FirstViewController), present it instead
In your FirstViewController.m,
-(void)viewDidLoad{
[super viewDidLoad];
LoginViewController *loginViewController ;//Instantiate LoginViewController here
[self presentViewController:loginViewController animated:NO completion:Nil];
}

View keeps empty after opened through an alert

there's an alert in my app and when the user clicks on its ok-button an new few opens: But this few keeps empty...no buttons, etc. are shown (somehow the background is correctly shown).
This alert opens at first launch of the app in AppDelegate.m file:
-(void) alertView:(UIAlertView *) alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
if (buttonIndex == 0) {
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
FirstVC *launchViewController = [[FirstVC alloc] init];
self.window.rootViewController = launchViewController;
[self.window makeKeyAndVisible];
}
}
Maybe it's wrong to use UIWindow.
Thanks to all of your answers. (FirstVC is the viewController shown after the alert-buttton is clicked)
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
NSUserDefaults* defaults = [NSUserDefaults standardUserDefaults];
if (! [defaults boolForKey:#"notFL"]) {
UIAlertView *alert = [[UIAlertView alloc]initWithTitle: #"Welcome"
message: #"..."
delegate: self
cancelButtonTitle:nil
otherButtonTitles:#"OK",nil];
[alert show];
[defaults setBool:YES forKey:#"notFL"];
}
}
You need to describe what controllers you have in the storyboard. What controller is the alert shown over when the app first opens? Your problem is that you're just alloc init'ing a FirstVC, not getting the one you set up in the storyboard. To get that instance, you need to do something like this:
FirstVC *launchViewController = [self.storyboard instantiateViewControllerWithIdentifier:#"FirstVC"];
This will work if you call it from the initial view controller that the alert is shown over. I think it would be better to put the coed you posted in the viewDidAppear method of that first controller.
If you want to do this in the app delegate, then you need to get a reference to the storyboard in a different way:
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"MainStoryboard" bundle:nil];
FirstVC *launchViewController = [storyboard instantiateViewControllerWithIdentifier:#"FirstVC"];

Resources