Usually, I change view like this:
var goBackToMainView:MainViewController = self.storyboard?.instantiateViewControllerWithIdentifier("navigation2") as MainViewController
self.presentViewController(goBackToMainView, animated: true, completion: nil)
But if I try to do this in my custom UITableViewCell when the user tap a button, it gives me errors like customTableViewCell does not have a member named 'storyboard' and 'presentViewController'
How can I change views in my TableViewCell ?
Have you tried adding as a subview?
Example:
var tableViewCell = UITableViewCell(frame: aFrame)
tableViewCell.addSubview(aView)
Why do it in the UITableViewCell subclass? You should be doing your view management in the ViewController.
In your view controller:
import UIKit
// Take note of UITableViewDelegate, you'll need it to be able to use
// tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath)
class MyTableViewController: UITableViewController, UITableViewDelegate {
// Assuming that you have a storyboard variable in AppDelegate
let appDelegate: AppDelegate = UIApplication.sharedApplication().delegate as AppDelegate
// .. more code here like viewDidLoad, etc.
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
var viewController = appDelegate.storyboard.instantiateViewControllerWithIdentifier("MainViewController") as MainViewController
// This will probably appear as a modal
self.presentViewController(viewController, animated: true, completion: nil)
}
}
Note: I just did a quick playground for the code above so you may need to unwrap optionals.
If you have your view controller embedded in a navigation controller, you might want to use Unwind Segues. More information about that here: What are Unwind segues for and how do you use them?
Cheers!
Related
Instead of assigning the view controller as the UITableViewDelegate, I'm trying to reduce the code in the view controller by creating an extension for the UITableViewDelegate.
Why am I getting the error "Use of unresolved identifier companyDetailVC" for the line companyDetailsVC = CompanyDetailsViewController() when that is correct Swift 3 syntax?
Code
extension TableViewDelegate: UITableViewDelegate {
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
companyDetailsVC = CompanyDetailsViewController()
self.present(companyDetailsVC, animated: true, completion: nil)
}
}
Edit: I'm trying to do this programmatically without storyboard. I created a UITableViewDelegate extension because I'm trying to reduce the code in the View Controller.
The code for presenting the Viewcontroller should be somewhat like this
and the extension should be like
extension YourClassNameHere: UITableViewDelegate {
//then your did select method comes here and in that put this code for presenting the viewcxontroller
let companyDetailsVC = CompanyDetailsViewController() //change this to your class name
self.present(companyDetailsVC, animated: true, completion: nil)
}
Replace your code with below code. You will need to provide your ViewController to create its extension and to use the property and method of it.
Make sure companyDetailsVC is declared in the controller. You don't need to use self until its called from block.
extension yourViewController: UITableViewDelegate {
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
self.companyDetailsVC = CompanyDetailsViewController()
self.present(companyDetailsVC, animated: true, completion: nil)
}
}
Hi I'm creating an app related to iOS. This app display number of people in tableview. Each cell in tableview shows the name of the person and detail of the person also each person have audio file attached. when i select the any cell and hit the shortlist button i want to display this information into another screen and email the selected cell to anyone. So i want to show name, detail, and the audio file into another screen. i am using swift language and Xcode 7. Please help me to find answer.
Please implement TableView Delegate method below, When you select a cell this method will invoke. There you can instantiate new ViewController where you want to display the data.
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath)
First of all you need to find out what cell was selected. Using the didSelectRowAtIndexPath function.
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath)
{
}
After you have found out what row was selected you can then segue into the next view controller but before this is done you will have to call the prepareForSegue function which allows the variables to be passed into the view controller.
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject!)
{
}
Use didSelectRowAtIndexPath method current ViewController,
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath)
{
let storyBoard = UIStoryboard(name: "Main", bundle: nil)
let sample = storyBoard.instantiateViewControllerWithIdentifier("YourViewControllerIdentifierName") as! ViewControllerB
sample.mainArray = yourArrayName.mutableCopy() as! NSMutableArray
self.navigationController?.pushViewController(sample, animated: true)
}
}
If you send data to Another ViewControllerB declare the variable mainArray,
import UIKit
class ViewControllerB: UIViewController {
var mainArray = NSMutableArray()
}
The above example is one way, you will declare the variable's to pass many data tableview didSelectRowAtIndexPath to Another ViewController,
hope its helpful
I am using a custom cell class with a button in it.
import UIKit
protocol TouchDelegateForShotsCell {
func touchedTwitterButton()
}
class ShotsCell : UITableViewCell {
let touchDelegateForShotsCell: TouchDelegateForShotsCell = MasterViewController()
#IBAction func twitterButtonPressed(sender: AnyObject) {
touchDelegateForShotsCell.touchedTwitterButton()
}
}
When the button is pressed I call a delegate function in the MasterViewController which contains the following standard code for sharing on Twitter:
func touchedTwitterButton() {
var shareToTwitter :SLComposeViewController = SLComposeViewController(forServiceType: SLServiceTypeTwitter)
self.presentViewController(shareToTwitter, animated: true, completion: nil)
}
I receive the error :
"Attempt to present ViewController whose view is not in the window hierarchy!".
I've tried several workarounds. For example, creating a separate view controller and performing a segue from the button. That works in part, however, when I try to pass data I get another error :
"'NSInvalidArgumentException', reason: Receiver ViewController has no segue with identifier 'segueToTwitterShare'".
Which isn't true. For this I tried deleting my app and restarting xcode which worked for some people. However, the problem persists.
let touchDelegateForShotsCell: TouchDelegateForShotsCell = MasterViewController()
It's not the good delegate. When you write MasterViewController() you create a new instance of MasterViewController (so it is not in the window hierarchy) but you don't pass the intense who is executed.
I suggest you an example to better understand what is the problem and see where changes is needed.
I add an init() function into ShotsCell :
class ShotsCell : UITableViewCell {
var touchDelegateForShotsCell: TouchDelegateForShotsCell!
init(withDelegate delegate: MasterViewController){
super.init(style: UITableViewCellStyle.Default, reuseIdentifier: "cellIdentifier")
touchDelegateForShotsCell = delegate
}
#IBAction func twitterButtonPressed(sender: AnyObject) {
touchDelegateForShotsCell.touchedTwitterButton()
}
}
And pass your MasterViewController to your cell in cellForRowAtIndexPath :
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = ShotsCell(withDelegate: self)
return cell
}
Hope that helps. It works for me.
I'm trying to change the text that follows the < on the Navigation Controller. I successfully accomplished this in a UIViewController with a TableView that is its own DataSource and Delegate with code like this:
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
var newVC = NewViewController()
self.title = "HomeVC"
navigationController?.showViewController(newVC, sender: indexPath)
}
When "HomeViewController" is presented, the back button on the Navigation Controller shows "< HomeVC" - the ViewController that we just came from.
Now, I'm trying to accomplish this with another UIViewController that has a TableView, but the DataSource and Delegate are in a separate file. Following the same logic as above, this is what I tried:
class CalendarTableDataSource: NSObject, UITableViewDataSource, UITableViewDelegate {
...
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
var newVC = NewViewController()
self.navController?.title = "HomeVC" // ----> did not work
HomeViewController().title = "HomeVC" // ----> did not work
self.navController?.showViewController(newVC, sender: indexPath)
}
}
Ideas, anyone? Assuming that I want to keep the DataSource and Delegate in a separate file.
I can't switch to another view controller using UITableView. My code is like below.
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
tableView.deselectRowAtIndexPath(indexPath, animated: true)
let storyBoard : UIStoryboard = UIStoryboard(name: "Main", bundle:nil)
let dashViewController = storyBoard.instantiateViewControllerWithIdentifier("menuView") as DashViewController
self.presentViewController(dashViewController, animated:true, completion:nil)
}
I click one of my cells, and it just does nothing.
I already found the answer, I forgot to put UITableViewDelegate.
class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate{
}
And assign tableview with view controller