I'm trying to load my view then transition to it using a custom segue. The problem is that it's lagging quite a bit, and I can't seem to find the source other than the view being presented is loading during the segue.
- (IBAction)continueButtonClicked:(id)sender{
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"MainStoryboard_iPhone" bundle:nil];
IntroViewController *imageLoader = (IntroViewController*)[storyboard instantiateViewControllerWithIdentifier:#"imageLoader"];
dispatch_sync(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0), ^{
// present
[self customSegue:imageLoader];
// dismiss
[self dismissViewControllerAnimated:YES completion:nil];
});
}
I've gone to the extent of incorporating grand central dispatch, but I see no difference. I have little to no experience with GCD either. I have the same segue going to another view and it loads without lag just fine.
You can only present UI on the main thread and you are trying to do on a background thread. Try this:
- (IBAction)continueButtonClicked:(id)sender
{
dispatch_async(dispatch_get_main_queue(), ^{
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"MainStoryboard_iPhone" bundle:nil];
IntroViewController *imageLoader = (IntroViewController*)[storyboard instantiateViewControllerWithIdentifier:#"imageLoader"];
// present
[self customSegue:imageLoader];
});
}
I've removed the [self dismissViewControllerAnimated:YES completion:nil]; line because dismissing the presenting view controller will result in the presented view controller not being able to unwind the segue.
Related
My app was working until I removed some code that made a network request (so this caused a few seconds of waiting). I guess this was enough time for the view to load onto the window hierarchy... But now that I removed this network request and it goes straight to try to open that view, I get this message:
Warning: Attempt to present on whose view is not in the window hierarchy!
How can I wait until the view is present before I try to open it?
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"Main" bundle: nil];
StoreFlyersViewController * vc = [storyboard instantiateViewControllerWithIdentifier:#"TabBar"];
[self presentViewController:vc animated:NO completion:nil];
Correct me if I'm wrong, but it seems you are pushing/presenting a view controller (call it A) and another one (the StoreFlyersViewController) immediately, e.g. from the viewDidLoad method of A - when the view of A is not in the window hierarchy yet.
To solve this, you could move your code to viewDidAppear:. Or, if this does not produce the results you want, you could try to move the code to a separate method, and call that method via performSelector:withObject:afterDelay: with a short delay (e.g. 0.1 second).
try this;
dispatch_async(dispatch_get_main_queue(), ^{
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"Main" bundle: nil];
StoreFlyersViewController * vc = [storyboard instantiateViewControllerWithIdentifier:#"TabBar"];
[self presentViewController:vc animated:NO completion:nil];
});
I have two View Controllers. ViewController & Next.
ViewContoller.m
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
bool static check = FALSE;
if(!check){
check = true;
[self changeView];
}
}
-(void)changeView{
dispatch_async(dispatch_get_main_queue(), ^{
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"Main" bundle:nil];
Next *viewController = (Next *)[storyboard instantiateViewControllerWithIdentifier:#"next"];
[self presentViewController:viewController animated:YES completion:nil];
});
}
Next.m is nothing more than the xcode boilerplate code of a view.
When i execute this as above, i receive this output:
2014-11-27 13:20:21.005 changeView[1804:72014] Unbalanced calls to begin/end appearance transitions for <ViewController: 0x7fdd80f41e20>.
You'll notice that i have a static boolean in the viewDidLoad. Without this, the viewDidLoad is called infinitely causing an animated loop of Next's view appearing ontop of ViewController's view.
I'm almost 100% certain that i should not have to implement the boolean to prevent the loop but this is the only way i can. What do?
Update!
When i embed a navigation controller, and use [self.navigationController presentViewController:viewController animated:NO completion:nil]; i do not experience the loop.
While this resolves the situation, the issue of changing views (without a navigation controller) still stands
I'm playing around with view life cycles & am having trouble changing a view from the load of a different view.
In my ViewController.h i have:
-(void)viewDidAppear:(BOOL)animated{
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"Main" bundle:nil];
ViewController2 *viewController = (ViewController2 *)[storyboard instantiateViewControllerWithIdentifier:#"ViewController2"];
[self presentViewController:viewController animated:YES completion:nil];
}
However this only causes the view to be between ViewController, and ViewController2 appearing with animation (in a loop).
I used the code in viewDidLoad however neither of the view's loaded (from reading you cannot change view until the viewWillAppear)
Update: When using the code in viewWillAppear, whose view is not in the window hierarchy error is thrown.
How does one change the view from view setup stage?
Update
Using the above code, inside & out of GCD, in viewDidLoad, viewWillAppear & viewDidAppear either results in an infinite loop of animated showing of the ViewController2, or crash on 2nd attempt of segue (result from the loop).
EDITED:
I'm not sure exactly what you're trying to do, but assuming you are wanting the first viewcontroller to appear and then the second viewcontroller to immediately animate on top of the first one, you should be able to accomplish using several options:
First you could just wrap your calls in a dispatch_async call:
dispatch_async(dispatch_get_main_queue(), ^{
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"Main" bundle:nil];
ViewController2 *viewController = (ViewController2 *)[storyboard
instantiateViewControllerWithIdentifier:#"ViewController2"];
[self presentViewController:viewController animated:YES completion:nil];
});
Or you could use a show modally segue:
- (void)viewDidLoad {
[super viewDidLoad];
dispatch_async(dispatch_get_main_queue(), ^{
[self performSegueWithIdentifier:#"myModalSegue" sender:self];
});
}
Or you could use a navigation controller and use a standard show segue (formally push). This one doesn't require the dispatch_async:
- (void)viewDidLoad {
[super viewDidLoad];
[self performSegueWithIdentifier:#"myshowsegue" sender:self];
}
I've posted working examples of all three on: github
It is better to exchange views in loadView method.
- (void)loadView {
CGRect rect = [[UIScreen mainScreen] applicationFrame];
MyView *view = [[MyView alloc] initWithFrame:rect];
[view setAutoresizingMask:UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth];
self.view = view;
}
Guys in my app I have some code in the app delegate method application:didFinishLaunchingWithOptions: that determines if the initial View Controller should be the LoginViewController or the MainViewController.
If the LoginViewController is showed first and the user logs in successfully I show the MainViewController modally with this piece of code:
UIStoryboard *storyBoard = [UIStoryboard storyboardWithName:#"Main" bundle:nil];
FSMainViewController *vc = (MainViewController *)[storyBoard instantiateViewControllerWithIdentifier:#"MainViewController"];
vc.loginViewController = self;
[self presentViewController:vc animated:YES completion:nil];
What I want to do next, after the MainController is showed on the screen, is remove the LoginViewController from memory so in the viewWillApper:animated: method of the MainViewController I use this code to remove (or at least try to) the LoginViewController:
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
if (self.loginViewController) {
[self.loginViewController dismissViewControllerAnimated:NO completion:nil];
}
}
Problem is that this code leads to strange behaviors like the MainViewController being removed from the screen and this error message showing up in the console.
Unbalanced calls to begin/end appearance transitions for <LoginViewController: 0xb06e350>
I also tried calling [self dismissViewControllerAnimated:NO completion:nil] in the completion block of the presentViewController:animated:completion method but still no luck, it didn't work.
What am I doing wrong? How can I remove from memory the underlying LoginViewController when the MainViewController is presented modally?
Don't present your main view controller if you want the login controller to go away, just make it the window's root view controller.
UIStoryboard *storyBoard = [UIStoryboard storyboardWithName:#"Main" bundle:nil];
FSMainViewController *vc = (MainViewController *)[storyBoard instantiateViewControllerWithIdentifier:#"MainViewController"];
Self.window.rootViewController = VC;
You can't dismissViewController after presenting another one on it or its presentingViewController. At here, you should dismiss LoginViewController first, then present MainViewController.
Otherwise, if you'd like pushViewController, you can call [self.navigationController setViewControllers: animated:] to remove LoginViewController.
If you think presentingViewController is just what you want, try something like this in application:didFinishLaunchingWithOptions:
if (self.loginViewController) { //Define loginViewController in appDelegate.h
[self dismissViewControllerAnimated:NO completion:^{
[self presentViewController:mainViewController animated:YES completion:nil];
}];
}
else{
[self presentViewController:mainViewController animated:YES completion:nil];
}
I'm looking for a way to present a modal view over my current UIViewController to basically show a UIActivityIndicator and force users to wait while data is being loaded.
in BaseViewController.m (base class of all my UIViewControllers):
// show loading view
-(void) showLoading
{
UIStoryboard *storyBoard = [UIStoryboard storyboardWithName:#"Main_iPhone" bundle:nil];
LoadingViewController *loading = [storyBoard instantiateViewControllerWithIdentifier:#"loadingView"];
loading.view.backgroundColor = [UIColor colorWithRed:0.9 green:0.9 blue:0.9 alpha:0.7];
self.modalPresentationStyle = UIModalPresentationCurrentContext;
[self presentViewController:loading animated:NO completion:nil];
}
This works great, but how can I go back to the background view after the loading view should be done?
Need a stopLoading method to go back to the original view:
// stop loading
-(void) stopLoading
{
// code here
}
If I try to present a new view after I present the loading view like so:
UIStoryboard *storyBoard = [UIStoryboard storyboardWithName:#"Main_iPhone" bundle:nil];
UIViewController *view = [storyBoard instantiateViewControllerWithIdentifier:#"loadingView"];
[self presentViewController:view animated:YES completion:nil];
The debugger gives Warning:
Attempt to present PropertyPickerViewController: 0x8af6010 on ViewController: 0x8ab23c0 which is already presenting LoadingViewController: 0x8acf530.
Try:
[self dismissViewControllerAnimated:YES completion:nil];
In fact, I'm not sure that it'a great idea to present new controller with animated gif.
The best option is (imo) show UIActivityIndicator + place a view on top on all other views to prevent user from clicking anything.
You must [self dismissViewControllerAnimated:YES completion:nil] first.
Check the Apple Documentation.