Modal VC not open (Swift) - ios

How can I make the VC appear also
I tried this but ms loaded clean without buttons and TextView.
let modalViewController = ClipboardViewController()
modalViewController.modalPresentationStyle = .overCurrentContext
present(modalViewController, animated: true, completion: nil)
Example:
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
if indexPath.row == 0 {
self.navigationController!.pushViewController(self.storyboard!.instantiateViewController(withIdentifier: "ViewController") as UIViewController, animated: true)
} else if indexPath.row == 1 {
// VC with animation
} else if indexPath.row == 2 {
self.navigationController!.pushViewController(self.storyboard!.instantiateViewController(withIdentifier: "Favorite") as UIViewController, animated: true)
} else if indexPath.row == 3 {
self.navigationController!.pushViewController(self.storyboard!.instantiateViewController(withIdentifier: "Settings") as UIViewController, animated: true)
}
}

let modalViewController = self.storyboard?.instantiateViewController(withIdentifier: "ClipboardViewController ID") as? ClipboardViewController
modalViewController?.modalPresentationStyle = .overCurrentContext
self.present(modalViewController!, animated: true, completion: nil)

Related

Presenting Full Screen Programmatically

I am presenting views from a switch statement as seen in the code. What code would i add to present each view in full screen. I have tried a few methods with self, but nothing changes. I have also tried setting the screen from auto to full screen in storyboard. Any ideas??
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
tableView.deselectRow(at: indexPath, animated: true)
let settingsSection = SettingsSection(rawValue: indexPath.section)
switch settingsSection {
case .social:
let value = SocialOptions(rawValue: indexPath.row)
switch value {
case .editProfile:
present(editProfileVC, animated: true) {
}
case .changePassword:
present(changePasswordVC, animated: true) {
}
case .deleteAccount:
present(deleteVC, animated: true) {
}
default:
debugPrint("Invalid Selection")
}
case .communications:
let value = CommunicationOptions(rawValue: indexPath.row)
switch value {
case .notifications:
present(notificationVC, animated: true) {
}
case .language:
present(languageVC, animated: true) {
}
case .contactUs:
present(contactVC, animated: true) {
}
default:
debugPrint("Invalid selection")
}
default:
debugPrint("Invalid selection")
}
}
}
Embed your ViewControllers in a UINavigationController. You can set UINavigationController().navigationBar.isHidden = true if you don't want the nav bar at the top. The code you want is UIViewController().modalPresentationStyle = .fullScreen.
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
tableView.deselectRow(at: indexPath, animated: true)
let settingsSection = SettingsSection(rawValue: indexPath.section)
var vc: UIViewController?
var navController: UINavigationController!
switch settingsSection {
case .social:
let value = SocialOptions(rawValue: indexPath.row)
switch value {
case .editProfile: vc = editProfileVC
case .changePassword: vc = changePasswordVC
case .deleteAccount: vc = deleteVC
default: debugPrint("Invalid Selection")
}
case .communications:
let value = CommunicationOptions(rawValue: indexPath.row)
switch value {
case .notifications: vc = notificationVC
case .language: vc = languageVC
case .contactUs: vc = contactVC
default: debugPrint("Invalid selection")
}
default: debugPrint("Invalid selection")
}
if let vc = vc {
navController = UINavigationController(rootViewController: vc)
navController.modalPresentationStyle = .fullScreen
self.present(navController, animated: true)
}
}

Need to double tap on cell to present modally VC

I am generating table cell.
cell2 = settingsTableView.dequeueReusableCell(withIdentifier: "ModuleCell", for: indexPath) as! ModuleCell
It looks fine, the cell has tag = 2000.
In override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath), I am checking the tag end if tag == 2000 I want to present modal view. I am doing that in this way
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let modalView = storyboard.instantiateViewController(withIdentifier: "ModalView")
present(modalView, animated: true, completion: nil)
Then in modalView I have a button which should dismiss modalView, what's happening as expected.
#IBAction func saveAndClosePopup(_ sender: UIButton) {
UserDefaults.standard.removeObject(forKey: "ActiveCantachoOptions")
UserDefaults.standard.set(Modules.activeCantachoOptions, forKey: "ActiveCantachoOptions")
self.dismiss(animated: true, completion: nil)
}
However, when I want to immediately present modalView again, sometimes it's okay, but sometimes I need to hit two times on the cell, which should show modalView. After the first hit, there is no difference if I hit the cell again in 1 second or in 30 seconds. The modalView will appear after the second one. What is the cause?
Try this
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath)
{
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let modalView = storyboard.instantiateViewController(withIdentifier: "ModalView")
self.present(modalView, animated: true) {
tableView.deselectRow(at: indexPath, animated: false)
}
}

storyboard?.instantiateViewController(withIdentifier: " ") as! viewcontroller

I have as a Menu tableview in my custom navigation bar. If i click on one cell , it should take me to another view controller. But my app crashes when i click on it.
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return menu_items.count //It has 5 values
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = UITableViewCell(style: UITableViewCellStyle.value1, reuseIdentifier: "cell")
cell.textLabel?.text = menu_items[indexPath.row]
cell.textLabel?.textColor = .white
return cell
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let item = menu_items[indexPath.row]
if (item == "Patienten") {
tableView.removeFromSuperview()
let vc = self.storyboard?.instantiateViewController(withIdentifier: "patientView") as! patientViewVC //App crashes here
self.present(vc, animated: false, completion: nil)
} else if (item == "Mitarbeiter") {
} else if (item == "Kalender") {
} else if (item == "Tourenplanung") {
tableView.removeFromSuperview()
let vc = storyboard?.instantiateViewController(withIdentifier: "tour") as! tourVC //App crashes here
self.present(vc, animated: false, completion: nil)
}else if (item == "Abmelden") {
let vc = self.storyboard?.instantiateViewController(withIdentifier: "loginvc") as! LoginVC //App crashes here
self.present(vc, animated: false, completion: nil)
}
let selectedCell:UITableViewCell = tableView.cellForRow(at: indexPath)!
selectedCell.contentView.backgroundColor = colorLightGreen
}
}
I have many times verified all Identifier names and view controller names are correct.
Make sure you load a correct storyboard before instantiating the viewController.
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let item = menu_items[indexPath.row]
if (item == "Patienten") {
tableView.removeFromSuperview()
let storyBoard = UIStoryboard(name: "StoryboardName", bundle: nil)
let vc = storyBoard.instantiateViewController(withIdentifier: "patientView") as! patientViewVC
self.present(vc, animated: false, completion: nil)
} else if (item == "Mitarbeiter") {
} else if (item == "Kalender") {
} else if (item == "Tourenplanung") {
tableView.removeFromSuperview()
let storyBoard = UIStoryboard(name: "StoryboardName", bundle: nil)
let vc = storyBoard.instantiateViewController(withIdentifier: "tour") as! tourVC //App crashes here
self.present(vc, animated: false, completion: nil)
}else if (item == "Abmelden") {
let storyBoard = UIStoryboard(name: "StoryboardName", bundle: nil)
let vc = storyBoard.instantiateViewController(withIdentifier: "loginvc") as! LoginVC //App crashes here
self.present(vc, animated: false, completion: nil)
}
let selectedCell:UITableViewCell = tableView.cellForRow(at: indexPath)!
selectedCell.contentView.backgroundColor = colorLightGreen
}
StoryboardName above is the name of the storyboard that contains the patientViewVC, tourVC, LoginVC
A view controller in storyboard needs to have a custom class set in Identity inspector:
Then you can cast it to desired type. If all the view controllers are in the same storyboard, you can instantiate them with storyboard property of self. If they are in different storyboard, you have to instantiate UIStoryboard instance with corresponding name and use it.
Btw, why are you calling tableView.removeFromSuperview() after cell selection?

How to disable editing UITableViewController in Popover

I turn on the editing table in the FirstViewController
#IBAction func editButtonPressed(sender: UIBarButtonItem) {
self.tableView.allowsMultipleSelectionDuringEditing = true
if self.editing {
let popoverEditMenu = self.storyboard?.instantiateViewControllerWithIdentifier("popoverEditMenu") as! EditMenuTableViewController
popoverEditMenu.modalPresentationStyle = .Popover
popoverEditMenu.popoverPresentationController!.delegate = self
let popover: UIPopoverPresentationController = popoverEditMenu.popoverPresentationController!
popover.barButtonItem = sender
presentViewController(popoverEditMenu, animated: true, completion: nil)
} else {
editButton.image = UIImage(named: "profile_more")
self.editing = !self.editing
}
}
Editing table is included successfully. After the above actions, I want to finish editing, by clicking on a table cell in a popover, code:
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
let firstTableVC = self.storyboard?.instantiateViewControllerWithIdentifier("firstTableVC") as! FirstTableViewController
tableView.deselectRowAtIndexPath(indexPath, animated: true)
switch indexPath.row {
case 0:
self.dismissViewControllerAnimated(true, completion: nil)
firstTableVC.editing = false // Disable Editing
firstTableVC.editButton.image = UIImage(named: "1461294921_15.Pencil")
default:
break
}
}
But there is no change in the button image, and table editing mode not disabled
Solution found!
The problem was solved by the use of delegation. Thanks to #pbasdf for the tip
import UIKit
protocol SecondTableViewControllerDelegate {
func endEditing()
}
class SecondTableViewController: UITableViewController {
var delegate: SecondTableViewControllerDelegate?
...
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
tableView.deselectRowAtIndexPath(indexPath, animated: true)
switch indexPath.row {
case 0:
self.dismissViewControllerAnimated(true, completion: nil)
delegate?.endEditing()
default:
break
}
}
}
Delegate function in FirstViewController. You need to specify a delegate inheritance in FirstViewController
func endEditing() {
self.editing = false
editButton.image = UIImage(named: "1461294921_15.Pencil")
}
Try to use
firstTableVC.editButton.setImage(UIImage(named:"1461294921_15.Pencil"), forState: UIControlState.Normal)
It must works.

How to call a controller from a modal popover in Swift?

I have three controllers, and i need this navigation:
| 1stVC | -> | popoverVC | -> | 2ndVC |
The first one show a modal view using a popover segue, and then from the modal view, using a protocol, it should show the second controller.
The protocol calls the method, and then, it should call the second controller, but it doesn't!. I have tried performing segue, and calling the controller but nothing happens, in fact, it reloads the first controller instead call the second. Please help. I think that i must have errors in delegation, but i can't figure what.(poor english, i know)
One important thing, the firstViewcontroller is called from another controller that is in a tabbarcontoller.
Thanks in advance.
This is the code:
PopoOverController:
import UIKit
protocol basketDelegate {
func didSelectValue()
}
class PopoverViewController: UITableViewController {
var delegate: basketDelegate!
let options = ["Filters", "Basket"]
override func viewDidLoad() {
super.viewDidLoad()
self.tableView.registerClass(UITableViewCell.self, forCellReuseIdentifier: "reuseIdentifier")
let rdc = storyboard!.instantiateViewControllerWithIdentifier("FirstViewController") as! FirstViewController
self.delegate = rdc
}
// MARK: - Table view data source
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
// #warning Incomplete implementation, return the number of rows
return options.count
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("reuseIdentifier", forIndexPath: indexPath)
cell.textLabel?.textAlignment = .Center
cell.textLabel?.textColor = colorWithHexString("#1C7393")
cell.textLabel?.text = options[indexPath.row]
return cell
}
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath!) {
delegate?.didSelectValue()
}
}
FirstController
import UIKit
class FirstViewController: AEAccordionTableViewController,
UIPopoverPresentationControllerDelegate, UISearchBarDelegate,basketDelegate {
override func viewDidLoad() {
super.viewDidLoad()
var Filters = PopoverViewController()
Filters.delegate = self
}
// MARK: - Popup filter call
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "popoverSegue" {
let popoverViewController = segue.destinationViewController as UIViewController
popoverViewController.modalPresentationStyle = UIModalPresentationStyle.Popover
popoverViewController.popoverPresentationController!.delegate = self
}
}
func adaptivePresentationStyleForPresentationController(controller: UIPresentationController) -> UIModalPresentationStyle {
return UIModalPresentationStyle.None
}
func didSelectValue() {
//Option 1--Failed!
let storyboard : UIStoryboard = UIStoryboard(name: "Main", bundle: nil);
let SecondViewControllerObject : UIViewController = storyboard.instantiateViewControllerWithIdentifier("SecondViewController") as! SecondViewController;
self.presentViewController(SecondViewControllerObject, animated: true, completion: nil);
//Option 2--Failed
self.performSegueWithIdentifier("secondSegue", sender: self)
//Option 3--Failed
let storyboard : UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
let SecondViewControllerObject : UIViewController = storyboard.instantiateViewControllerWithIdentifier("SecondViewController") as! SecondViewController;
let navigationController = UINavigationController(rootViewController: SecondViewControllerObject)
self.presentViewController(navigationController, animated: true, completion: nil)
//Option 4--Failed
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let vc = storyboard.instantiateViewControllerWithIdentifier("SecondViewController") as! SecondViewController
FirstViewController()!.presentViewController(vc, animated: true, completion: nil)
//Option 5--Failed
self.navigationController?.presentViewController(vc, animated: false, completion: nil)
}
}
SecondController
import UIKit
class SecondViewController: AEAccordionTableViewController {
#IBOutlet weak var dismissButton: UIButton!
override func viewDidLoad() {
super.viewDidLoad()
}
#IBAction func dismissTap(sender: UIButton) {
self.dismissViewControllerAnimated(true, completion: nil)
}
}
Your first view controller should be a PopoverViewControllerDelegate, not a basketDelegate.
This behavior can be reached with an Action Sheet. Just in the First Controller add a new method to create a Contextual Menu with the same option to call the second controller. This method is called from a button.
Is a simple solution, without protocols or delegations.
In the first controller:
#IBAction func showSheetASction(sender: UIBarButtonItem) {
print("click")
let alertController = UIAlertController(title: nil, message: nil, preferredStyle: .ActionSheet)
let cancelAction = UIAlertAction(title: "Cancel", style: .Cancel) { (action) in
print(action)
}
alertController.addAction(cancelAction)
let CallSVCAction = UIAlertAction(title: "Filters", style: .Default) { (action) in
// ...
print(action)
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let vc = storyboard.instantiateViewControllerWithIdentifier("SecondViewController") as! SecondViewController
self.presentViewController(vc, animated: true, completion: nil)
}
alertController.addAction(CallSVCAction)
alertController.view.tintColor = colorWithHexString("#1C7393")
self.presentViewController(alertController, animated: true) {
}
}

Resources