I have designed a UITableviewCell using Xib. The cell has a button when the button clicked it should move to another viewController. I have written delegate method to trigger the function in viewController, in the function, I used PushViewController but It has not moved to another ViewController so how to resolve it?. Also, I have included UINavigationController as RootViewController by Embaded in the project. The following method is triggered by the button when clicked.
func showMoreAboutLeads(index: IndexPath)
{
print("Show_More_About_Leads:",index)
let detailController = self.storyboard?.instantiateViewController(withIdentifier: "DetailsLeadController") as! DetailsLeadController
detailController.leadsDetail = self.leadsArray[index.row] as! NSDictionary
self.navigationController?.pushViewController(detailController, animated: true)
}
if the function is called when clicked, you must make sure that the page has a navigation bar or not.
otherwise you will not be able to push the page but must present the page
self.present(detailController, animated: true, completion: nil)
Related
I have app with 7 screens(VC) which are embedded in navigation controller.
And I have one separate VC that is not embedded. This VC is acting like custom popup class PopupView: UIViewController {} and get's called by pressing button from every screen that I have in my app using custom segue (setup as custom segue in storyboard):
open class MIBlurPopupSegue: UIStoryboardSegue {
open override func perform() {
MIBlurPopup.show(destination, on: source)
} }
In this popup there's a button that should open another VC (VC always the same) that is embedded in navigation stack.
What I'm trying to achieve is to actually open that VC that is inside navigation stack by pressing button in Popup VC and then get back to the screen where Popup was called.
So, user journey will look like - Opened VC1(2,3,5,6,7) -> Called popup VC -> Pressed button -> opened VC4 -> pressed navigation back button -> returned to VC1.
I what I have right now:
Connected in storyboard all 6 screens to VC4, with segues ids
Tried performSegue(withIdentifier: "toVC4"), present(vc, animated: true, completion: nil)
let storyboard = UIStoryboard(name: "MyStoryboardName", bundle: nil)
let controller = storyboard.instantiateViewController(withIdentifier: "VC4")
self.present(controller, animated: true, completion: nil)
Using protocols to call func in VC1 but failed.
I'm definitely missing something and would be very thankful if someone could provide code sample to solve this issue.
You can try this inside the popup button's action
self.dismiss(animated:true) {
if let nav = UIApplication.shared.keyWindow?.rootViewController as? UINavigationController {
let vc1 = storyboard!.instantiateViewController(withIdentifier: "MenuId")
let finalVC = storyboard!.instantiateViewController(withIdentifier: "finalId")
nav.setViewControllers([vc1,vc4],animated:true) // set it to false if you want
}
}
There is just a button rimB, in GeneralChooser controller when i clicking on it, i call ChooserPopupViewController which presented modally, where info is returned back when i click on a row , to the previous GeneralChooser controller and displayed in ViewDidLoad ()
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let controller = storyboard.instantiateViewController(withIdentifier: "GeneralChooser") as! GeneralChooser
controller.idSet = idAr[indexPath.row]
controller.nameSet = nameAr[indexPath.row]
dismiss(animated: true)
present(controller, animated: false, completion: nil)
in ViewDidLoad () in GeneralChooser controller i see my passed info
print("id:", idSet)
print("name:", nameSet)
BUT, i cannot set this string on my button`s text!
rimB.setTitle(nameSet, for: .normal)
What is the problem? Perhaps i need to redraw my controller?
The major problem I can see from your code is that, you presented ChooserPopupViewController on GeneralChooser instance 1. Then when selecting a cell on ChooserPopupViewController, you are creating another instance 2 of GeneralChooser, and present it on ChooserPopupViewController while it is being dismissed. So I am pretty sure you are seeing the original GeneralChooser instance 1 after you dismiss the ChooserPopupViewController, but you passed the idSet and nameSet to the dismissed GeneralChooser instance 2. That is not how you should be passing info back from a presented view controller. Check this tutorial for different ways you can do it (specially sections with properties, delegation or closures)
https://learnappmaking.com/pass-data-view-controllers-swift-how-to/
You need to use a delegate to pass back the information. what you're actually doing is creating an entirely new instantiation of GeneralChooser and setting the variables there. Which is why you're not seeing the changes being made on the original GeneralChooser.
What you should be doing is:
1) creating a protocol to act as a delegate
2) Add a delegate method to the protocol that GeneralChooser would need to implement
3) Add a delegate variable within ChooserPopupViewcontroller and have GeneralChooser set it to "self" prior to presenting "ChooserPopupViewController.
4) Have ChooserPopupViewController call the protocol method from the delegate variable, in order to pass back the information to GeneralChooser. Then update the GeneralChooser button title, prior to dimissing the ChooserPopupViewController.
Hope this helps!
The presenting ViewController seems to load, but the screen is actually not reloading. After I touch the screen the ViewController is visible.
This is my instantiateViewController code:
let v = self.storyboard?.instantiateViewController(withIdentifier: "BrowsingDaragaViewController") as! BrowsingDaragaViewController
self.present(v, animated: true, completion: nil)
In the BrowsingDaragaViewController is no additional code. The viewDidAppear() function is not called until I touch the screen.
I'm using Xcode 9 with Swift4.
edit:
presentation code is in tableview(didSelectedRow at indexPath) but when i but it in button action it works fine
If you have used any gesture recogniser then remove it and check if it causing the problem?
And if You are not using gesture recogniser then use below code in didSelectRowAtIndexPath
// Your stuff
DispatchQueue.main.async(execute: {
self.present(yourViewControllerHere, animated: true, completion: nil)
})
// Your stuff
OR
Still facing issue then make sure you tableView has single selection set in your XIB or StoryBoard.
Currently making an app in swift where all elements are added programatically. how can i add segue programmatically for the action of a given button to switch from one view to the next?
Instead of creating segues programmatically, you can use this inside button action to navigate to another view controller.
let viewController = MyViewController()
self.navigationController?.pushViewController(viewController, animated: true)
You can push to your next viewcontroller without using segue as well like.
#IBAction func btnClickNextVC(_ sender: Any) {
let objSecondVc = self.storyboard?.instantiateViewController(withIdentifier: "ViewController2") as? ViewController2
self.navigationController?.pushViewController(objSecondVc!, animated: true)
//or you can use
present(objSecondVc, animated: true, completion: nil)
}
Note: Don't forget to take navigationcontroller in storyboard.
and give storyboard identifier of your viewcontroller same as you pass here in the code withidentifier.
I am trying to create segues between my UIViewControllers but am having some difficulties with creating a segue from a UITableViewCell and a UIButton.
When I create a show detail segue through storyboard from a UITableViewCell to a UIViewController it works perfectly and I get the back button showing up. But when I try to create a show detail segue from a UIButton to a UIViewController it doesn't register the navigation stack and presents the screen modally without the back button.
How can I make a successful show detail segue from a UIButton to a viewcontroller? I am new to iOS and am having trouble determining why the UIButton segue doesn't behave the same as the UITableViewCell segue.
Thanks in advance for any help!
Dont connect a segue manually do it through button action.
This is assuming the viewController that has this button is the root view controller of the navigation controller
#IBAction func myButtonTapped(_ sender: Any) {
let vc = self.storyboard!.instantiateViewController(withIdentifier: "YOUR STORYBOARD IDENTIFIER GOE HERE")
self.show(vc, sender: self)
}
If you want to go to a tab of a tab bar controller you have to specify its index. I think you can set it in storyboard but i just go with 0 is on the left then they go up sequentially. so in the example below i want to go to the second tab of the tab bar controller with a modal transition. I assume you can use show here like example above I've just never done it.
let tbc = self.storyboard!.instantiateViewController(withIdentifier: "MyTabController") as! UITabBarController
tbc.selectedIndex = 1 // this is 2nd tab index.. start at 0
tbc.modalPresentationStyle = .overCurrentContext
tbc.modalTransitionStyle = .coverVertical
self.present(tbc, animated: true, completion: { finished in
// you can also do some completion stuff in here if you require it.
// self.view.removeFromSuperview()
// self.navigationController?.navigationBar.removeFromSuperview()
})
let vc = self.storyboard!.instantiateViewController(withIdentifier: "StoryBoardID")
self.navigationController?.pushViewController(vc, animated: true)