Presenting ViewController will be loaded but does not appear - ios

The presenting ViewController seems to load, but the screen is actually not reloading. After I touch the screen the ViewController is visible.
This is my instantiateViewController code:
let v = self.storyboard?.instantiateViewController(withIdentifier: "BrowsingDaragaViewController") as! BrowsingDaragaViewController
self.present(v, animated: true, completion: nil)
In the BrowsingDaragaViewController is no additional code. The viewDidAppear() function is not called until I touch the screen.
I'm using Xcode 9 with Swift4.
edit:
presentation code is in tableview(didSelectedRow at indexPath) but when i but it in button action it works fine

If you have used any gesture recogniser then remove it and check if it causing the problem?
And if You are not using gesture recogniser then use below code in didSelectRowAtIndexPath
// Your stuff
DispatchQueue.main.async(execute: {
self.present(yourViewControllerHere, animated: true, completion: nil)
})
// Your stuff
OR
Still facing issue then make sure you tableView has single selection set in your XIB or StoryBoard.

Related

Pop to next viewController in ios

I used this code :
self.navigationController?.popToViewController(vc, animated: true)
By using this it is poping to view controller but tableView is not loading at a time,i need to go back and come again then it is loading.
This code :
let vc = self.storyboard?.instantiateViewController(withIdentifier: "DiscountViewController") as! DiscountViewController
self.navigationController?.pushViewController(vc , animated: true)
By using this code i'm popping to view controller ,also loading tableView at a time, but the problem is, that it is going two or three to return back because we are pushing the viewController.
How can solve this by using popToViewController(vc, animated: true) and load tableView at a time?
Initially this code
self.navigationController?.popToViewController(vc, animated: true)
has worked. Whatever I'm trying also I'm not able to pop view controller and reload() tableView when popViewController is used.
How to solve this?
If you are popping back to a previously displayed view controller, then you can reload your tableView by doing reloadData in that DiscountViewController's viewWillAppear function.

Dismiss one custom popup UIViewController and present another custom popup UIViewController straight away -SWIFT

I have an application with a main UIViewController. When I press a button ("Save") I have a custom popup (UIViewController) present itself. Within this popup I have another button, whereby if I press it I want to dismiss the current popup view controller and then immediately present another different custom popup view controller. I am able to dismiss the first popup , but then i get an error(see below). I am using a protocol to get this working, but I am making a mistake somewhere. Please can some one advise?
[![enter image description here][1]][1]
class mainViewController: UIViewController, popUpDismissedDelegate{
// CUSTOM PROTOCOL DELEGATE FUNCTION
func popUpDimissed() {
// SHOW ANOTHER POPUP TO CREATE CUSTOM HASHTAGS!
let createTagVC = storyboard?.instantiateViewController(withIdentifier: "createTag") as! CreateHashTagPopUpViewController
present(createTagVC, animated: true, completion: nil)
}
// SAVE PDF
#IBAction func savePdf(_ sender: Any) {
// SHOW CUSTOM SELECTION OH HASHTAGS TO ASSIGN PDF
let popUpVC = storyboard?.instantiateViewController(withIdentifier: "hashtagpicker") as! CustomHashTagPopup
popUpVC.delegate = self
present(popUpVC, animated: true, completion: nil)
}
}
protocol popUpDismissedDelegate {
func popUpDimissed()
}
class CustomHashTagPopup: UIViewController, UITableViewDelegate, UITableViewDataSource{
var delegate: popUpDismissedDelegate!
// TAP ON CELLS
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
if(indexPath == [0,0]){
// OPTION TO CREATE A NEW HASHTAG
self.dismiss(animated: true) {
self.delegate.popUpDimissed()
}
}else{
// DO NOTHING
// TO SELECT A HASH TAG USE NEEDS TO PRESS ON THE CHECKBOX!
}
}
}
ERROR:
Warning once only: Detected a case where constraints ambiguously
suggest a height of zero for a tableview cell's content view. We're
considering the collapse unintentional and using standard height
instead.
popup - viewDidDisappear
Could not cast value of type 'UIViewController' (0x10c1ec1f0) to
'zapdocuments.CreateHashTagPopUpViewController' (0x107cd0520).
2018-08-28 18:18:48.196815+0100 zapdocuments[28989:4660894] Could not
cast value of type 'UIViewController' (0x10c1ec1f0) to
'zapdocuments.CreateHashTagPopUpViewController' (0x107cd0520).
You didn't actually post the error, but I think I know what the problem is. You're probably trying to present the next VC before the old one actually dismissed.
You should use the completion parameter of the dismiss method and put your delegate callback in there, to ensure you don't try to do anything until it's fully dismissed.
Move your presentation of createTagVC to the next loop of the runloop. This should guarantee that the UI is in the correct "non-presenting" state.
DispatchQueue.main.async {
present(createTagVC, animated: true, completion: nil)
}
Sometimes, some things aren't really done until the runloop ends. Best to start fresh in the next one.

Swift-4: Can't move to another ViewController from a ViewController

I have designed a UITableviewCell using Xib. The cell has a button when the button clicked it should move to another viewController. I have written delegate method to trigger the function in viewController, in the function, I used PushViewController but It has not moved to another ViewController so how to resolve it?. Also, I have included UINavigationController as RootViewController by Embaded in the project. The following method is triggered by the button when clicked.
func showMoreAboutLeads(index: IndexPath)
{
print("Show_More_About_Leads:",index)
let detailController = self.storyboard?.instantiateViewController(withIdentifier: "DetailsLeadController") as! DetailsLeadController
detailController.leadsDetail = self.leadsArray[index.row] as! NSDictionary
self.navigationController?.pushViewController(detailController, animated: true)
}
if the function is called when clicked, you must make sure that the page has a navigation bar or not.
otherwise you will not be able to push the page but must present the page
self.present(detailController, animated: true, completion: nil)

Using viewDidAppear to present a View Controller, re-opening it when it's closed

In my App, I've created a new storyboard that serves as a very basic tutorial for how to use certain features. (Instructions.storyboard). This storyboard has it's own class - InstructionsVC.swift
I want to present InstructionsVC when MainVC loads within viewDidAppear.
It works great. Fires up on App load just like it's supposed to. The problem occurs when I press the [Close] button on the Instructions interface. It closes the VC, fades to the main screen, and then immediately fires the Instructions VC back up.
How can I prevent the Instructions VC from loading back up once it's closed?
func openInstructions() {
let storyboard = UIStoryboard(name: "Instructions", bundle: nil)
let instructionsView = storyboard.instantiateViewController(withIdentifier: "instructionsStoryboardID")
instructionsView.modalPresentationStyle = .fullScreen
instructionsView.modalTransitionStyle = .crossDissolve
self.present(instructionsView, animated: true, completion:nil)
}
override func viewDidAppear(_ animated: Bool) {
openInstructions()
}
And within my instructions class, I have the following action on the close button:
#IBAction func closeButtonPressed(_ sender: UIButton) {
let presentingViewController: UIViewController! = self.presentingViewController
presentingViewController.dismiss(animated: true, completion: nil)
}
Note - I'd rather not use UserDefaults to resolve this, because I'm going to be incorporating something similar in other parts of the App and don't want to resort to UserDefaults to achieve the desirable behavior.
Thanks in advance buddies!
viewWillAppear and viewDidAppear are called every time a view controller's content view becomes visible. That includes the first time it's rendered and when it's shown again after being covered by a modal or by another view controller being pushed on top of it in a navigation stack.
viewDidLoad is only called once when a view controller's content view has been loaded, but before it is displayed. Thus when viewDidLoad is called it may be too soon to invoke your second view controller.
You might want to add an instance variable hasBeenDisplayed to your view controller. In viewDidAppear, check hasBeenDisplayed. If it's false, display your second view controller and set hasBeenDisplayed to true.

MMDrawerController can't close drawer

I'm trying to use MMDrawerController in my application, but I can't get the side drawer to close with gestures on the center view. I've used MMDrawerController somewhere else in my app with great success, but can't figure out why its not working in this case.
The top-level View Controller is a UINavigationController, whose default view controller is the MasterViewController (source below). This class extends MMDrawerController and configures the view for what I want (center and right, close gestures, max width). The center view has a button that opens the drawer. Once the drawer is open I can't close it with gestures on the center view. I added a button to the drawer and it can close the drawer programmatically, but I need to be able to tab/pan on the center view.
class MasterViewController: MMDrawerController {
override func viewDidLoad() {
let centerView = storyboard!.instantiateViewControllerWithIdentifier("CenterControllerName") as? CenterControllerType
super.setCenterViewController(centerView, withCloseAnimation: false, completion: nil)
let drawer = storyboard!.instantiateViewControllerWithIdentifier("Drawer") as? DrawerType
super.rightDrawerViewController = drawer
super.setMaximumRightDrawerWidth(200, animated: true, completion: nil)
super.closeDrawerGestureModeMask = MMCloseDrawerGestureMode.PanningCenterView | MMCloseDrawerGestureMode.TapCenterView
}
}
The function to open the drawer:
#IBAction func drawerButtonPressed(sender: AnyObject) {
drawer?.openDrawerSide(MMDrawerSide.Right, animated: true, completion: nil)
}
I was able to work around this by placing a ContainerView as the only object in my view and then configuring the MMDrawerContainer from my ViewController. It doesn't seem like the right answer but everything looks and functions right from the user's perspective.
Ok so I run into the same issue, and I am almost sure that it comes from this:
Ok, the VC1 (with the star) is our application Home, the VC2 is the left drawer, and the VC3 is the DrawerController.
During a long time I tried to put gesture functions in the VC1 and VC2, but only opening in the VC1, and closing in the VC2 would work.
The functions I used in VC1:
let rightSwipe = UISwipeGestureRecognizer(target: self, action: Selector("showDrawer:"))
rightSwipe.direction = .Right
view.addGestureRecognizer(rightSwipe)
...
func showDrawer(sender: UISwipeGestureRecognizer) {
if let m = mmdc {
if !m.isLeftDrawerOpen() {
m.toggleDrawerSide(MMDrawerSide.Left, animated: true, completion: nil)
}
}
}
And the same one in VC2 but with the LeftSwipe and closeDrawer.
The solution is tu put both of these functions and gestures recognizer in the VC3 (DrawerController).
The problem is coming from the fact that your gestures are defined for a given VC, but when you open the drawer, it changes the current VC by a new one, and the previous VC is just displayed but can't be interactif with. By putting things in the parentVC of VC1/VC2 it solves the problem.

Resources