I took 2 UIViewController HomeView and ViewMyStuff in Storyboard.
I am adding ViewMyStuff Controller in HomeView. In HomeView's ViewDidLoad, written below in my code.
- (void)viewDidLoad
{
ViewMyStuff *vwMyStuff = [self.storyboard instantiateViewControllerWithIdentifier:#"viewreportvc"];
[self addChildViewController: vwMyStuff];
[vwMyStuff didMoveToParentViewController:self];
}
vwMyStuff controller is not added in HomeViewController. Is there anything I missing? Please help
Thanks.
You need to add subView of your ViewMyStuff *vwMyStuff viewController.
- (void)viewDidLoad
{
ViewMyStuff *vwMyStuff = [self.storyboard instantiateViewControllerWithIdentifier:#"viewreportvc"];
[self addChildViewController: vwMyStuff];
[vwMyStuff didMoveToParentViewController: self];
[self.view addSubview:vwMyStuff.view];
}
Hope it works for you!!
Related
I have main dashboard (UITableViewController) and once i sign in i need to show this page with a welcome message which am showing using a UIViewController.
How can i show this popup from my ViewDidAppear() method ?
Am using following code and its not working
-(void)viewDidAppear:(BOOL)animated{
popupObj= [self.storyboard instantiateViewControllerWithIdentifier:#"popup"];
[popupObj setModalPresentationStyle:UIModalPresentationCurrentContext];
}
please help me..
i saw couple of stackoverflow links
Update
When i change my code to this one
-(void)viewDidAppear:(BOOL)animated{
popupObj= [self.storyboard instantiateViewControllerWithIdentifier:#"popup"];
// [popupObj setModalPresentationStyle:UIModalPresentationCurrentContext];
popupObj.modalPresentationStyle = UIModalPresentationOverCurrentContext;
popupObj.modalTransitionStyle = UIModalPresentationPopover;
[self presentViewController:popupObj animated:YES completion:nil];
}
Now i can see my UIViewController coming as popup but now UIViewController
coming as full screen view.
But i need only this frame (320 , 320)
popupObj= [self.storyboard instantiateViewControllerWithIdentifier:#"popup"];
popupObj.view.frame = CGRectMake(20, 200, 280, 168);
[self.view addSubview:popupObj.view];
[self addChildViewController:popupObj];
You can add UIViewController as subview and set it's frame so it will look like popup.
Hope this will help you.
I have two way,maybe not good, but work at most of time.
First,when the second view controller appear, show a screen shot of the first view controller. like this:
- (void)setBackGround {
UIGraphicsBeginImageContextWithOptions(self.view.frame.size, NO, [UIScreen mainScreen].scale);
[self.presentingViewController.view drawViewHierarchyInRect:self.view.bounds afterScreenUpdates:NO];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
self.view.layer.contents = (__bridge id)(image.CGImage);
}
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
if (!_isShown) {
_isShown = YES;
[self setBackGround];
}
}
Do't forget set "_isShown = NO" when init.
Second: you can only init a view, and show it on a view controller with animation. code at :http://blog.moemiku.com/?p=101
I have update a example on the blog. Direct download url
Your code creates the view controller and sets its presentation style, but doesn't actually present it. For that to happen you need this line after the two you have right now:
[self presentViewController:popupObj animated:true completion:nil];
Please set navigation controller on your rootviewcontroller :
-(void)viewDidAppear:(BOOL)animated
{
popupObj= [self.storyboard instantiateViewControllerWithIdentifier:#"popup"];
[self.navigationController presentViewController:popupObj animated:YES completion:nil];
}
Or
-(void)viewDidAppear:(BOOL)animated
{
popupObj= [self.storyboard instantiateViewControllerWithIdentifier:#"popup"];
[self.view addSubview:popupObj.view];
}
I have a method that takes a viewController and an identifier and returns a viewController. It looks like this:
- (UIViewController *)createStepVC:(UIViewController *)viewController identifier:(NSString *)identifier
{
viewController = [self.storyboard instantiateViewControllerWithIdentifier:identifier];
[_scrollView addSubview:viewController.view];
[self addChildViewController:viewController];
[viewController didMoveToParentViewController:self];
viewController.view.translatesAutoresizingMaskIntoConstraints = NO;
return viewController;
}
In viewDidLoad I call it like this:
_step1VC = (TNSettingsViewController *)[self createStepVC:_step1VC identifier:#"TNSettingsViewController"];
_step1VC which is of type TNSettingsViewController inherits from UIViewController.
When I try and run it - this happens:
The scrollView is an IBOutlet which is connected. All the storyboard ID's are correct. I have no idea as to why this is happening.
Any help would be greatly appreciated!
According to the error message, most likely somewhere in your Storyboard you have a view that points to backgroundImage outlet which is no longer presented in TNSettingsViewController.
I have a UIView named scoreView on ViewController. When I load the ViewController I hide the scoreView in the viewDidLoad method. It works fine.
- (void)viewDidLoad{
[super viewDidLoad];
self.navigationController.navigationBarHidden = YES;
scoreView.hidden = YES;
}
Then after being pushed from another view controller to this ViewController, I call a method, where I want to show the scoreView. But the scorView is still being hidden. Where is the mistake I am doing?
-(void)levelCompleteViewAppear: (NSString *)score{
NSLog(#"This method is called!");
scoreView.hidden = NO ;
[gameScore setText:score];
}
This is how I am pushing to the ViewController from anothe view controller.
-(void)levelComplete{
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"Main" bundle: nil];
ViewController *vc = [storyboard instantiateViewControllerWithIdentifier:#"ViewController"];
[self.navigationController pushViewController:vc animated:YES];
[vc levelCompleteViewAppear:scoreLabel.text];
}
Try This,
-(void)viewDidLoad
{
[super viewDidLoad];
[self.view addSubview:scrollview];
[self.view bringSubviewToFront:scrollview];
}
When you are pushing the viewController to navigationController, it will not call viewDidLoad directly. If you want to make it visible, write it down in viewWillAppear or vieDidAppear method.
- (void) viewWillAppear{
scoreView.hidden = YES;
}
add this in your code
If first time ViewController class in opeining then ViewDidLoad will call but if you are coming back from some other ViewController then ViewDidLoad wont get call for that you need to show your View in ViewWillAppear Or ViewDidAppear.
Suppose you called Class A then ViewDidLoad will get call then again you moved class B then class B viewDidLoad will get call but if you are coming back again to class A then your viewDidLoad wont get call, it will call ViewWillAppear and viewDidAppear.
Hope it may clear you more.
-(void)viewWillApper:(BOOL)animated{
scoreView.hidden = NO ;
}
On View1 I hide the navigationBar in viewDidLoad:
- (void)viewDidLoad
{
[super viewDidLoad];
[self.navigationController setNavigationBarHidden:YES];
}
Then I navigate to View2 where I show the navigationBar
- (void)viewDidLoad
{
[super viewDidLoad];
[self.navigationController setNavigationBarHidden:NO];
self.title = #"Title";
}
But on back to View1 again, the navigationBar doesn't hide, even if I did tried to hide it after the pushViewController in View2
[self.navigationController pushViewController:View1 animated:YES];
[self.navigationController setNavigationBarHidden:YES];
I also tried to hide the navigation from viewWillAppear in View1 and it hides it, but there is an ugly delay and I don't find it as a good practice.
So can anyone help me with this issue, how can I hide correctly the navigationBar on back to View1?
The best practice to do what you want is putting bellow in your first viewController:
- (void)viewWillAppear:(BOOL)animated{
[self.navigationController setNavigationBarHidden:YES animated:animated];
[super viewWillAppear:animated];
}
- (void)viewWillDisappear:(BOOL)animated{
[self.navigationController setNavigationBarHidden:NO animated:animated];
[super viewWillDisappear:animated];
}
-(void) viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
[self.navigationController setNavigationBarHidden:YES];
}
The ViewController1 is not going to get allocated again and so viewDidLoad is not going to get called.
You can do it in viewWillAppear though. But if you are saying that there is a delay, you can do one more thing.
You can get the reference ofViewController1 in ViewController2. Suppose ViewController1 is the first controller in the navigation controller, then do this:
//ViewController2.m
- (IBAction)backButtonPressed:(id)sender{
ViewController1 *view1 = [self.navigationController.viewControllers objectAtIndex:0];
[view1.navigationController setNavigationBarHidden:YES];
Your code is correct, but you need to write like this:
[self.navigationController setNavigationBarHidden:YES];
first, then write
[self.navigationController pushViewController:View1 animated:YES];
See when you are pushing View2 from View2 in navigation stack than View1 doesn't gets deallocated. it is there in in the stack. So when you popping out View2 that time View1 viewDidLoad won't get called. so your code setNavigationBarHidden to hide navigation bar doesn't executes. So put that code to ViewWillAppear or ViewDidAppear because these methods gets called every time View appears.
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
[self.navigationController setNavigationBarHidden:YES];
}
I am doing an iPad app with UISplitViewController. I want to open a modalViewController in the masterViewController itself. When I load my view controller modally, it takes a whole screen to present it.
Here it is my code, which is in my masterViewController.m to present the new viewController modally
- (void)addNewContactButtonPressed:(id)sender {
AddOrEditContact *addContact = [self.storyboard instantiateViewControllerWithIdentifier:#"AddOrEditContact"];
addContact.screenMode = addMode;
UINavigationController *navigationController = [[UINavigationController alloc]initWithRootViewController:addContact];
[self.navigationController presentViewController:navigationController animated:YES completion:nil];
}
I want to load a new viewController modally inside the masterViewController. Any help would be appreciated.
You can't present a modal viewController over the masterViewController only, but you can add a childView controller to the masterViewController nd perform your own animation to present it
- (void)addiewControllerToHierarchy:(UIViewController *)viewController
{
[self addChildViewController:viewController];
[self.view addSubview:frontViewController.view];
if ([viewController respondsToSelector:#selector(didMoveToParentViewController:)])
{
[viewController didMoveToParentViewController:self];
}
}
and to remove
- (void)_removeViewControllerFromHierarchy:(UIViewController *)viewController
{
[viewController.view removeFromSuperview];
if ([viewController respondsToSelector:#selector(removeFromParentViewController)])
{
[viewController removeFromParentViewController];
}
}
this example doesn't have animation and probably you need to adjust the frame of the view etc... but I hope could help you