I have 2 view parentView and SubView. I am called the subView from my parentView:
subView = [self.storyboard instantiateViewControllerWithIdentifier:#"subView"];
[self presentViewController:subView animated:YES completion:nil];
[self.view addSubview:subView.view];
In my subView I have some functionalities and storing the values in a string. I want to display these values in a label in my parentView after I dismiss the subView:
[self dismissViewControllerAnimated:YES completion:Nil];
How can I pass these values to my parentView and display it over there after dismissViewControllerAnimated?
You can have properties in your SubView and before calling [self dismissViewControllerAnimated:YES completion:Nil]; take values from SubView and show in parentView
You can set your parent view as delegate of subView and transfer values to parent view.
By the way. Why are you calling addSubview: after presentViewController: ?
EDIT
Check this answer
Hiding buttons in a UIViewController from a UIView delegate
Related
I am writing a UIControl. I need to display a popover when the user touches an area of the control. But of course the usual code:
[self presentViewController:self.popover animated:YES completion:nil];
does not work because we are in a UIControl, not in a UIViewController.
How can I display a popover from a UIControl?
You should use rootViewController to present it.
Use UIApplication.sharedApplication.delegate.window.rootViewController instead of self
[UIApplication.sharedApplication.delegate.window.rootViewController presentViewController:self.popover animated:YES completion:nil];
I have 2 UIViewController's presented with [self presentViewController:viewController animated:YES completion:nil];, I want to dismiss the first one of them, without animation (It's not visible to the user anyway) and when the second one (currently visible) will be dismissed, the user will see the parent view controller who present them both.
- Parent
- First -> Dismiss first without animation
- Second -> Dismiss second with animation
How can I do that?
With your current view controller hierarchy if first view controller will be dismissed it will dismiss second view controller automatically. If you don't want that behaviour than make parent present second view controller. You can do that from first view controller by using [self.presentingViewController presentViewController:secondViewController animated:YES completion:nil]
Why do you want to do this?
You should do it like this for cleaner view hierarchy and better user experience:
Present first view controller :
[self presentViewController:viewController1 animated:YES completion:nil];
Dismiss first & present second view controller :
__weak MyViewController *aBlockSelf = self;
[self dismissViewControllerAnimated:YES completion:^{
[aBlockSelf presentViewController:viewController2 animated:YES completion:nil];
}];
I have a UIViewController subclass that contains a UICollectionView. Selecting a cell presents a new view controller. When I return to the first view controller, the contentOffset of the collection view is reset to CGPointZero.
From my research this seems to be standard behaviour.
I can reset the contentOffset by adding a private property to my view controller subclass, saving the contentOffset in the viewWillDissapear method and resetting it on the collection view in the viewWillAppear method.
I would like to know though, is there another way to prevent the scroll view content offset from being reset in the first place (removing the need for an extra property)?
I am targetting iOS7.
The 2nd view controller is presented like this:
[self presentViewController:secondVC animated:YES completion:nil];
And dismissed like this (in the 2nd view controller):
-(void) dismiss
{
[self dismissViewControllerAnimated:YES completion:nil];
}
Edit: After further investigation it appears resetting the contentOffset is not the default behaviour. I haven't figured out why it is happening in my application yet. I am presenting just as have showed in the code above.
There are 2 options. If your application works with UINavigationController, it should keep the last accessed cell, not sure about how you have worked with. If not, you can always create a notification and in the moment you are getting back to the previous screen, send the update of the UICollectionView or whatever you have and scroll to the correct position.
Bear in mind, the UIViewController life cycle and how it´s going to appear before to update the user interface.
You can create a CGPoint object in AppDelegate. Then you can set that value to your CollectionView objects contentOffset while present and dismiss like that;
[self presentViewController:secondVC animated:YES completion:{
appDelegate.tempContentValue = _collectionView.contentOffset;
}];
[self dismissViewControllerAnimated:YES completion:{
UIViewController *first = [UIViewController alloc] init];
[first.collectionView setContentOffset:appDelegate.tempContentValue animated:YES];
}];
#property (nonatomic,assign) CGPoint newContentOffset;
Then when you navigate to another View / present another view
self.newContentOffset = self.collectionView.contentOffset;
In viewWillAppear
[self.collectionView reloadData];
if (self.newContentOffset) {
[self performSelector:#selector(setContentOffset) withObject:nil afterDelay:0.1];
}
-(void)setContentOffset {
[self.collectionView setContentOffset:self.newContentOffset];
}
There will be a small jerk while applying this . But if thats ok, you can go ahead with this method
When I try to present a UIImageViewController from the table view, the starting animation is a vertical cover, which I want, but when the controller gets dismissed, the animation is a Horizontal Flip. I tried [self presentViewController:self.imageController animated:YES completion:nil]; and [self.navigationController presentViewController:self.imageController animated:YES completion:nil]; but the dismiss animation is the same in both cases. When I remove the middle view controller (i.e just navigation controller with the tableview as its root), the dismiss animation is correct (cover vertical).
Anyone know how I can get the correct dismiss animation with the current storyboard configuration?
You can try this method:
[self.navigationController pushViewController:self.imageController animated:YES];
I have a uiview that has a subview. That subview has a button that loads a modalViewController. Then When I use [self dismissModalViewControllerAnimated:YES]; I need it to go back to the uiview with the subview, not just the subview that presents the modal view.
How can I do this?
Generally, you can remove a subview with:
[subView removeFromSuperview];
You can just call that right before your dismissModalViewControllerAnimated or use the completion:(Block) parameter if you want it to only go away after the brief animation:
[self dismissViewControllerAnimated:YES completion:^{
[subView removeFromSuperview];
}];