Long delay before view controller appears when using GCD and NSNotificationCenter - ios

Having odd behavior that I just can't figure out - when I'm pushing my second view controller onto a UINavigationController, there is a 5-10 second delay before the new view appears. The second view controller is being pushed based on an NSNotificationCenter notification.
The oddness: the delay only occurs when the event is posted from a background thread and not on the main thread (see comments in sample code below).
Why is there such a delay? This occurs on both simulator and device.
Here's the console output. As you can see, all logs appear in a timely manner, just not the 2nd view controller which suddenly appears after about 5-10 seconds.
2015-04-27 06:58:47.973 DelayedViews[49845:5005806] Background thread started
2015-04-27 06:58:49.978 DelayedViews[49845:5005806] Pushing second view controller
2015-04-27 06:58:49.979 DelayedViews[49845:5005806] Done pushing second view controller
2015-04-27 06:58:49.980 DelayedViews[49845:5005806] In completion block
2015-04-27 06:58:49.980 DelayedViews[49845:5005783] Back on main thread
And here's my simple example AppDelegate.m which demonstrates the issue.
#import "AppDelegate.h"
#interface AppDelegate ()
#property (nonatomic, strong) UINavigationController *navController;
#end
#implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
self.window = [[UIWindow alloc] initWithFrame: [[UIScreen mainScreen] bounds]];
self.navController = [[UINavigationController alloc] init];
self.window.rootViewController = self.navController;
UIViewController *vc = self.viewControllerOne;
[self.navController pushViewController:vc animated:YES];
// register for notification center
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(pushSecondViewController) name:#"PushSecondVC" object:nil];
// start background thread that posts notification and calls completion block
void (^completion)() = ^void() {
NSLog(#"In completion block");
dispatch_async(dispatch_get_main_queue(), ^{
NSLog(#"Back on main thread");
// If I do the notification here, the 2nd view controller is pushed immediately
// [[NSNotificationCenter defaultCenter] postNotificationName:#"PushSecondVC" object:self];
});
};
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^{
NSLog(#"Background thread started");
[NSThread sleepForTimeInterval:2]; // simulate some work
// if the notification is posted here, there is a 5-10 second delay on simulator before second VC appears
// even though the 2nd VC was pushed onto navigationcontroller.
[[NSNotificationCenter defaultCenter] postNotificationName:#"PushSecondVC" object:self];
// call completion block now
completion();
});
self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];
return YES;
}
- (UIViewController*)viewControllerOne {
UIViewController *vc = [[UIViewController alloc] init];
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(8, 100, 100, 100)];
label.text = #"View #1";
[vc.view addSubview:label];
return vc;
}
- (UIViewController*)viewControllerTwo {
UIViewController *vc = [[UIViewController alloc] init];
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(50, 200, 100, 100)];
label.text = #"View #2";
[vc.view addSubview:label];
return vc;
}
- (void)pushSecondViewController {
NSLog(#"Pushing second view controller");
UIViewController *vc = self.viewControllerTwo;
[self.navController pushViewController:vc animated:YES];
NSLog(#"Done pushing second view controller");
}
#end

Thanks, A-Live, that appears to be it.
I changed the pushSecondViewController to the following and it seems to work perfectly now. I wonder what is going on under-the-covers and why it takes so long for the view to appear?
- (void)pushSecondViewController {
if (![[NSThread currentThread] isMainThread]) {
dispatch_sync(dispatch_get_main_queue(), ^{
[self pushSecondViewController];
});
return;
}
NSLog(#"Pushing second view controller");
UIViewController *vc = self.viewControllerTwo;
[self.navController pushViewController:vc animated:NO];
NSLog(#"Done pushing second view controller");
}

Related

Can't pop iOS viewController. Not sure, but I think it's something with the Navigation Controller

I'm having trouble trying to pop a view
App Delegate
#implementation MAAppDelegate
#synthesize navController;
#synthesize detailViewController;
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Init the navController for the Master Detail View of the grade cells
UINavigationController *navController = [[UINavigationController alloc] init];
detailViewController = [[UIViewController alloc] init]; //step6
navController = [[UINavigationController alloc] initWithRootViewController:[[MAController alloc] init]]; //step7
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
self.window.backgroundColor = [UIColor whiteColor];
self.window.rootViewController = navController; //step8
[self.window makeKeyAndVisible];
// Set MAController as rootViewController
//self.window.rootViewController = [[MAController alloc] init];
self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];
// Use the insanely cool TSMessages to show network alerts
[TSMessage setDefaultViewController: self.window.rootViewController];
return YES;
}
First part of viewController
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
[self.navigationController setNavigationBarHidden:YES];
UIBarButtonItem *newBackButton = [[UIBarButtonItem alloc] initWithTitle:#"Home" style:UIBarButtonItemStyleBordered target:self action:#selector(home:)];
self.navigationItem.leftBarButtonItem=newBackButton;
Later, when I change the viewController
NSLog(#"Opened progress report");
UIViewController *detailViewControl = [[UIViewController alloc] init];
// Set progress report as the view controller
[self.navigationController pushViewController:detailViewControl animated:YES];
UIImage *background = [UIImage imageNamed:#"bg"];
// Add static image bg
self.backgroundImageView = [[UIImageView alloc] initWithImage:background];
self.backgroundImageView.contentMode = UIViewContentModeScaleAspectFill;
[self.view addSubview:self.backgroundImageView];
// Add blurred layer to image when tableView goes in front of it
self.blurredImageView = [[UIImageView alloc] init];
self.blurredImageView.contentMode = UIViewContentModeScaleAspectFill;
self.blurredImageView.alpha = 0;
[self.blurredImageView setImageToBlur:background blurRadius:10 completionBlock:nil];
[self.view addSubview:self.blurredImageView];
[self.navigationController setNavigationBarHidden:NO];
So I don't understand why that when I do this, a selector from the button (that I know fires, because I get Righthtere in my log):
-(void)home:(UIBarButtonItem *)sender {
NSLog(#"Righthtere");
// Set progress report as the view controller
[self.navigationController popToViewController:self animated:YES];
}
It doesn't go back to the initial view controller.
You seem to be confusing popToViewController and popViewControllerAnimated. popViewControllerAnimated removes the current view from the stack and brings the new stack top the active view controller. popToViewController pops the stack until the listed view controller is on top of the stack.
Since you are calling popToViewController with self, it will look and see that the requested view controller is already on top of the stack and do nothing. If you wish to go back one view controller then your call should be.
[self.navigationController popViewControllerAnimated:YES];
I use the below code to pop the previous viewcontroller in iOS 8.
[self presentModalViewController:viewcontroller animated:YES];

UINavigationController setViewControllers autorotation issue

I present a UINavigationController with two view controllers in stack and present the last view controller first. And tapping the back button obviously goes back to the first view controller.
navCtrl = [[UINavigationController alloc] init];
ViewController1 *vc1 = [[ViewController1 alloc] init];
ViewController2* vc2 = [[ViewController2 alloc] init];
[navCtrl setViewControllers:[NSArray arrayWithObjects:vc1, vc2, nil] animated:NO];
[self presentViewController:navCtrl animated:YES completion:^{
}];
The problem is that when I push the back button on the navigation controller in landscape mode, view controller vc1 frame is incorrect. The frame is (0,0,320,568) and is laid out in landscape mode. I am running iOS 7. The autorotation code is not invoked on pressing the back button.
Whereas, If I present the navigation controller with the natural order vc1,vc2, I don't see any issue.
EDIT: FYI, here are the -viewWillAppear and -viewDidAppear calls in vc1 :
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
self.navigationController.navigationBar.barStyle = UIBarStyleBlackTranslucent;
[[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleBlackTranslucent];
[[UIApplication sharedApplication] setStatusBarHidden:NO
withAnimation:UIStatusBarAnimationNone];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(keyboardWillShow:)
name:UIKeyboardWillShowNotification
object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(keyboardWillHide:)
name:UIKeyboardWillHideNotification
object:nil];
NSLog(#"Frame = %#", NSStringFromCGRect(self.view.frame));
}
-(void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
// Get status bar height if visible
if (![UIApplication sharedApplication].statusBarHidden) {
CGRect statusBarFrame = [[UIApplication sharedApplication] statusBarFrame];
CGFloat statusBarHeight = MIN(statusBarFrame.size.height, statusBarFrame.size.width);
// Set navigation bar frame
CGRect navBarFrame = self.navigationController.navigationBar.frame;
[self.navigationController setNavigationBarHidden:YES animated:NO];
[self.navigationController setNavigationBarHidden:NO animated:NO];
/*
navBarFrame.origin.y = statusBarHeight;
self.navigationController.navigationBar.frame = navBarFrame;
*/
UIEdgeInsets e = UIEdgeInsetsMake(statusBarHeight + navBarFrame.size.height, 0, navBarFrame.size.height + 12, 0);
[_tableView setScrollIndicatorInsets:e];
[_tableView setContentInset:e];
}
_tableView.rowHeight = 75;
self.spinner = [[[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhite] autorelease];
spinner.frame = CGRectMake(145, 200, 30, 30);
[self.view addSubview:spinner];
[spinner startAnimating];
[self performSelector:#selector(refreshData) withObject:nil afterDelay:0.f];
}
I found the problem. For some old iOS bug workaround which I don't remember I had the following line in viewDidLoad
[self.navigationController.view setFrame: [self.navigationController.view bounds]];
There are a few things you need to check here:
You said you already checked, but lets be safe - check the Auto-Resizing on the view.
Do your view controllers implement the following method?
- (NSUInteger)supportedInterfaceOrientations;
Are you presenting the navigation controller as a child view controller? When doing that make sure the following method is returning its default YES
- (BOOL)shouldAutomaticallyForwardRotationMethods;
Check you are not adding child view controllers to the view of a UINavigationController as it doesn't forward the appearance methods, its best to handle that from within the view controllers on the navigation controller.
Failing all of the above you should put a breakpoint in viewWillAppear of vc1 and check its frame, if the frame is correct, but the navigationContoller.view frame is incorrect then you must check your auto-resizing masks.

Login Screen and Navigation controller

So in my App.Delegate I'm doing this -
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
self.loginViewController = [[LoginViewController alloc] initWithNibName:#"LoginView" bundle:[NSBundle mainBundle]];
UINavigationController *navigation = [[UINavigationController alloc] initWithRootViewController: self.loginViewController];
self.window.rootViewController = navigation;
self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];
return YES;
}
and in my login controller I'm doing this -
- (IBAction)login:(UIButton *)sender {
NSString *username = self.userName.text;
NSString *password = self.password.text;
[AccountUtils emailLogin:username password:password useCookie:true callback:^(NSDictionary *loginResponseJSON){
if([loginResponseJSON count] != 0){
[self performSelectorOnMainThread:#selector(displaySearchController) withObject:nil waitUntilDone:YES];
// [self performSelectorOnMainThread:#selector(switchState) withObject:nil waitUntilDone:YES];
} else {
//incorrect entry info view here.
}
}];
}
- (void) displaySearchController {
SearchViewController *searchViewController = [[SearchViewController alloc] initWithNibName:#"SearchView" bundle:[NSBundle mainBundle]];
UINavigationController *navigator = self.navigationController;
[navigator popViewControllerAnimated: YES];
[navigator pushViewController: searchViewController animated:YES];
}
If I correctly login, I go to the second controller's view, but at the top I'm still allowed to go 'back' to the login page. I don't want that to happen and I thought this case would be taken care off by the popViewControllerAnimated line. How do I make it so that when I login, I am not allowed to go back to the login page?(in other words, I guess pop the login controller off the navigation controller's stack?)
If you want just to remove the loginVC you could set the new navigationController as the rootViewController of the AppDelegate after the user has logged in. So you could move the displaySearchController method in the AppDelegate and call this method (from the loginVC) after the user has logged in:
-(void)displaySearchController{
SearchViewController *searchViewController = [[SearchViewController alloc] initWithNibName:#"SearchView" bundle:[NSBundle mainBundle]];
UINavigationController *navigation = [[UINavigationController alloc] initWithRootViewController: searchViewController];
self.window.rootViewController=navigation;
}
Otherwise, if you really want the push animation, you can remove the loginVC from the navigationController viewControllers stack in the viewDidAppear of SearchDispalyController:
-(void)viewDidAppear:(BOOL)animated{
NSMutableArray *stackVCs=[self.navigationController.viewControllers mutableCopy];
int idx=[stackVCs indexOfObject:self];
//this remove the previous viewcontroller from the stack
[stackVCs removeObjectAtIndex:idx-1];
self.navigationController.viewControllers=stackVCs;
[super viewDidAppear:animated];
}
Also in the loginVC just before push the searchVC you should call this to hide the back button:
[navigation.navigationItem setHidesBackButton:YES];
You don't want to use a UINavigationController with the LoginViewController and you don't want to push the SearchViewController. Instead, use a UINavigationController with SearchViewController and when you display it, make it the rootViewController.
Do this to achieve what you want.
Move the displaySearchController method to your AppDelegate.m file
Do declare the displaySearchController method in AppDelegate.h file
Now Define the displaySearchController method in AppDelegate.m file as :
- (void) displaySearchController {
SearchViewController *searchViewController = [[SearchViewController alloc] initWithNibName:#"SearchView" bundle:[NSBundle mainBundle]];
UINavigationController *navigator = [[UINavigationController alloc]initWithRootViewController:searchViewController];
self.window.rootViewController = navigator;
}
Call a new local method showNewViewController from your loginController as :
[self performSelectorOnMainThread:#selector(showNewViewController) withObject:nil waitUntilDone:YES];
Now define showNewViewController in your loginController.m file as
-(void)showNewViewController {
AppDelegate *appDele = [UIApplication sharedApplication].delegate;
[appDele displaySearchController];
}
Don't forget to import the AppDelegate.h file to your loginController.m
This will certainly help you.

App crashes on iPhone 5 when calling the Viewcontroller in delegate

I have a project that works well in other simulators including The New iPad. However, with the iPhone5, it crashes when calling the Viewcontroller in delegate
I don't know why this error happens. Please let me know if you discover any possible causes in the code below:
self.rootviewController = [[RootViewController alloc] initWithNibName:#"RootViewController" bundle:nil];
self.rootNavController = [[UINavigationController alloc]
self.rootNavController.navigationBar.hidden=YES;
[window addSubview:rootNavController.view];'
Please see image below:
Thank you very much
I think you do something wrong with the creation of your UINavigationController, try the following code to replace yours in your AppDelegate.m :
EDIT add code to display and remove splashscreen with UIViewController
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Create View Controller
RootViewController *rootViewController = [[RootViewController alloc] initWithNibName:#"RootViewController" bundle:nil];
// Create Navigation Controller
UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:rootViewController];
// Create Navigation Controller
self.window.rootViewController = navigationController;
[self.window makeKeyAndVisible];
// SplashScreen
[self displaySplashscreen];
return YES;
}
#pragma mark - SplashScreen Methods
- (void)displaySplashscreen
{
// Create View
self.splashscreenViewController = [[SplashscreenViewController alloc] init];
// Display Splashscreen
[_window addSubview:_splashscreenViewController.view];
// Dismiss Splashscreen
[self performSelector:#selector(dismissSplashscreen) withObject:nil afterDelay:3.0f]; // Modify the time
}
- (void)dismissSplashscreen
{
// Splashscreen Animation
[UIView animateWithDuration:0.5f
animations:^{
_splashscreenViewController.view.alpha = 0.0f;
} completion:^(BOOL finished) {
[_splashscreenViewController.view removeFromSuperview];
}];
}

Orientation Problem while using insertSubview

I get an orientation problem while using the following to code to display a view on top of a split view.
[window addSubview:aSplitViewController.view];
[window insertSubview:aViewController.view aboveSubview:aSplitViewController.view];
the plain view has a couple of buttons and labels.
So the problem I am facing is that the first view opens in landscape mode but the labels and buttons on the view are in portrait mode.
UPDATE: Here is some code so if anyone wants to see more details...
In my App Delegate
- (void) makeSplitViewController {
NSMutableArray *controllers = [NSMutableArray arrayWithArray:tabBarController.viewControllers];
// First tabbbar item
// detail view
detailViewController = [[DetailViewController alloc] initWithNibName:#"DetailView" bundle:nil];
UINavigationController *navDetailView = [[[UINavigationController alloc] initWithRootViewController:detailViewController] autorelease];
navDetailView.hidesBottomBarWhenPushed = YES;
// root view
rootViewController = [[RootViewController alloc] initWithStyle:UITableViewStylePlain];
rootViewController.detailViewController = detailViewController;
rootViewController.navigationItem.title = #"List";
UINavigationController *navRootView = [[[UINavigationController alloc] initWithRootViewController:rootViewController] autorelease];
navRootView.hidesBottomBarWhenPushed = YES;
navRootView.navigationBar.barStyle = UIBarStyleBlackTranslucent;
splitViewController = [[UISplitViewController alloc] init];
splitViewController.tabBarItem.title = #"Face Sheet";
splitViewController.tabBarItem.image = [UIImage imageNamed:#"gear1.png"];
splitViewController.navigationItem.title = #"Face Sheet";
splitViewController.viewControllers = [NSArray arrayWithObjects:navRootView, navDetailView, nil];
splitViewController.delegate = detailViewController;
splitViewController.hidesBottomBarWhenPushed = YES;
[controllers addObject:splitViewController];
// Second tabbbar item
scoreViewController = [[ScoreCardViewController alloc] initWithNibName:#"TableViewController" bundle:nil];
scoreViewController.tabBarItem.title = #"Score Card";
scoreViewController.tabBarItem.image = [UIImage imageNamed:#"gear1.png"];
scoreViewController.navigationItem.title = #"Score Card";
[controllers addObject:scoreViewController];
tabBarController.viewControllers = controllers;
}
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
// Create tabbar
tabBarController = [[UITabBarController alloc] init];
//tabBarController.delegate = self;
// Set window
[window addSubview:splashController.view];
[window insertSubview:tabBarController.view belowSubview:splashController.view];
[self.window makeKeyAndVisible];
application.statusBarOrientation = UIInterfaceOrientationLandscapeRight;
return YES;
}
and here is the code in my SplashScreenView
- (IBAction) proceedButtonClick:(id)sender
{
// Initialize loginpopview
PhysicianLoginViewController *loginViewController = [[PhysicianLoginViewController alloc] init];
popOverController = [[UIPopoverController alloc] initWithContentViewController:loginViewController];
popOverController.popoverContentSize = CGSizeMake(350, 200);
popOverController.delegate = self;
// Set a notification to dismiss it later
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(loginViewControllerDone:) name:#"loginViewControllerDone" object:popOverController.contentViewController];
// Present popover
if ([popOverController isPopoverVisible])
{
[popOverController dismissPopoverAnimated:YES];
}
else
{
[popOverController presentPopoverFromRect:CGRectMake(485, 600, 100, 100) inView:self.view permittedArrowDirections:UIPopoverArrowDirectionDown animated:YES];
}
}
// Dismiss popview controller and setup the tabbar
- (void)loginViewControllerDone:(NSNotification *)notification{
[[NSNotificationCenter defaultCenter] removeObserver:self];
// Button in content view controller was tapped, dismiss popover...
[self.popOverController dismissPopoverAnimated:YES];
// remove subview
[self.view removeFromSuperview];
// set tabbar
i3EAppDelegate *appDelegate = (i3EAppDelegate *) [[UIApplication sharedApplication]delegate];
[appDelegate makeSplitViewController];
}
It would be great if someone could point out where I am going wrong. I have been stuck with this problem for quite a few days and I have tried everything that comes to my mind...
UIWindow has a subview that it uses for rotations and puts other views inside of that. You need to insert yourself into the root view (or something lower), not the window. Look at -[UIWindow rootViewController].
UIView *rootView = [[[self window] rootViewController] view];
[rootView addSubview:view];
This will work as long as you're using something with a root view controller. This will work as long as rootViewController isn't nil. If you're doing a raw "View Based" application, then it's usually best to pick another view and add your view as its sibling rather than digging through the undocumented hierarchy:
UIView *sibling = ... (some other view)
[[sibling superview] addSubview:view];

Resources