Here is my tableview controller. I declared my array of mapSearch in the class to pass.
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
tableView.deselectRowAtIndexPath(indexPath, animated: true)
let selectedMap = mapSearch[indexPath.row]
let mapviewDestination = MapViewController()
mapviewDestination.mapString = selectedMap
performSegueWithIdentifier("showDetails", sender: tableView)
}
Here is my mapViewController code.
var mapString : String?
The actual search.
var request = MKLocalSearchRequest()
request.naturalLanguageQuery = mapString
request.region = self.placeMap.region
var search:MKLocalSearch = MKLocalSearch.init(request: request)
search.startWithCompletionHandler {
(response:MKLocalSearchResponse!, error:NSError!) in
if !(error != nil) {
var placemarks:NSMutableArray = NSMutableArray()
for item in response.mapItems {
placemarks.addObject((item as MKMapItem).placemark)
}
self.placeMap.removeAnnotations(self.placeMap.annotations)
self.placeMap.showAnnotations(placemarks, animated: true)
} else {
}
}
My problem is that when I run the simulator it doesn't search based on the mapString.
Any ideas are welcome!
EDIT:
I have been trying this and no luck:
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
let cell = tableView.cellForRowAtIndexPath(indexPath)
tableView.deselectRowAtIndexPath(indexPath, animated: true)
let selectedMap = mapSearch[indexPath.row]
performSegueWithIdentifier("showDetails", sender: self)
}
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject!) {
if segue.identifier == "showDetails" {
var mapController = segue.destinationViewController as MapViewController
mapController.mapString = mapSearch
}
}
Are you sure mapviewDestination in your didSelectRowAtIndexPath isn't released and destroyed? You're assigning it a mapString but you're not doing anything with it.
The search is performed on a nil (or empty) string because you never actually set a mapString.
If you need to pass data between two view controllers using segues, you should be using UIViewController's prepareForSegue:sender: (you just re-implement it in your presenting view controller). It gives you a pointer to the presented view controller that you can configure just before it gets on screen.
Related
I am trying to convert an app from Objective-C to Swift but I can't find how to pass data between views using Swift. My Objective-C code is
UIStoryboard *storyBoard = [UIStoryboard storyboardWithName:#"Main" bundle:nil];
AnsViewController *ansViewController;
ansViewController = [storyBoard instantiateViewControllerWithIdentifier:#"ansView"];
ansViewController.num = theNum;
[self presentViewController:ansViewController animated:YES completion:nil];
What that is doing is it basically takes the variable, theNum, and passes it to the variable, num, on a different view controller. I know this may be an easy question but I am getting pretty confused with Swift so if someone could explain how they changed it to Swift that would be greatly appreciated!
Thanks
Let's assumed we stand at the firstView go to the DetailView and want passing data from firstView to Detailview. To do that with storyboard, at the firstView we will have a method:
override func prepareForSegue(segue: UIStoryboardSegue!, sender: AnyObject!) {
if (segue.identifier == "segueTest") {
//Checking identifier is crucial as there might be multiple
// segues attached to same view
var detailVC = segue!.destinationViewController as DetailViewController;
detailVC.toPass = textField.text
}
}
and then into the class of DetailView we declared a variable:
var toPass: String!
then you can use the variable toPass (of course you can change the type of the variable as you want, in this EX I just demo for string type).
class AnsViewController: UIViewController {
var theNum: Int
override func viewDidLoad() {
super.viewDidLoad()
println(theNum)
}
}
override func tableView(tableView: UITableView!, didSelectRowAtIndexPath indexPath: NSIndexPath!) {
let viewController = self.storyboard.instantiateViewControllerWithIdentifier("ansView") as AnsViewController
viewController.num = theNum
self.presentViewController(viewController, animated: true, completion: nil)
}
To pass string or any data from one controller to another in swift.
Follows below steps:
1) Create property in child controller as var abc:string!
2) Create object of childcontroller
let storyboard:UIStoryboard()
let viewController: childcontroller = storyboard.instantiateViewControllerWithIdentifier("childcontroller") as! childcontroller
viewController.abc = "hello";
self.navigationController.pushviewController(Controller:viewController animated:true CompletionHandler:nil)
Using tableview,
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath)
{
let ClubProfileView = self.storyboard?.instantiateViewController(withIdentifier: "CBClubProfileViewController") as! CBClubProfileViewController
let TempCulubDic:NSDictionary =
((ClubsListbyDateDic.object(forKey:((ClubsListbyDateDic.allKeys as! [String]).sorted(by: <)as NSArray).object(at: indexPath.section) as! String) as! NSArray).object(at: indexPath.row))as! NSDictionary
let ClubId:String=(TempCulubDic.value(forKey: "club_id") as? String)!
let CheckIndate:String=(TempCulubDic.value(forKey: "chekin_date") as? String)!
ClubProfileView.ClubID=ClubId
ClubProfileView.CheckInDate = CheckIndate
// self.tabBarController?.tabBar.isHidden=true
ClubProfileView.hidesBottomBarWhenPushed = true
self.navigationController?.pushViewController(ClubProfileView, animated: true)
}
#IBAction func nextbtnpreesd(_ sender: Any) {
let mystring = "2"
performSegue(withIdentifier: "MusicVC", sender: mystring)
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if let destination = segue.destination as? MusicVC{
if let song = sender as? String{
destination.strlablevalie = song
}
}
}
//in MusicVC create string like:
var strlablevalie:String!
To pass string or any data from one controller to another in swift.
Follows below steps:
1) Create variable of type which you want like (String,Int)
var test : String!
2) Create object of childcontroller
let vc = self.storyboard?.instantiateViewController(withIdentifier: "Here identifier of your VC") as! (Here Name Of Your Controller)
vc.test = "Hello" (Or any data which you want to pass)
self.navigationController?.pushViewController(VC, animated: true)
That should solve your problem
Note: if we are using storyboard
Step 1: Master controller :
// table row which row was selected
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath!) {
tableView.deselectRowAtIndexPath(indexPath, animated: true)
println("You selected cell #\(indexPath.row)!")
nextScreenRow = indexPath.row
// get to the next screen
self.performSegueWithIdentifier("dashboard_static_screen_segue", sender: self)
}
and then;
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject!) {
if (segue.identifier == "dashboard_static_screen_segue") {
var detailController = segue.destinationViewController as StaticScreens;
detailController.screenNumber = nextScreenRow
}
}// end prepareForSegue
step 2: Detail controller (StaticScreen)
// set variable into your detail controller
var screenNumber: NSInteger?
println("selected row \(screenNumber!)")
I tried many ways but still can't go to another ViewController. This is my storyboard.
and This is my code.
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
if let cell = tableView.dequeueReusableCell(withIdentifier: "cell") as? HistoryMainTableViewCell {
listItems.sort() { $0.seq > $1.seq }
let history = listItems[indexPath.row]
cell.setValue(history: history)
return cell
} else {
return HistoryMainTableViewCell()
}
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "vcProductHistory" {
if let productHistoryPage = segue.destination as? HistoryProductViewController {
if let IndexPath = tableViewHistory.indexPathForSelectedRow {
let historyArray = listItems[IndexPath.row]
productHistoryPage.history = historyArray
}
}
}
}
Try this code:(Untested Code)
Note: Below code will trigger prepare segue. When, tableViewCell selected.Let me know the code works...
func tableView(_ tableView: UITableView, didSelectRowAtIndexPath indexPath: IndexPath) {
// let indexPath = myTableView!.indexPathForSelectedRow
// let currentCell = tableView.cellForRow(at: indexPath)! as UITableViewCell
//sendSelectedData = (currentCell.textLabel?.text)! as String as NSString
performSegue(withIdentifier: "vcProductHistory", sender: self)
}
Based on your answer to my initial comment: dragging the line from initial vc to destination vc tells your vc about the segue, preparing for segue tells your vc what do when the segue is triggered, but you still need to let your vc know when it should perform the segue. try this:
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
guard let cell = tableView.dequeueReusableCell(withIdentifier: "cell") as? HistoryMainTableViewCell else { return HistoryMainTableViewCell() }
let history = listItems[indexPath.row]
cell.setValue(history: history)
return cell
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "vcProductHistory" {
guard let productHistoryPage = segue.destination as? HistoryProductViewController, let indexPath = sender as? IndexPath else { return }
let historyArray = listItems[indexPath.row]
productHistoryPage.history = historyArray
}
}
func tableView(_ tableView: UITableView, didSelectRowAtIndexPath indexPath: IndexPath) {
performSegue(withIdentifier: "vcProductHistory", sender: indexPath)
}
I think this is a little bit neater using the guard syntax. Also, it uses sender as the indexPath, not using tableView.indexPathForSelectedRow.
Also, I noticed you have this in your cellForRowAtIndexPath:
listItems.sort() { $0.seq > $1.seq }
Your sorting your data source in cellForRowAtIndexPath. You can't do that there because that function gets called for each row individually. You need to do the sorting before that is called, somewhere like viewWillAppear. Also, to sort an array in place you need to remove the () after sort. try this.
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
listItems.sort { $0.0.seq > $0.1.seq }
tableView.reloadData()
}
I think you should connect to UINavigationController , not to HistoryProductionViewController
Because your MainViewController's segue connected to Navigation Controller
I guess in func prepareForSegue segue.identifier == "vcProductHistory" it works but, if let productHistoryPage = segue.destination as? HistoryProductViewController not works
because your destination ViewController's class is NavigationController
you should call
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "vcProductHistory" {
if let naviVC = segue.destination as? UINavigationController {
if let productHistoryPage = naviVC.topViewController as? HistoryProductViewController {
if let IndexPath = tableViewHistory.indexPathForSelectedRow {
let historyArray = listItems[IndexPath.row]
productHistoryPage.history = historyArray
}
}
}
}
}
Your solution - as you described it - should work.
The most work here can be done directly in the Storyboard.
Select the prototype cell in the TableView, drag the Segue to the NavigationController and select presentModally.
This should work without further magic.
When selecting the TableViewCell the NavigationController should be presented. There is also no code needed at this point (except for the population of the TableView).
If not - you maybe misconfigured your TableView by setting the selection to No Selection:
or you set User Interaction Enabled to false somewhere.
I am trying to create a similar tableview like the one below:
https://github.com/justinmfischer/SwiftyExpandingCells
I already have a complete tableview with contents, including a filtering system. The current problem is that I want to implement the same functionality to expand cells and show detailed information, without using storyboard.
The sample code (link) uses a storyboard segue, but I want to get rid of that so that it can basically do the same thing through code. I removed the label for the background. Just want to get the title and a new controller first by clicking a tableview cell.
DetailViewController
class DetailVC: UIViewController {
var brand: Brand?
override func viewDidLoad() {
super.viewDidLoad()
self.setup()
}
func setup() {
self.view.frame.origin.y = UIApplication.sharedApplication().statusBarFrame.size.height
if let brand = self.brand {
self.title = brand.name
}
}
}
This part needs to change
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
self.selectedCellFrame = tableView.convertRect(tableView.cellForRowAtIndexPath(indexPath)!.frame, toView: tableView.superview)
self.selectedBrand = BrandManager.sharedInstance.brands[indexPath.row]
self.performSegueWithIdentifier(.DetailVC , sender: nil)
}
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
switch segueIdentifierForSegue(segue) {
case .DetailVC:
let vc = segue.destinationViewController as! DetailVC
vc.brand = self.selectedBrand
self.navigationController?.delegate = self
}
}
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
self.selectedCellFrame = tableView.convertRect(tableView.cellForRowAtIndexPath(indexPath)!.frame, toView: tableView.superview)
self.selectedBrand = BrandManager.sharedInstance.brands[indexPath.row]
let vc = storyboard?.instantiateViewControllerWithIdentifier("DetailVc") as! DetailVC
vc.brand = self.selectedBrand
self.navigationController?.pushViewController(vc, animated: true)
}//// add the storyboardID for the detailsVc as "DetailVc"
I am new on iOS. Trying to implement a navigation bar for my UIWebView.
My TableView calls the webview when user clicks on any row. I load different local HTML files based on the index user clicked on. My webview is loading correctly but the problem is, I want to keep a navigation bar and < back button there, so that user can click it to come back to the tableview.
Here is the my ProductTableViewController:
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cellIdentifier = "ProductTableViewCell"
let cell = tableView.dequeueReusableCellWithIdentifier(cellIdentifier, forIndexPath: indexPath) as! ProductTableViewCell
let product = products[indexPath.row]
cell.name.text = product.name
cell.generic.text = product.generic
return cell
}
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
if let resultController = storyboard!.instantiateViewControllerWithIdentifier("webView") as? WebViewController {
presentViewController(resultController, animated: true, completion: nil)
}
//print(indexPath.row)
clickedOn = indexPath.row
}
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
let svc = segue.destinationViewController as! WebViewController;
//if clickedOn == 3{
svc.fileName = "Ace-VetBolus"
//}
}
And my WebViewController:
override func viewDidLoad() {
//print("got: "+self.fileName)
let url = NSBundle.mainBundle().URLForResource(self.fileName, withExtension:"html")
let request = NSURLRequest(URL: url!)
web1.loadRequest(request)
}
Please help me to write Swift code for this.
If you are presenting the WebViewController on ProductTableViewController, and want to have Bar Buttons on Navigation bar, I would suggest you to embed WebViewController in UINavigationController in IB.
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
let navc = segue.destinationViewController as! UINavigationController;
let svc = navc.topViewController as! WebViewController;
//if clickedOn == 3{
svc.fileName = "Ace-VetBolus"
//}
}
Will be the minor change in prepareForSegue method.
The back button on bar has to be added manually.
replace your didSelectRowAtIndexPath with following one, it works for me.
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
if let resultController = storyboard!.instantiateViewControllerWithIdentifier("webView") as? WebViewController {
let navController : UINavigationController = UINavigationController (rootViewController: resultController)
self.navigationController?.presentViewController(navController, animated: true, completion: nil)
}
//print(indexPath.row)
clickedOn = indexPath.row
}
I am trying to convert an app from Objective-C to Swift but I can't find how to pass data between views using Swift. My Objective-C code is
UIStoryboard *storyBoard = [UIStoryboard storyboardWithName:#"Main" bundle:nil];
AnsViewController *ansViewController;
ansViewController = [storyBoard instantiateViewControllerWithIdentifier:#"ansView"];
ansViewController.num = theNum;
[self presentViewController:ansViewController animated:YES completion:nil];
What that is doing is it basically takes the variable, theNum, and passes it to the variable, num, on a different view controller. I know this may be an easy question but I am getting pretty confused with Swift so if someone could explain how they changed it to Swift that would be greatly appreciated!
Thanks
Let's assumed we stand at the firstView go to the DetailView and want passing data from firstView to Detailview. To do that with storyboard, at the firstView we will have a method:
override func prepareForSegue(segue: UIStoryboardSegue!, sender: AnyObject!) {
if (segue.identifier == "segueTest") {
//Checking identifier is crucial as there might be multiple
// segues attached to same view
var detailVC = segue!.destinationViewController as DetailViewController;
detailVC.toPass = textField.text
}
}
and then into the class of DetailView we declared a variable:
var toPass: String!
then you can use the variable toPass (of course you can change the type of the variable as you want, in this EX I just demo for string type).
class AnsViewController: UIViewController {
var theNum: Int
override func viewDidLoad() {
super.viewDidLoad()
println(theNum)
}
}
override func tableView(tableView: UITableView!, didSelectRowAtIndexPath indexPath: NSIndexPath!) {
let viewController = self.storyboard.instantiateViewControllerWithIdentifier("ansView") as AnsViewController
viewController.num = theNum
self.presentViewController(viewController, animated: true, completion: nil)
}
To pass string or any data from one controller to another in swift.
Follows below steps:
1) Create property in child controller as var abc:string!
2) Create object of childcontroller
let storyboard:UIStoryboard()
let viewController: childcontroller = storyboard.instantiateViewControllerWithIdentifier("childcontroller") as! childcontroller
viewController.abc = "hello";
self.navigationController.pushviewController(Controller:viewController animated:true CompletionHandler:nil)
Using tableview,
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath)
{
let ClubProfileView = self.storyboard?.instantiateViewController(withIdentifier: "CBClubProfileViewController") as! CBClubProfileViewController
let TempCulubDic:NSDictionary =
((ClubsListbyDateDic.object(forKey:((ClubsListbyDateDic.allKeys as! [String]).sorted(by: <)as NSArray).object(at: indexPath.section) as! String) as! NSArray).object(at: indexPath.row))as! NSDictionary
let ClubId:String=(TempCulubDic.value(forKey: "club_id") as? String)!
let CheckIndate:String=(TempCulubDic.value(forKey: "chekin_date") as? String)!
ClubProfileView.ClubID=ClubId
ClubProfileView.CheckInDate = CheckIndate
// self.tabBarController?.tabBar.isHidden=true
ClubProfileView.hidesBottomBarWhenPushed = true
self.navigationController?.pushViewController(ClubProfileView, animated: true)
}
#IBAction func nextbtnpreesd(_ sender: Any) {
let mystring = "2"
performSegue(withIdentifier: "MusicVC", sender: mystring)
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if let destination = segue.destination as? MusicVC{
if let song = sender as? String{
destination.strlablevalie = song
}
}
}
//in MusicVC create string like:
var strlablevalie:String!
To pass string or any data from one controller to another in swift.
Follows below steps:
1) Create variable of type which you want like (String,Int)
var test : String!
2) Create object of childcontroller
let vc = self.storyboard?.instantiateViewController(withIdentifier: "Here identifier of your VC") as! (Here Name Of Your Controller)
vc.test = "Hello" (Or any data which you want to pass)
self.navigationController?.pushViewController(VC, animated: true)
That should solve your problem
Note: if we are using storyboard
Step 1: Master controller :
// table row which row was selected
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath!) {
tableView.deselectRowAtIndexPath(indexPath, animated: true)
println("You selected cell #\(indexPath.row)!")
nextScreenRow = indexPath.row
// get to the next screen
self.performSegueWithIdentifier("dashboard_static_screen_segue", sender: self)
}
and then;
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject!) {
if (segue.identifier == "dashboard_static_screen_segue") {
var detailController = segue.destinationViewController as StaticScreens;
detailController.screenNumber = nextScreenRow
}
}// end prepareForSegue
step 2: Detail controller (StaticScreen)
// set variable into your detail controller
var screenNumber: NSInteger?
println("selected row \(screenNumber!)")