Present alert controller in main app window? - ios

In my model class i want to show alert when something bad happen (for example, server send an error). However, i dont want to bother with delegate or KVO.
Is there an easy way to show an alert for "main" window, to place a view above others?
Currently i use this code (not work, if placed out of viewController classes):
UIAlertController *vc = [UIAlertController alertControllerWithTitle:#"Произошла ошибка"
message:response
preferredStyle:UIAlertControllerStyleAlert];
[self presentViewController:vc animated:YES completion:^{
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
[vc dismissViewControllerAnimated:YES completion:nil];
});
}];
Obviously, classes that don't have a view cant use:
[self presentViewController:vc animated:YES completion:^{

as per solution of #Ashley Mills You can find top view controller with this method
- (UIViewController *)currentTopViewController
{
UIViewController *topVC = [[[[UIApplication sharedApplication] delegate] window] rootViewController];
while (topVC.presentedViewController)
{
topVC = topVC.presentedViewController;
}
if ([topVC isKindOfClass:[UINavigationController class]]) {
return [(UINavigationController *)topVC topViewController];
}
return topVC;
}
show you alert in return object of currentTopViewController method

You need to present the UIAlertController from a UIViewController. If you want the main view controller, you'll need to do this from your application delegate's window's rootViewController.
A nasty way to do this is to refer to UIApplication.sharedApplication().delegate.window.rootViewController
A better way would be to post a notification from your model method and listen for it in the App Delegate
Ideally though you should be looking at passing back an error from your model method to the calling view controller.
Or perhaps you could pass in your current view controller as a parameter to the model method

Related

Obj C - Present view controllers one after another

It may be simple, but i'm scratching my head to find out the issue.
I'm presenting a UINavigationController and once it's dismissed I need to present another one which is a UITabBarController, when I do that I get below error
Warning: Attempt to present < MyTabBarViewController: 0xXXXX > on <
ParentViewController: 0XXX> whose view is not in the window hierarchy!
UINavigationController *nav = [self.storyboard instantiateViewControllerWithIdentifier:#"myWeb"];
MyWebViewController *webVC = (MyWebViewController *)nav.topViewController;
[self presentViewController:webVC animated:YES completion:nil];
I can see MyWebViewController is presented without any issue, once the previous one is dismissed, I try to present next one like below, I'm getting the above warning.
MyTabBarViewController *trController = [[MyTabBarViewController alloc] init];
[self presentViewController:trController animated:NO completion:nil];
You need to present MyTabBarViewController only when the MyWebViewController has finished dismissing,
try adding a delay like this:
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 1 * NSEC_PER_SEC), dispatch_get_main_queue(), ^{
MyTabBarViewController *trController = [[MyTabBarViewController alloc] init];
[self presentViewController:trController animated:NO completion:nil];
});
if this works, you will need to get rid of that (in case you want to keep your code clean) and call a delegate method inside completion telling the parent that the MyTabBarViewController is ready to be presented.

Presenting an alert from a view controller that has closed

I have an iOS Objective-C application in which I am attempting to show a UIAlertController, from a UIViewController which is in the process of closing. I have tried adding this common workaround in AppDelegate:
- (UIViewController *)currentTopViewController
{
UIViewController *topVC = [[[[UIApplication sharedApplication] delegate] window] rootViewController];
while (topVC.presentedViewController)
{
topVC = topVC.presentedViewController;
}
return topVC;
}
Called with:
[appDelegate.currentTopViewController presentViewController:alert animated:YES completion:nil];
However this error is still appearing:
Warning: Attempt to present UIAlertController on MyViewController whose view is not in the window hierarchy!
Can anyone advise?
If you want to display an alert upon closing a viewcontroller, then you can just implement dismiss with completion block and display it there. For example:
[self dismissViewControllerAnimated:YES completion:^(void) {
// UIAlertController code here
}];
You can use UIWindow with a transparent UIViewController and then presenting the UIAlertController on it.
Below link provides a category on UIAlertController that has a show method in Objective-C. You can use it.
Link: https://stackoverflow.com/questions/26554894/how-to-present-uialertcontroller-when-not-in-a-view-controller

Unable to trigger Segue to manually load VC - self.navigationController is Nil?

EDIT
After some more digging around it has become apparent that if i try to call a segue from the view controller class after a fast application switch to/from Safari i get an exception stating that the segue does not exist. It definitely does, in the VC that is loaded on screen, and i have checked the spelling and it's 100% correct.
What makes it even weirder is that if i call that exact same method from an IBAction it works fine.
Here's the code i am using:
-(void)goToPhotoPage{
[self performSegueWithIdentifier:#"twitterAuthComplete" sender:self];
}
If i call this when the app resumes i get an exception, however if i call it from this IBAction, attached to a UIButton, with doing nothing different the segue works as expected and no exception.
- (IBAction)twitterToPhoto:(id)sender
{
[self goToPhotoPage];
}
Arrrrgrghrhhgrgrg!
ORIGINAL QUESTION
I am working on an iPad application that uploads user photos to Facebook/Twitter.
As part of the process i have to jump to Safari to do OAuth for twitter, the app then jumps back via a specific URL and get's the tokens e.t.c
However for some reason when the application re-awakes i cannot trigger any segue's or manually load any VC's.
I have a success block in my AppDelegae which get's called when the upload is complete which calls a method in the VC to close a spinner and segue to the success view.
The spinner stops as expected, but no matter what i try i cannot get the segue to work.
Everything is hooked up, the segue has an ID and i get no error or exceptions in the console, just nothing happens. The only way i can get code triggered segue to work after this point is to use a user trigger one connect to a UIButton, after that one complete they start to work again.
Here is the code with callback in my AppDelegate:
- (void)postToTwitter:(NSString*)postText image:(UIImage*)image
{
NSData *data = UIImageJPEGRepresentation(image, 0.5);
[_twitter postStatusUpdate:postText
mediaDataArray:#[data]
possiblySensitive:nil
inReplyToStatusID:nil
latitude:nil
longitude:nil
placeID:nil
displayCoordinates:nil
uploadProgressBlock:^(NSInteger bytesWritten, NSInteger totalBytesWritten, NSInteger totalBytesExpectedToWrite) {
} successBlock:^(NSDictionary *status) {
ViewController * vc = [[ViewController alloc]init];
[vc triggerSuccess];
} errorBlock:^(NSError *error) {
ViewController * vc = [[ViewController alloc]init];
[vc uploadFail:error];
}];
}
in the VC i have tried the following:
- (void)triggerSuccess
{
[self segueToSuccess];
}
- (void)segueToSuccess
{
[self hideMessage];
[self.navigationController performSegueWithIdentifier:#"approveSegue" sender:self];
}
As well as:
- (void)triggerSuccess
{
[self segueToSuccess];
}
- (void)segueToSuccess
{
[self hideMessage];
UIViewController *controller = [self.storyboard instantiateViewControllerWithIdentifier:#"done"];
[self.navigationController pushViewController:controller animated:YES];
}
In a moment of pure desperation i even tried:
- (void)triggerSuccess
{
[self segueToSuccess];
}
- (void)segueToSuccess
{
[self hideMessage];
//[self.navigationController performSegueWithIdentifier:#"approveSegue" sender:self];
[self dismissViewControllerAnimated:NO completion:nil];
[self.navigationController popToRootViewControllerAnimated:NO];
[self performSelector:#selector(segueToSuccessPart2) withObject:nil afterDelay:0.25];
}
- (void)segueToSuccessPart2
{
UIViewController *controller = [self.storyboard instantiateViewControllerWithIdentifier:#"done"];
[self.navigationController pushViewController:controller animated:YES];
}
I've obviously missed something, and i'm guessing it's to do with the application going into the background and the VC being "unloaded" from memory, and needing re-instatiating, but i'm not sure where to begin with that.
Any help would be greatly appreciated as i'm about to throw the computer out of the window . .
Thanks
Gareth
Edit 1
After doing some more troubleshooting it appears that during this call in the VC:
[self dismissViewControllerAnimated:NO completion:nil];
[self.navigationController popToRootViewControllerAnimated:NO];
self.navigationController is nil . . .
I'm guessing that is likely the cause of the issues i'm seeing?
Edit 2
So as suggested by Kelin, i am trying to make sure i retain the Navigation Controller.
I have done the following in AppDelegte:
#property (nonatomic, strong) UINavigationController *navController;
and
#synthesize navController;
But when i try to access this from the VC using the below code, it's still nil . .
AppDelegate *appDelegate = [[UIApplication sharedApplication]delegate];
UINavigationController *navController = appDelegate.navController;
[self dismissViewControllerAnimated:NO completion:nil];
[navController popToRootViewControllerAnimated:NO];
Edit 3
I have also added this to the AppDelegate, which means the Navigation Controller is no longer nil. But still nothing happens . . .
if (!self.navController)
{
self.navController = [[UINavigationController alloc]initWithNibName:#"Main" bundle:nil];
}
The problem is you trying to perform segue from storyboard using programmatically created View Controller.
You use -performSegueWithIdentifier: sender: completely wrong. In this method "sender" is a button, or any other control which action initialises segue, but not the view controller to be pushed.
Usually segue is created with storyboard. But if you really need to create it manually, call:
-[UIStoryboardSegue initWithIdentifier:source:destination:]
where source and destination are view controllers.
Read this: https://developer.apple.com/library/ios/recipes/xcode_help-interface_builder/articles-storyboard/StoryboardSegue.html
or this (about custom segues):
You can not do both dismiss and pop together either you have to pop or dismiss the view controller and
Every ViewController have it's own NavigationController which is default point out to navigation controller you added to AppDelegate.
So, You also can access it by self.navigationController (Edit 3)
Also check whether you initialised navigation controller in
AppDelegate
self.navController = [[UINavigationController alloc] initWithRootViewController:firstVC];

Issues presenting UIActivityViewController

I want to give a share screen throughout my app. I am using UIActivityViewController for that purpose. The problem is, as per your location in the app, the current root view controller can be of kind UINavigationController(Case1) or UIViewController(Case2).
I can present UIActivityViewController using
[viewController presentViewController:viewController animated:YES completion:nil];
But I have to get currently visible UIViewController of UINavigationController in (Case1), and root view controller itself in (Case2).
But how to detect which kind of root view controller is present & code accordingly?
Thanks.
Try:
UIViewController *root = [[[[[UIApplication sharedApplication] keyWindow] subviews] objectAtIndex:0] nextResponder];
and then:
if ([root isKindOfClass:[UINavigationController class]]) {
// Navigation Controller
} else {
// The other one
}
that should tell you which one is presented.
Use the above method to detect for a UINavigationControler;
- (void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated
{
//If this will be called it is a navigation controller
}
use performselector to start and stop rorating activity indicater
Here AI is activity indicater variable name
[self performSelector:#selector(animateAI) withObject:self afterDelay:0.1];
[self performSelector:#selector(stopAI) withObject:self afterDelay:0.9];
-(void)animateAI
{
[AI startAnimating];
}
-(void)stopAI
{
[AI stopAnimating];
}

Presentview(full screen) controller while dismissing View1 which is model view itself

I have an ipad app.
I am trying to open view 2 (kind of push view) full with entire screen. how normally do with push view or UIModalPresentationFullScreen. but my base view which is view 1 is also modal view.
so i was trying to open view 2 when view 1 get dismiss…
- (void) handleNewButton :(int)id
{
[self dismissViewControllerAnimated:YES
completion:^{
NewViewController *View2 = [NewViewController alloc] init];
View2.modalPresentationStyle = UIModalPresentationFullScreen;
View2.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
[self presentViewController: View2 animated:YES completion:nil];
}];
}
but my view 2 is not opening. i know i can not do push view. But is there any way to achieve it?.
When you do this dismissViewControllerAnimated the UIViewController (self in this case) is gone, in the sense that he is not on the screen anymore, if it has been released or not, that's another story. The reason for you to not be able to show the View2 (very poor name, it should at least ViewController2) is because you are trying to show it from a UIViewController that is not on the screen anymore.
So, what can you do?
The current self in the context of the handleNewButton method, in theory was presented by another UIViewController, that's from where you want to present your View2.
Probably the quickest way of implementing of what I said, would probably be with a notification described here. Although I would do it with a block, so when the self would be created, I would pass a dismissiCompletionBlock that would be called when that UIViewController was dismissed.
try to allocate NewViewController with nib name if you are not using storyboard,
[self dismissViewControllerAnimated:YES
completion:^{
NewViewController *n=[[NewViewController alloc]initWithNibName:#"NewViewController" bundle:nil];
View2.modalPresentationStyle = UIModalPresentationFullScreen;
View2.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
[self presentViewController: View2 animated:YES completion:nil];
}];
or if you are using storyboard get NewViewController using identifier.

Resources