How dismiss modal view form sheet controller on outside tap? - ios

How dismiss modal view form sheet controller on outside tap?
#IBAction func btnAction(sender: AnyObject) {
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let vc = storyboard.instantiateViewControllerWithIdentifier("popoverID") as! PopoverViewController
vc.modalPresentationStyle = .FormSheet
vc.popoverPresentationController?.sourceView = self.view
self.presentViewController(vc, animated: true, completion: nil)
}

Related

How can I present VC and set as new Root viewController in Swift?

I have this warning:
Presenting view controllers on detached view controllers is discouraged
I need to know how can set my rootViewController in another VC and avoid this warning
I have this code in my VC:
#IBAction func dissmissInfo(_ sender: UIButton) {
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let vc = storyboard.instantiateViewController(withIdentifier: "firstVC")
present(vc, animated: true, completion: nil)
})
And in the firstVC I have this:
override func viewDidLoad() {
super.viewDidLoad()
UIApplication.shared.keyWindow?.rootViewController = self
}
but when I try to move to another VC I have the same warning:
Presenting view controllers on detached view controllers is discouraged
You mean you want to set firstVC
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let vc = storyboard.instantiateViewController(withIdentifier: "firstVC")
as the new RootViewController?
If YES:
#IBAction func dissmissInfo(_ sender: UIButton) {
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let vc = storyboard.instantiateViewController(withIdentifier: "firstVC")
UIApplication.shared.keyWindow?.rootViewController = vc
})
Then in firstVC, remove
UIApplication.shared.keyWindow?.rootViewController = self

Popover View is Blank

I am trying to add a popover to my controller using code. For some reason, the popover appears, but there is no content inside.
The code I'm using for the transition is:
#IBAction func presentPopover(_ sender: UIButton) {
//performSegueWithIdentifier("Popover", sender: self)
let vc = PopoverViewController()
vc.modalPresentationStyle = .popover
let popover = vc.popoverPresentationController!
popover.delegate = self
popover.permittedArrowDirections = .right
vc.popoverPresentationController?.sourceView = sender
vc.popoverPresentationController?.sourceRect = sender.bounds
...
present(vc, animated: true, completion: nil)
}
The popover view is made in the storyboard, and is of class PopoverViewController After doing some testing it says that the PopoverViewController's viewDidAppear is triggered.
as rmaddy mention The line let vc = PopoverViewController() does not use the storyboard. so you need to do it like that
if let vc = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "PopoverViewController") as? PopoverViewController {
vc.modalPresentationStyle = .popover
let popover = vc.popoverPresentationController!
popover.delegate = self
popover.permittedArrowDirections = .right
vc.popoverPresentationController?.sourceView = sender
vc.popoverPresentationController?.sourceRect = sender.bounds
self.present(vc, animated: true, completion: nil)
}

Attempt to present UIImagePickerController on ViewController whose view is not in the window hierarchy

I'm in trouble, after that I'm moving from View Controller to a second View Controller and then switching back to the View Controller and clicking on the UI Image Picker it's not appearing and bringing me back to the second View Controller. On the debugger it's appearing:
Attempt to present on whose view is not in the window hierarchy!
I'm really becoming crazy. My code that is bringing me back from second View Controller to the first View Controller is inside a button and it's below:
#IBAction func profileButton(_ sender: Any) {
let webVC = UIStoryboard.init(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "ViewController") as! ViewController
webVC.profileLink = linkProfilo
self.present(webVC, animated: false, completion: nil)
}
in second view controller add this variable:
var firstVC: ViewController?
and this setter function to set it
setup(firstVC: ViewController) {
self.firstVC = firstVC
}
in the first view controller, when presenting second view controller pass in a reference to self (first view controller)by calling the setup function
let secondVC = UIStoryboard.init(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "SecondViewController") as! ViewController
secondVC.setup(firstVC: self)
self.present(secondVC, animated: false, completion: nil)
now when button is clicked in secondVC,
guard let first = self.firstVC else {return}
first.profileLink = linkProfilo
self.dismiss(animated: false, completion: nil)
I finally found the way for fix all of this:
#IBAction func profileButton(_ sender: Any) {
if let webVC = UIStoryboard.init(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "ViewController") as? ViewController {
let appDelegate = UIApplication.shared.delegate as! AppDelegate
webVC.profileLink = linkProfilo
dismiss(animated: false, completion: nil)
appDelegate.window?.rootViewController!.present(webVC, animated: false, completion: nil)
}
}
it's enough to add the appDelegate sharing method and dismiss before to present the webVC for share the data in the right way and don't cause problems on the first view when presented.

Show and dismiss View Cntrollers in swift 4

I'm trying to present a view controller and dismiss my current modal view. I saw the answers on StackOverflow, but I don't understand it.
self.dismissViewControllerAnimated(true, completion: {
let vc = self.storyboard?.instantiateViewControllerWithIdentifier("myViewController")
self.presentViewController(vc!, animated: true, completion: nil)
})
Could anyone please tell me, how to do it simply? Thanks
Check your main controller in stack and pop all other view controllers and then present new View Controller.
if let controllers = self.navigationController?.viewControllers {
for vc in controllers {
// Check if the view controller is of MainViewController type
if let myVC = vc as? MainViewController {
self.navigationController?.popToViewController(myVC, animated: false)
}
}
}
Push view controller :
let secondVC = self.storyboard?.instantiateViewController(withIdentifier: "secondVC") as! SecondViewController
self.navigationController?.pushViewController(secondVC, animated: true)
or Present View Controller :
let next = self.storyboard?.instantiateViewController(withIdentifier: "secondVC") as! SecondViewController
self.present(next as UIViewController, animated: true, completion: nil)
You can use this code to Present viewcontroller.
let yourVC = UIStoryboard(name: storyBord, bundle: nil).instantiateViewController(withIdentifier:ID) as! yourViewController
let navController = UINavigationController(rootViewController: yourVC)
self.present(navController, animated: true, completion: {
})
And for dissmissing use this in your presented viewController
self.dismiss(animated: true, completion: {
})

How to navigate and pass data to another view controller without use of segue in swift

Hy, I am new to swift. I have on question. I have one view controller in which have a textbox and button and similarly I have second view controller which have a textbox and button. I want to do that whenever I pressed button of first view controller The second view controller appear and data of textbox of first view controller will pass to the textbox of second view controller but without the use of segue.
Please help me.
In FirstViewController.swift
#IBAction weak var textFiled: UITextField!
#IBAction func sendDatatoNextVC(sender: UIButton) {
let mainStoryboard = UIStoryboard(name: "Storyboard", bundle: NSBundle.mainBundle())
let vc : SecondViewController = mainStoryboard.instantiateViewControllerWithIdentifier("SecondViewControllerSBID”) as SecondViewController
vc. recevedString = textFiled.text
self.presentViewController(vc, animated: true, completion: nil)
}
In SecondViewController.swift
var recevedString: String = “”
#IBAction weak var textFiled2: UITextField!
override func viewDidLoad() {
super.viewDidLoad()
textFiled2.text = recevedString
}
You can use instantiateViewControllerWithIdentifier if you don't want to use segue.
Consider below code:
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let someViewConroller = storyboard.instantiateViewControllerWithIdentifier("someViewControllerID") as! SomeViewController
someViewConroller.someObject = yourObject //here you can assign object to SomeViewController
self.presentViewController(someViewConroller, animated: true, completion: nil)
try this:
if you creating custom view controller.
var storyboard = UIStoryboard(name: "IDEInterface", bundle: nil)
var controller = storyboard.instantiateViewControllerWithIdentifier("IDENavController") as! MyCustomViewController
controller.exercisedPassed = "Ex1"
self.presentViewController(controller, animated: true, completion: nil)
In first VC ,
let storyboard = UIStoryboard(name: "Main", bundle: nil)
var vc = storyboard.instantiateViewController(withIdentifier:"SecondViewController") as! SecondViewController
vc.detail = [name,age,sal]
self.present(vc, animated: true, completion: nil)
In second VC ,
var detail:[String]?

Resources