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)
}
}
Related
I am trying to get data from a different screen but for some reason, the variable does not update. here is the code so far
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let viewController: UIViewController = UIViewController()
switch (indexPath.row) {
case 0:
print("hello")
let vc = AddViewController()
vc.chosenDaily = true
dismiss(animated: true, completion: nil)
here I created a tableView and once the user clicks on the first index of my tableView I want to set a boolean variable to true and return to another screen. I declared this boolean variable on the other screen here.
var chosenDaily:Bool = false
on the same screen, I have a save button and want to print the result.
#IBAction func didTapSaveButton() {
if chosenDaily == true {
print("it's true")
} else if chosenDaily == false {
print("it's false")
}
}
but for some reason, the code doesn't update and returns false instead of true. can anyone help? thanks for any help that i get
I don't know if you are using navigation controller during the transition between screens. But I suppose you are using it. So let's say you have two ViewController. FirstViewController and SecondViewController. And they are also using navigation controller when you make a transition from FirstViewController to the SecondViewController. In the SecondViewController you have your tableView and when you click a row in that tableView you want to send some value to the FirstViewController before dismiss. Add this function in the SecondViewController and call it before dismiss :
func changeValue(myValue : Bool) {
if let navController = SecondViewController as? UINavigationController {
let getfirstVC = navController.topViewController as! FirstViewController
getfirstVC.chosenDaily = myValue
}
}
and your tableview function will be like this :
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let viewController: UIViewController = UIViewController()
switch (indexPath.row) {
case 0:
print("hello")
changeValue(true)
dismiss(animated: true, completion: nil)
Don't forget to change FirstViewController and SecondViewController according to your own.
You can use Delegate to interactive between view.
protocol tableViewControllerDelegate {
func change(value:Bool)
}
class tableViewController: UIViewController {
var del:tableViewControllerDelegate!
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
switch (indexPath.row) {
case 0:
del?.change(value:true)
dismiss(animated: true, completion: nil)
break;
default:
break
}
}
}
Class is reference type, You can also processing data by class.
class AddViewController:UIViewController,tableViewControllerDelegate{
var chosenDaily = false
func change(value:Bool){
chosenDaily = value
}
}
class tableViewController: UIViewController {
var data:Sample!
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
switch (indexPath.row) {
case 0:
data?.chosenDaily = true
dismiss(animated: true, completion: nil)
break;
default:
break
}
}
}
class Sample{
var chosenDaily = false
}
class AddViewController:UIViewController{
var data = Sample()
func open(){
let view = tableViewController()
view.data = data
}
}
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)
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?
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.
I have this strange crash in Crashlytics report and I'm not sure what's going on. I don't even know what does the line number "0" means. Any ideas?
Code from didSelect method:
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
switch indexPath.section {
case 1:
let vc = WTShowDetailVC()
vc.show = bottomFeaturedShows[indexPath.row].show
navigationController?.pushViewController(vc, animated: true)
case 2:
let vc = WTShowDetailVC()
vc.show = lastWatchedShows[indexPath.row]
navigationController?.pushViewController(vc, animated: true)
case 3:
let vc = WTTapperProfileUserVC()
vc.tapper = popularTappers[indexPath.row]
navigationController?.pushViewController(vc, animated: true)
default:
println("Something went wrong")
}
}
This normally happens when you are unwrapping an optional and it's value is nil. Try putting a nil check to navigationController.
if let navigationcontroller: UINavigationController = navigationController {
navigationcontroller.pushViewController(vc, animated: true)
}