I have a view controller that presents another view controller like so
override func viewDidLoad() {
super.viewDidLoad()
self.view.backgroundColor = .white
let qrScannerViewController = QRScannerViewController()
qrScannerViewController.presentedBy = self
self.present(qrScannerViewController, animated:true, completion: nil)
// Do any additional setup after loading the view.
}
qrScannerViewController (the presented view controller) then calls
self.dismiss(animated:true, completion: nil)
which to my understanding calls the presenting view controllers dismiss function anyway.
Problem is, once the presented view controller has been dismissed, the presenting view controller's viewDidLoad gets called again, meaning the view controller is presented again.
Any ideas how to get around this?
Even if I use delegation the presenting view controller's viewDidLoad gets called again
Thanks
The presenting view controller is defined in a UITabController:
let qrPlaceholderViewController = QRPlaceholderViewController()
let controllers = [restaurantNavController,favouritesViewController, qrPlaceholderViewController, profileViewController]
self.viewControllers = controllers
Ok so the problem here was ARC doing its job.
When the presenting view controller presented the other view controller, ARC was unloading the presenting controller. This meant that when the presented view controller was dismissed, the presenting one was reinstantiated, hence forcing the viewDidLoad method to get called again
Solution:
A few solutions are available:
First of all I just stored a flag in a helper that I could check in the viewDidLoad method to see if it had already been loaded before and if it had, dont present the view controller again
Alternatively, I changed to once a qr code had been scanned, call a function in the presented view controllers delegate (the presenting controller) that navigated to the view that I wanted, therefore skipping the issue of the viewDidLoad being executed again.
Related
I'm using this library to show a side menu inside my app: https://github.com/jonkykong/SideMenu
All is fine except for one thing. When I present a viewcontroller from the side menu, and then dismiss this view controller to the main view controller, the viewWillAppear and/or viewDidAppear of the main view controller is not called ever.
From SideMenuVC (side menu view controller) I present other view controller:
let myPortfoliosNC = storyboard!.instantiateViewController(withIdentifier: "myPortfoliosNC")
present(myPortfoliosNC, animated: true, completion: nil)
Next, form the myPortfolioNC i dismiss (from the first view controller) and when go to main view controller the viewDidAppear and viewWillAppear is not called. Any idea of what i'm doing wrong ??
I have a container view that is set as the input accessory view for my view controller. Whenever I present a modal view controller the input accessory view dismisses.
I tried using the code below which works when I present the modal view controller. However, that view controller presents an image picker controller and after presenting that image picker controller I get the error "Keyboard cannot present view controllers." Is there a way to keep the input accessory view open for any view controllers presented on top of the base view controller.
let rootViewController: UIViewController = (UIApplication.shared.windows.last?.rootViewController)!
rootViewController.present(addVideoController, animated: true, completion: nil)
I don't think there is a way to keep an inputAccessoryView active through the view hierarchy - either pushing the view onto a navigationController stack or displaying modally will not keep the root's inputAccessoryView visible from my experience. This is because inputAccessoryView is a member of the UIViewController class rather than the window itself - and the new UIViewController you are making active does not have an inputAccessoryView.
I believe you will need to add a inputAccessoryView into the UIViewController class that you are presenting modally. You can set it up the exact same way that you did with the root view controller's class.
// call this in your root view controller that has the inputAccessoryView currently displayed
let addViewController = YourViewControllerClass()
modalViewController.inputAccessoryView = self.inputAccessoryView
self.present(addViewController, animated: true, completion: nil))
I've got a kind of complicated modal segue setup in my project. I'm trying to dismiss a view controller another view controller previously presented. I'm doing so with this code:
if(self.presentedViewController != nil){
print(self.presentedViewController!)
self.presentedViewController!.dismiss(animated: false)
print(self.presentedViewController!)
}
The prints are there for debugging purposes. They show that the presentedViewController doesn't actually get closed.
Even though I've set animated to false, I still see an animation occuring in the app when dismiss is called. Yet, the VC doesn't actually get dismissed.
Anyone knows a solution?
Apple
The presenting view controller is responsible for dismissing the view
controller it presented. If you call this method on the presented view
controller itself, UIKit asks the presenting view controller to handle
the dismissal.
dismiss(animated:completion:) dismisses the view controller that was
presented modally by the view controller.
https://developer.apple.com/documentation/uikit/uiviewcontroller/1621505-dismiss
If you present a view controller from the app's root, for example:
Presenting view controller
let root = UIApplication.shared.keyWindow!.rootViewController!
root.present(someViewController, animated: true, completion: nil)
You would dismiss it from the presented view controller like so:
Presented view controller
let root = UIApplication.shared.keyWindow?.rootViewController
root?.dismiss(animated: true, completion: nil)
My target include a lot view need to present different view modally base on each user action. Here what I want to do to get cleaner view hierarchy and better user experience.
Root View Controller present First View Controller modally
When I clicked button on the First View Controller, then the Second View Controller appear modally over it.
As soon as the Second View Controller did appear, I want to dismiss or remove the first one from view hierarchy.
Can I do that? If so, how should i do it?
If not, what is the right way to solve this out cause I will present many modally presented view controllers over each view. I think even if I want to dismiss current view, the previous one will still remain appear when current one dismiss.
UPDATE :
VC1 (Root) > VC 2 (which was present modally) > VC 3 (which was
present modally over VC 2)
When i dismiss VC3, the VC2 is still on view memory. So, I don't want to appear VC2 as soon as I dismiss VC3 and instead I want to see VC1 by removing or dismissing VC2 from view hierarchy.
WANT : At the image, when I dismiss the blue,I don't want see the pink in my view memory and I want to remove it as soon as the blue one appear.
That's what i want to do.
Any Help?Thanks.
So, let's assume that you have a storyboard similar to:
What should happens is:
Presenting the the second ViewController (from the first ViewController).
Presenting the the third ViewController (from the second ViewController).
dismissing to the first ViewController (from the third ViewController).
In the third ViewController button's action:
#IBAction func tapped(_ sender: Any) {
presentingViewController?.presentingViewController?.dismiss(animated: true, completion: nil)
}
As you can see, by accessing the presentingViewController of the current ViewController, you can dismiss the previous hierarchy of the view controllers:
The view controller that presented this view controller.
By implementing presentingViewController?.presentingViewController? that means that: the presented of the presented current ViewController :)
It might seem a little bit confusing, but it is pretty simple.
So the output should be like (I added background colors to the viewControllers -as vc1: orange, vc2: black and vc3: light orange- to make it appears clearly):
EDIT:
If you are asking to remove the ViewController(s) in the middle (which in this example the second ViewController), dismiss(animated:completion:) does this automatically:
If you present several view controllers in succession, thus building a
stack of presented view controllers, calling this method on a view
controller lower in the stack dismisses its immediate child view
controller and all view controllers above that child on the stack.
When this happens, only the top-most view is dismissed in an animated
fashion; any intermediate view controllers are simply removed from the
stack. The top-most view is dismissed using its modal transition
style, which may differ from the styles used by other view controllers
lower in the stack.
Referring to what are you asking:
I think even if I want to dismiss current view, the previous one will
still remain appear when current one dismiss.
I think that appears clearly on the UI (and I find it ok), but as mentioned in the dismiss documentation discussion, both the third and the second will be removed from the stack. That's the right way.
Here is my opinion in different perspective,
Root View Controller present Second View Controller
Add FirstView onto Second View
Dismiss FirstView Controller when button pressed.
Second View Controller,
class ViewController: UIViewController, FirstViewControllerProtocol {
weak var firstViewController: FirstViewController?
override func viewDidLoad() {
super.viewDidLoad()
print("Not initiated: \(firstViewController)")
firstViewController = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "FirstViewController") as? FirstViewController
addChildViewController(firstVC!)
firstViewController?.delegate = self
view.addSubview((firstViewController?.view)!)
print("Initiated: \(firstViewController)")
}
func dismiss() {
firstViewController?.view.removeFromSuperview()
firstViewController?.removeFromParentViewController()
}
}
FirstViewController,
protocol FirstViewControllerProtocol {
// Use protocol/delegate to communicate within two view controllers
func dismiss()
}
class FirstViewController: UIViewController {
var delegate: FirstViewControllerProtocol?
override func viewDidLoad() {
super.viewDidLoad()
}
#IBAction func dismiss(_ sender: Any) {
delegate?.dismiss()
}
deinit {
print("BYE")
}
}
What you want is an "unwind segue":
https://developer.apple.com/library/archive/featuredarticles/ViewControllerPGforiPhoneOS/UsingSegues.html#//apple_ref/doc/uid/TP40007457-CH15-SW8
https://developer.apple.com/library/archive/technotes/tn2298/_index.html
It allows you to dismiss multiple view controllers at the same time, without having to know how many there are in the stack.
In VC1 you would implement an IBAction called (for instance) unwindToRoot. Then in the storyboard for VC3, you wire up your Done button to the Exit object and choose the unwindToRoot action.
When that button is pressed, the system will dismiss all the view controllers it needs to bring you back to VC1.
This is better than calling presentingViewController?.presentingViewController?.dismiss(), because VC3 doesn't need to know anything about the view controller hierarchy underneath it.
viewController's view is not loaded just after that viewController is pushed into navigation controller.
This is my code snippet.
- (void)myMethodInClassA {
// window's root view controller is navigation controller
UINavigationController *naviCtrl = (UINavigationController*)[UIApplication sharedApplication].keyWindow.rootViewController;
MyViewController *myVC = [[MyViewController alloc] initWithNibName:#"MyViewController" bundle:nil];
[naviCtrl pushViewController:myVC animated:NO];
// at this point, myVC's view is NOT loaded
}
When I call myMethodInClassA, myVC's viewDidLoad is called AFTER that method returns. I'd expected that myVC's view is loaded just after navigation controller's pushViewController:animated: is called and before myMethodInClassA returns.
When exactly view controller's view is loaded? Apple's documentation just says it is loaded when it is first accessed. It's a bit ambiguous. why doesn't navigation controller's pushViewController: access view controller's view?
p.s. sorry for initial ambiguous question.
Pushing a view controller (VC) onto a navigation controller's stack makes the VC into a child view controller of the navigation controller (which is a container view controller). Creating such a child-parent relationship is a distinct step which does not cause the child VC's view to be loaded immediately. Rather the container VC loads the view at a later time. I believe there is no explicit specification for what "later" means - usually it will be when the container VC has decided that the time has come to integrate the child VC's view into the container VC's view hierarchy. But basically it simply happens at the discretion of the container VC's implementation.
That being said, anyone can force a VC's view to be loaded by simply accessing the VC's view property. For instance, in your code you could add this line
myVC.view;
which would trigger loadView and then viewDidLoad in MyViewController.
However, in your case if MyViewController needs to react to the event that it has been associated with a container VC, then it would be better to override one (or both?) of the following methods in MyViewController:
- (void) willMoveToParentViewController:(UIViewController*)parent
{
// write your code here
}
- (void) didMoveToParentViewController:(UIViewController*)parent
{
// write your code here
}
You need to be aware, though, that willMoveToParentViewController and didMoveToParentViewController are also invoked when MyViewController is popped from its parent navigation controller's stack. You can detect that this is the case by checking the parent argument for nil.
(Swift 2)
Since this question doesn't have an accepted answer...
What I ended up doing is create a convenience init at the child view controller:
convenience init() {
self.init(nibName: "ChildViewController", bundle: nil)
//initializing the view Controller form specified NIB file
}
and in the parentViewController's viewDidLoad():
let commentsView = CommentsViewController()
self.addChildViewController(commentsView)
self.momentsScrollView.addSubview(commentsView.view)
commentsView.didMoveToParentViewController(self)
As stated above,viewDidLoad gets called once when a view is pushed,you might want to do your stuff in viewWillAppear or viewDidAppear.
Ya if that ViewController will be already pushed in navigationController stack then ViewDidLoad method will not be called again.
First time when you will push that ViewController then viewDidLoad will be called.
So if you need that your some functionality is to be executed every time then implement it in viewWillAppear method because it will be called every-time you push your viewController.
Hope it helps you.
are you pushing the view controller for the first tym?if YES then only viewDidLoad() of the controller will be called and if its already pushed and this is not the first tyn then viewWillAppear () will be called.(or) if you are making a new instance every tym u push it then viewDidLoad() will be called.
I find that I have to call loadViewIfNeeded()
https://developer.apple.com/documentation/uikit/uiviewcontroller/1621446-loadviewifneeded