From my appDelegate I load the the homeScreen ViewController like this:
UIStoryboard* sb = [UIStoryboard storyboardWithName:#"Storyboard" bundle:nil];
UIViewController *controller = [sb instantiateViewControllerWithIdentifier:#"HomeScreen"];
[self.window setRootViewController:controller];
The app only ever then changes between my 'homeScreen' and 'PlayViewController' ViewControllers and that is done like this:
PlayViewControlller* vc = [self.storyboard instantiateViewControllerWithIdentifier:#"PlayViewController"];
[self presentModalViewController:vc animated:NO];
and this
UIStoryboard* sb = [UIStoryboard storyboardWithName:#"Storyboard" bundle:nil];
UIViewController *controller = [sb instantiateViewControllerWithIdentifier:#"HomeScreen"];
[self presentModalViewController:controller animated:NO];
respectively.
This all works fine. I am able to switch between the viewcontrollers with uibuttons and the above code. However, I'm not sure what causes this, it happens after I've switched between the two viewcontrollers a few times, but the transition starts to get animated and they start twirling when switching.
Ok so I'm trying to narrow down the problem and see what's causing it. I think its the other animation blocks that I'm using in my app. But there's a lot there so don't know exactly what it is.
OK so I found the line of code that was causing this if anyone happens to find themselves in the same situation.
[UIView beginAnimations:#"Move" context:NULL];
Related
Returning to iOS development after a year break, and its all mayhem.
When executing the 3 lines of code below, i see the screenshot of the emulator below.
I've also attempted to add the LoginForm view controller as the root view controller, as it should, except i also see a blank screen.
As shown in the screenshot of xcode, the naming all matches up. All the constraints are also blue.
I've included a NSLog in the viewDidLoad of the LoginForm, that i have seen log at points, but still nothing is displayed.
What am i missing, or doing wrong? I've got nothing from errors to work with.
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"Main" bundle:nil];
UIViewController *view = [storyboard instantiateViewControllerWithIdentifier:#"LoginForm"];
[self.navigationController pushViewController:view animated:YES ];
Try using
[self presentViewController:myVC animated:NO completion:nil];
Because your storyboard doesn't seem to be using navigation contoller.
If you Have Xib File
Viewcontroller1 *viewController = [[Viewcontroller1 alloc]
initWithNibName:#"Viewcontroller1" bundle:nil];
[[self navigationController] pushViewController:viewController animated:YES];
if you have Storyboard
Viewcontroller1*vc=[self.storyboard instantiateViewControllerWithIdentifier:#"Viewcontroller1"];
[self.navigationController pushViewController:vc animated:YES];
My application is almost completed, now i'm working on push notifications. I able to receive push notifications successfully, unfortunately i'm not able to perform segue in Appdelegate. Basically i have two base screens 1.login 2.Tabbar controller i'm handling this in app delegate as shown below.
UIStoryboard* appStoryboard = [UIStoryboard storyboardWithName:#"Main" bundle:nil];
if (![self connect])
{
UINavigationController *home=[appStoryboard instantiateViewControllerWithIdentifier:#"loginScreen"];
self.window.rootViewController=home;
}
else
{
//TabbarHome
UITabBarController *home=[appStoryboard instantiateViewControllerWithIdentifier:#"TabbarHome"];
//pushNotificationSeague
self.window.rootViewController=home;
}
when i receive a notification i tried to push using Storyboard ID like
UIStoryboard* storyboard = [UIStoryboard storyboardWithName:#"Main" bundle:nil];
NotificationViewController *controller = [storyboard instantiateViewControllerWithIdentifier:#"MyNotification"];
[[[[UIApplication sharedApplication]delegate]window]addSubview:controller.view];
[self.navigationController pushViewController:controller animated:YES];
Now navigation is done. But the Click event are making app to crash. Reason i found is the below line, without this line of code app will not navigate. If we i use the below code the allocation of memory issue is there i guess.
[[[[UIApplication sharedApplication]delegate]window]addSubview:controller.view];
Please help me with fixing issue. Why the actions in the NotificationViewController are making crash. crash report is
[NotificationViewController performSelector:withObject:withObject:]: message sent to deallocated instance 0x7ffdc60a8890
Do not add addSubview to window.
It is not making sense that you are pushing the viewController and also you are adding it to window subview. Just remove the following line
[[[[UIApplication sharedApplication]delegate]window]addSubview:controller.view];
Finally it should be
UIStoryboard* storyboard = [UIStoryboard storyboardWithName:#"Main" bundle:nil];
NotificationViewController *controller = [storyboard instantiateViewControllerWithIdentifier:#"MyNotification"];
[self.navigationController pushViewController:controller animated:YES];
After some time searching for an answer, I have to admit that i'm pretty confuse with this case.
At my job, I'm asked to do something really specific :
I have to present a UIViewController on a previous UIViewController, actually, the current ViewController dismiss itself before the second appear. That give the whole thing a funny animation that a ViewController goes down and another rise from the bottom after this. But... It's not really "pro" and, we're able to see the rootViewController behind the scene during the animation.
So, I have to precise that there is NO NavigationController, that would have made this a lot easier in my opinion, so I'm forced to do it with two UIViewController, and there is my actual code :
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"Storyboard" bundle:nil];
UIViewController *controller = [storyboard instantiateViewControllerWithIdentifier:#"userViewController"];
[[UIApplication sharedApplication].keyWindow.rootViewController presentViewController:controller animated:YES completion:^{
[self dismissViewControllerAnimated:YES completion:nil];
}];
It is call right after a button is pressed, so there is no problem with the actual ViewController viewDidLoad or viewDidAppear I think.
But each time this code runs, I get the following error :
[1163:17641] Warning: Attempt to present <UserViewController: 0x7b0f0a00> on <EIHomeViewController: 0x7b0d2a00> whose view is not in the window hierarchy!
I don't really know how I could manage to keep a trace of the current UIViewController to dismiss it in the next ViewController viewDidAppear to be sure there will be no "blackout" on the screen.
Thank you for your help in advance!
Try this code:
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"Storyboard" bundle:nil];
UIViewController *controller = [storyboard instantiateViewControllerWithIdentifier:#"userViewController"];
[self dismissViewControllerAnimated:NO completion:^{
[[UIApplication sharedApplication].keyWindow.rootViewController presentViewController:controller animated:NO completion:nil];
}];
I developed an application using only one storyboard and towards the end, when my app grew to have some 80 views, working in the storyboard was extremely difficult.
After some clicks the storyboard became non responsive/sluggish and I had to restart the xcode to get things back to normal.
So this time I decided to use multiple storyboards but I have the following problem. When I switch from one storyboard to the other, views on the storyboard I am popping from are not deallocated. As a result instruments show a continuous increase in memory allocations which is not acceptable.
Anybody knows how to dealloc the view in the second storyboard when I move back to the first ?
Bellow are the two ways I use for navigation
With one storyboard
-(IBAction)goToVcSameStoryboard:(id)sender{
[self performSegueWithIdentifier:#"segue" sender:self];
}
-(IBAction)backSameStoryboard:(id)sender{
[self.navigationController popViewControllerAnimated:YES];
}
With two storyboards
-(IBAction)goToVcDiffStoryboard:(id)sender{
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"Storyboard1" bundle:nil];
UIViewController *initialVC = [storyboard instantiateInitialViewController];
initialVC.modalPresentationStyle = UIModalPresentationCurrentContext;
[self presentViewController:initialVC animated:YES completion:nil];
}
-(IBAction)backDiffStoryboard:(id)sender{
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"Main_iPhone" bundle:nil];
UIViewController *initialVC = [storyboard instantiateInitialViewController];
initialVC.modalPresentationStyle = UIModalPresentationCurrentContext;
[self presentViewController:initialVC animated:YES completion:nil];
}
I am trying the "pass the baton" method for passing the managedObjectContext (MOC) thru multiple views. I have it successfully passed to the rootViewController. From there I move to a tabBarController via presentViewController. I can't seem to find a way to pass the MOC when the tabBarController is pushed.
AppDelegate.m
UIViewController *navigationController = (UIViewController *)self.window.rootViewController;
MyViewController *controller = (MyViewController *) navigationController;
controller.managedObjectContext = managedObjectStore.mainQueueManagedObjectContext;
The main view controller is basically a start up screen that will kick you into a login screen or if you are already logged in, to the tabBarController. Below is where I transition to the tabBarController from within the viewDidAppear method.
MyViewController.m
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"MainStoryboard_iPhone" bundle:nil];
UIViewController *mainTabVC = [storyboard instantiateViewControllerWithIdentifier:#"mainTabVC"];
[mainTabVC setModalPresentationStyle:UIModalPresentationFullScreen];
[self presentViewController:mainTabVC animated:NO completion:nil];
The tabBarController in the storyboard has the identifier "mainTabVC".
I've tried lines like
MyTabBarController.managedObjectContext = self.managedObjectContext;
but I get the error Property 'MOC' not found on object of type MyTabBarController even though I do have the property declared in MyTabBarController.h
Could someone show me a line of code that I can throw in this segue to push the MOC to the tab bar controller.
BTW- I'm utilizing RestKit in this app if that changes the way I should be handling this please let me know.
*****Solution********
To make things clear for any other new guys with the same question. I went from this:
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"MainStoryboard_iPhone" bundle:nil];
UIViewController *mainTabVC = [storyboard instantiateViewControllerWithIdentifier:#"mainTabVC"];
[mainTabVC setModalPresentationStyle:UIModalPresentationFullScreen];
[self presentViewController:mainTabVC animated:NO completion:nil];
To this:
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"MainStoryboard_iPhone" bundle:nil];
MyTabBarViewController *mainTabVC = [storyboard instantiateViewControllerWithIdentifier:#"mainTabVC"];
mainTabVC.managedObjectContext = self.managedObjectContext;
[mainTabVC setModalPresentationStyle:UIModalPresentationFullScreen];
[self presentViewController:mainTabVC animated:NO completion:nil];
Notice the assignment in the third line and using MyTabBarViewController instead of UIViewController in the second line. BIG thanks again to rdelmar!
Your code is somewhat confusing. Is MyTabBarController the class? It looks like mainTabVC is your instance. You should use that rather than the class, and you should change the type when you instantiate mainTabVC to MyTabBarController, instead of UITabBarController. You also don't need to get the storyboard the way you do, you can just use self.storyboard.
MyTabBarController *mainTabVC = [self.storyboard instantiateViewControllerWithIdentifier:#"mainTabVC"];
mainTabVC.managedObjectContext = self.managedObjectContext;
[mainTabVC setModalPresentationStyle:UIModalPresentationFullScreen];
[self presentViewController:mainTabVC animated:NO completion:nil];