I am using a segue(searchSegue) to connect my search screen (SearchViewController) to the search result page (PhotoStreamViewController).
SearchViewController is a collection view. Every collection view cell has a design in Xib file. (Xib file's class is SearchCategoryRowCell)
Currently, when I triggered searchSegue through SearchViewController it works fine. But whenever I trigger the same segue through SearchCategoryRowCell, I am getting Has no segue with identifier 'searchSegue'
SearchViewController.swift
class SearchViewController: UIViewController,UISearchBarDelegate,UICollectionViewDataSource, UICollectionViewDelegateFlowLayout{
...
func searchBarSearchButtonClicked(_ searchBar: UISearchBar) {
searchRedirect(keyword: searchBar.text!)
}
func searchRedirect(keyword:String) {
performSegue(withIdentifier: "searchSegue", sender:self)
PhotoStreamViewController.searchString = keyword
navigationController?.setNavigationBarHidden(false, animated: true)
}
...
}
SearchCategoryRowCell.swift
class SearchCategoryRowCell: UICollectionViewCell {
//the method below is triggered by a click action
#objc func searchRedirection(_ sender:UIButton) {
print("we")
print(subCategoryArray[sender.tag].name)
let searchViewcontroller = SearchViewController(nibName: "SearchViewController", bundle: nil)
searchViewcontroller.searchRedirect(keyword: "otherSearchKeyword")
}
...
}
You've explained the reason perfectly. You are saying:
let searchViewcontroller = SearchViewController(nibName: "SearchViewController", bundle: nil)
searchViewcontroller.searchRedirect(keyword: "otherSearchKeyword")
So searchViewcontroller is a completely new view controller instantiated right there in your code. Thus, it has no segues. The view controller with the segues is the one in the storyboard, which is a completely different view controller.
This
let searchViewcontroller = SearchViewController(nibName: "SearchViewController", bundle: nil)
is not the currently presented VC , you have to use delegate or declare self as a property inside the cell class
class SearchCategoryRowCell: UICollectionViewCell {
var sear:SearchViewController?
}
ans set it inside cellForItem in SearchViewController
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cellID", for: indexPath) as! cellClassName
cell.sear = self
return cell;
}
//
then you can use the presented object
#objc func searchRedirection(_ sender:UIButton) {
print("we")
print(subCategoryArray[sender.tag].name)
sear?.searchRedirect(keyword: "otherSearchKeyword")
}
Related
I have tableView within collectionView and i created using xib. I want to pushViewController when item is selected .I tried pushing the view controller in itemForRowAt method but it's not possible
class SecondTableView: UITableViewCell , UIcollectionViewDelegate ,UICollectionViewDataSource {
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
// let vc = DataTableViewController()
// vc.delegate = self
// vc.pushNav()
let memeVC = storyboard?.instantiateViewController(withIdentifier: "MemeViewController") as! MemeViewController
memeVC.imagePassed = image
navigationController?.pushViewController(memeVC, animated: true)
print("item tapped\(indexPath)")
}
}
errors
Use of unresolved identifier 'storyboard'; did you mean 'UIStoryboard'?
Add segue as mentioned in the image and select show
Set segue identifier in your storyboard as mentioned in the image
Add below Protocol:
protocol CollectionViewCellDelegate: class {
func userDidTap()
}
Add below property in your table view class where the collection view delegate returns.
weak var delegate: CollectionViewCellDelegate?
Update delegate method
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
delegate.userDidTap()
print("item tapped\(indexPath)")
}
}
confirm CollectionViewCellDelegate delegate to your first view controller & add this method:
func userDidTap() {
performSegue(withIdentifier: "showMemeViewController", sender: nil)
}
Add prepare for segue in your first VC.
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "showMemeViewController" {
let memeVC: MemeViewController = segue.destination as! MemeViewController
memeVC.imagePassed = image
}
}
Please try this let me know if you faced any other issue. Thanks.
You're trying to access storyboard property in a tableview cell, which isn't there in the first place, it's a UIViewController property not a UITableViewCell property, hence the error, you'll have to delegate your selection to the viewController then let the controller do the segue normally.
You can start doing that via delegates as I mentioned above, for example if you're passing image to the controller you wanna segue to:
protocol ImageTableViewCellDelegate: class {
// pass the the image or any variable you want in the func params
func userDidTap(_ image: UIImage)
}
then you'd add it to the cell and confirm the controller to it
class SecondTableView {
weak var delegate: ImageTableViewCellDelegate?
}
In your controller:
extension MyViewController: ImageTableViewCellDelegate {
func userDidTap(_ image: UIImage){
let memeVC = storyboard?.instantiateViewController(withIdentifier: "MemeViewController") as! MemeViewController
memeVC.imagePassed = image
navigationController?.pushViewController(memeVC, animated: true)
print("item tapped\(indexPath)")
}
}
this should work, let me know if you had any further problems
I'm using an external delegate file to handle all a UICollectionView's processing and I'm struggling to get the collection view cell to perform a segue based on the selected cell, via the delegate file.
Here's what I currently have inside the delegate file:
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
let cell = collectionView.cellForItem(at: indexPath)
let mainController = MainController()
mainController.performSegue(withIdentifier: "detailSegue", sender: cell)
}
and on the Main Controller I have:
override func performSegue(withIdentifier identifier: String, sender: Any?) {
if identifier == "detailSegue" {
let detailController = DetailController()
self.present(detailController, animated: true)
}
}
The error I'm getting:
Warning: Attempt to present <DetailController: 0x7fbed8e6e4b0> on <MainController: 0x7fbedda79830> whose view is not in the window hierarchy!
I thought I could call up the reference via the delegate and it would present the controller.
Thanks
The reference of MainController that you are trying to get like below is wrong.
let mainController = MainController()
This will not give you the loaded/previewing instance of the object instead of this it will create a new object of MainController for you which is not in the previewing Hierarchy. So you need an actual previewing instance.
Solution
Create a global object of of UIViewController type in your delegate class and pass you previewing class reference so that you may use it when you need.
Example.
class DelegateDataSource: NSObject {
var viewController: UIViewController?
//Other Methods/Objects that you need.
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
let cell = collectionView.cellForItem(at: indexPath)
if let mainController = viewController as? MainController {
mainController.performSegue(withIdentifier: "detailSegue", sender: cell)
}
}
}
class MainController: UIViewController {
var delegateDataSource: DelegateDataSource?
func initializeDelegates() {
//Initialize you datasource object and do some tasks that you need and then assign your current instance to this class like this.
delegateDataSource.viewController = self
}
}
let mainController = MainController()
mainController.performSegue(withIdentifier: "detailSegue", sender: cell)
This will not work, either use delegate pattern or use singleton pattern for your MainController.
Only an already presented controller can present another controller. Is the class implementing didSelectItemAt a UIViewController? If so, why not just do:
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
let cell = collectionView.cellForItem(at: indexPath)
performSegue(withIdentifier: "detailSegue", sender: cell)
}
I need to refresh my collectionView when user returns to that VC because what he/she did in the detailVC has affect on the previous VC data. I tried collectionView.reloadData() in both viewDidLoad() and viewDidAppear() of my VC has the collectionView in it. And It came up that when user taps the 'Back' in detailVC both viewDidLoad() and viewDidAppear() do not work. So, I tried to call one of them in detailVC with instantiate the firstVC(which has the collectionView)
then I got an runtime error which said collectionView is nil. Any thoughts? (BTW, the segue between them is ShowPush, and I can not change it because I have to have the transition of this segue in my app.)
Here is the firstVC:
class SkillsController: UIViewController{
#IBOutlet weak var collectionView: UICollectionView!
var TAGS: [TAG] = []
override func viewDidLoad() {
super.viewDidLoad()
let nib = UINib(nibName: "TagCell", bundle: nil)
collectionView.register(nib, forCellWithReuseIdentifier: "tagCell")
self.sizingCell = (nib.instantiate(withOwner: nil, options: nil) as NSArray).firstObject as! TagCell?
self.loadMore()
}
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
print("back to skills")
self.TAGS = TagManager.shared.tagList
collectionView.reloadData()
}
}
TAGS is my data which is stored in Realm database.
Here is the detailVC:
class SeeSelectedController: UICollectionViewController {
var TAGS: [TAG] = []
#IBOutlet weak var layout: FSQCollectionViewAlignedLayout!
override func viewDidLoad() {
super.viewDidLoad()
if currentTab.shared.isSkill {
self.title = "Selected Skills"
//init tags
let list = RealmManager.shared.skills
if let list = list {
for element in list {
TAGS.append(TAG(n: element.value!, iS: true))
}
}
collectionView?.reloadData()
}else{
self.title = "Selected Needs"
//init tags
let list = RealmManager.shared.needs
if let list = list {
for element in list {
TAGS.append(TAG(n: element.value!, iS: true))
}
}
collectionView?.reloadData()
}
let nib = UINib(nibName: "TagCell", bundle: nil)
collectionView?.register(nib, forCellWithReuseIdentifier: "tagCell")
}
override func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
let item = TAGS[indexPath.row].name!
let currentState = TAGS[indexPath.row].isSelected!
TAGS[indexPath.row].isSelected = currentState ? false:true
if currentState {
print("deselect")
//remove from realm
RealmManager.shared.deleteItemFromList(type: getTypeOfTag(isSkill: currentTab.shared.isSkill), item: item)
}else{
print("select")
//add to realm
RealmManager.shared.addItemToList(type: getTypeOfTag(isSkill: currentTab.shared.isSkill), item: item)
}
if currentTab.shared.isSkill {
let VC: SkillsController = storyboard?.instantiateViewController(withIdentifier: "SkillsController") as! SkillsController
VC.viewDidAppear(true)
}
collectionView.reloadData()
//addd
}
}
So how it is working? in the SkillsVC user can select some tags from a pool, in the detailVC which is SeeSelecteVC he/she can drop selected tags. It is constantly changing in the Realm as you can see. The problem when user has dropped some tags in detailVC and press the Back button, the dropped tags are still looking as selected in SkillsVC. However when if user goes another VC and comes back to SkillsVC (by this way the viewDidLoad() is gonna work) the dropped tags are seems to be unselected. That's all.
If what you are looking for is just to reload on back button
What you can do is create your own custom UIBarButtonItem that will make you navigate backwards from your "detail view controller". What you should do next after adding your own back button is add an IBAction for UIBarBUttonItem and pop your "detail view controller".
Right before you do this, you should create a delegate that will be executed before the popping happens that will reload your UICollectionView.
The following is not the best way to achieve what you want:
In your didSelectItem for your second view controller, you are creating a new view controller here and you shouldn't force call viewDidAppear. Since you are creating a new UIViewController, you are not referencing the previous UIViewController that you came from and soo your UICollectionView is nil.
if currentTab.shared.isSkill {
//remove the below lines and call the delegate here
let VC: SkillsController = storyboard?.instantiateViewController(withIdentifier: "SkillsController") as! SkillsController
VC.viewDidAppear(true)
}
collectionView.reloadData()
What you should be doing is:
You should use delegates to send callbacks to previous view controllers or perform actions.
To create a delegate-
Using the first approach (using your own back button)-
protocol delegateVC{
func reloadCollectionView()
}
class SeeSelectedController: UICollectionViewController{
//add this inside this class
var delegate : delegateVC?
...
//implement your IBAction for back button and inside it-
... {
self.delegate.reloadCollectionView()
}
}
OR the second approach i pointed out (Just change your didSelectItem and it will reload the collectionView, no need to fret about back button at all and save the hassle, i strongly recommend this approach)
override func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
let item = TAGS[indexPath.row].name!
let currentState = TAGS[indexPath.row].isSelected!
TAGS[indexPath.row].isSelected = currentState ? false:true
if currentState {
print("deselect")
//remove from realm
RealmManager.shared.deleteItemFromList(type: getTypeOfTag(isSkill: currentTab.shared.isSkill), item: item)
}else{
print("select")
//add to realm
RealmManager.shared.addItemToList(type: getTypeOfTag(isSkill: currentTab.shared.isSkill), item: item)
}
if currentTab.shared.isSkill {
self.delegate.reloadCollectionView()
}
}
}
And in your first view controller-
func reloadCollectionView(){
collectionView.reloadData()
}
Note: In your prepareForSegue remember to set the delegate of your detail view controller to be your first view controller
I have a UIViewController with a UISegmentedControl from which I'd like to present a UITableViewController as a popover when I click on a segmentedControl segment. The issue I'm having is the moment I click on a segment, the popover starts to load, but crashes as myPopoverTableViewController loads. It crashes in
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell, saying the attributes of my PopoverTableViewCell are nil.
For simplicity's sake, I'll reference my classes here:
myViewController: MyViewController
myPopoverTVController: PopoverTableViewController
myPopoverTVCell: PopoverTableViewCell
In lldb, I checked for the values of the cell, the dataSource, and it seems the only thing that's nil are the the attributes of myPopoverTVCell, which I register in myPopoverTVController's viewWillAppear with the following line:
tableView.register(PopoverTableViewCell.self, forCellReuseIdentifier: "cell")
myPopoverTVController is not connected via a popover segue (though I've tried that) to myViewController. I've checked that I have PopoverTableViewCell referenced in the class for myPopoverTVController's prototype cell. I've double checked the connections from the cell to to the PopoverTableViewCell class. I checked that I've got the Table View Cell's identifier set to cell on the storyboard.
Here's how I begin the popover from myViewController, following Apple's code:
#IBAction func segmentedControlAction(_ sender: UISegmentedControl) {
// instantiate the PopoverTableViewController
let popoverTVC = PopoverTableViewController()
// set variables on it
popoverTVC.selectedSegmentIndex = sender.selectedSegmentIndex
popoverTVC.currentRegion = currentRegion
// disignate presentation style as a popover
popoverTVC.modalPresentationStyle = .popover
present(popoverTVC, animated: true, completion: nil)
let presentationController = UIPopoverPresentationController(presentedViewController: popoverTVC, presenting: self)
presentationController.permittedArrowDirections = .up
presentationController.sourceView = view
presentationController.sourceRect = segmentedControl.frame
}
On myPopoverTVController, here's what my cellForRowAt indexPath looks like:
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
// let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! PopoverTableViewCell
let cell = tableView.dequeueReusableCell(withIdentifier: "cell") as! PopoverTableViewCell
// Configure the cell...
switch selectedSegmentIndex {
case 0, 1:
cell.areaLabel.text = popoverStringArray[indexPath.row]
case 2:
let countryList = locationManager.countryList
let countryCodes = locationManager.countryCodes
cell.areaLabel?.text = countryList[indexPath.row]
cell.flagLabel?.text = countryCodes[indexPath.row]
default:
break
}
return cell
}
I checked the variables that are set upon instantiation on myViewController and they've all got values. It's just the tableViewCell attributes that are nil--lldb returns a memory address for the cell when I type po cell. I've set up UITableViews a million times, but I can't figure this thing out. Any suggestions re: what I'm doing wrong are greatly appreciated. I'll rest assured my problem is a startlingly silly omission on my part. Thank you for reading.
I threw in the towel trying to use Apple's code and figured out an alternative method to get the popover to work.
I ctrl+dragged in storyboard from my myViewController to myPopoverTVController. I set the segue identifier to popoverSegue and set it to display as a popover. I also specified an anchor point.
From there, I gutted the code in segmentedControlAction() to replaced it with the following:
#IBAction func segmentedControlAction(_ sender: UISegmentedControl) {
self.performSegue(withIdentifier: "popoverSegue", sender: segmentedControl.selectedSegmentIndex)
}
I added the following code to my prepareForSegue on myViewController:
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "popoverSegue" {
let destinationViewController = segue.destination as! PopoverTableViewController
destinationViewController.selectedSegmentIndex = sender as! Int
destinationViewController.currentRegion = currentRegion
let popoverController = destinationViewController.popoverPresentationController
if popoverController != nil {
popoverController?.delegate = self
}
}
}
I also added a delegate method to myViewController with an extension:
extension MyViewController: UIPopoverPresentationControllerDelegate {
func adaptivePresentationStyle(for controller: UIPresentationController) -> UIModalPresentationStyle {
return .none
}
}
Lastly, I took out a local reference to the data source in myPopoverTVController so it now looks like this:
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! PopoverTableViewCell
// Configure the cell...
switch selectedSegmentIndex {
case 0:
cell.areaLabel.text = locationManager.cityList(geographicRegion: currentRegion!)[indexPath.row]
cell.flagLabel.isHidden = true
case 1:
cell.areaLabel.text = locationManager.stateList(geographicRegion: currentRegion!)[indexPath.row]
cell.flagLabel.isHidden = true
case 2:
cell.areaLabel?.text = locationManager.countryList[indexPath.row]
cell.flagLabel?.text = locationManager.countryCodes[indexPath.row].flag()
default:
break
}
return cell
}
...and it worked.
The End ;)
I have a TableViewController, TableViewCell and a ViewController. I have a button in the TableViewCell and I want to present ViewController with presentViewController (but ViewController doesn't have a view on storyboard). I tried using:
#IBAction func playVideo(sender: AnyObject) {
let vc = ViewController()
self.presentViewController(vc, animated: true, completion: nil)
}
Error: Value of type TableViewCell has no member presentViewController
Then, I tried
self.window?.rootViewController!.presentViewController(vc, animated: true, completion: nil)
Error: Warning: Attempt to present whose view is not in the window hierarchy!
What am I doing wrong? What should I do in order to presentViewController from TableViewCell? Also how can I pass data to the new presenting VC from TableViewCell?
Update:
protocol TableViewCellDelegate
{
buttonDidClicked(result: Int)
}
class TableViewCell: UITableViewCell {
#IBAction func play(sender: AnyObject) {
if let id = self.item?["id"].int {
self.delegate?.buttonDidClicked(id)
}
}
}
----------------------------------------
// in TableViewController
var delegate: TableViewCellDelegate?
func buttonDidClicked(result: Int) {
let vc = ViewController()
self.presentViewController(vc, animated: true, completion: nil)
}
I receive error: Presenting view controllers on detached view controllers is discouraged
(Please note that I have a chain of NavBar & TabBar behind TableView.)
I also tried
self.parentViewController!.presentViewController(vc, animated: true, completion: nil)
Same Error.
Also tried,
self.view.window?.rootViewController?.presentViewController(vc, animated: true, completion: nil)
Same Error
It seems like you've already got the idea that to present a view controller, you need a view controller. So here's what you'll need to do:
Create a protocol that will notify the cell's controller that the button was pressed.
Create a property in your cell that holds a reference to the delegate that implements your protocol.
Call the protocol method on your delegate inside of the button action.
Implement the protocol method in your view controller.
When configuring your cell, pass the view controller to the cell as the delegate.
Here's some code:
// 1.
protocol PlayVideoCellProtocol {
func playVideoButtonDidSelect()
}
class TableViewCell {
// ...
// 2.
var delegate: PlayVideoCellProtocol!
// 3.
#IBAction func playVideo(sender: AnyObject) {
self.delegate.playVideoButtonDidSelect()
}
// ...
}
class TableViewController: SuperClass, PlayVideoCellProtocol {
// ...
// 4.
func playVideoButtonDidSelect() {
let viewController = ViewController() // Or however you want to create it.
self.presentViewController(viewController, animated: true, completion: nil)
}
func tableView(tableView: UITableView, cellForRowAtIndexPath: NSIndexPath) -> UITableViewCell {
//... Your cell configuration
// 5.
cell.delegate = self
//...
}
//...
}
You should use protocol to pass the action back to tableViewController
1) Create a protocol in your cell class
2) Make the button action call your protocol func
3) Link your cell's protocol in tableViewController by cell.delegate = self
4) Implement the cell's protocol and add your code there
let vc = ViewController()
self.presentViewController(vc, animated: true, completion: nil)
I had the same problem and found this code somewhere on stackoverflow, but I can't remember where so it's not my code but i'll present it here.
This is what I use when I want to display a view controller from anywhere, it gives some notice that keyWindow is disabled but it works fine.
extension UIApplication
{
class func topViewController(_ base: UIViewController? = UIApplication.shared.keyWindow?.rootViewController) -> UIViewController?
{
if let nav = base as? UINavigationController
{
let top = topViewController(nav.visibleViewController)
return top
}
if let tab = base as? UITabBarController
{
if let selected = tab.selectedViewController
{
let top = topViewController(selected)
return top
}
}
if let presented = base?.presentedViewController
{
let top = topViewController(presented)
return top
}
return base
}
}
And you can use it anywhere, in my case I used:
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
let storyboard = UIStoryboard(name: "Main", bundle: Bundle.main)
let vc = storyboard.instantiateViewController(withIdentifier: "WeekViewController")
UIApplication.topViewController()?.navigationController?.show(vc, sender: nil)
}
So self.presentViewController is a method from the ViewController.
The reason you are getting this error is because the "self" you are referring is the tableViewCell. And tableViewCell doesn't have method of presentViewController.
I think there are some options you can use:
1.add a delegate and protocol in the cell, when you click on the button, the IBAction will call
self.delegate?.didClickButton()
Then in your tableVC, you just need to implement this method and call self.presentViewController
2.use a storyboard and a segue
In the storyboard, drag from your button to the VC you want to go.