Swift Audio passing to another ViewController - swift

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "stopRecording" {
let playSoundsVC = segue.destination as! PlaySoundsViewController
let recordedAudioURL = sender as! URL
playSoundsVC.receivedAudio = recordedAudioURL
}
}
The question is that my Xcode generating error
Value of type 'PlaySoundsViewController' has no member 'receivedAudio'

For this to work the destinationVC should look like this
class PlaySoundsViewController:UIViewController {
var receivedAudio:URL?
.....
}

Related

Extract a String Variable from a Void function

here is my problem :
I would like to recover the variable jan from the Launch App() function and insert it in the override above instead of "Hello there".
override func prepare(for segue: UIStoryboardSegue, sender: Any?){
let destVC : troisViewController = segue.destination as! troisViewController
destVC.dataFromFirst = "Hello there"
}
func launchApp(decodedURL: String) -> Void {
if presentedViewController != nil{
return
}
let jan: String = "\(decodedURL)"
print(jan)
self.performSegue(withIdentifier: "troissegue", sender: self)
}
The problem is the decoded URL is a barcode obtained by using the camera of my phone, a solution of type : destVC.dataFromFirst = launchApp() does not work...
Does Anyone has a similar issue ?
Thank you in advance
A simple solution is to pass the string as sender parameter
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "troissegue" {
let destVC = segue.destination as! troisViewController
destVC.dataFromFirst = sender as! String
}
}
func launchApp(decodedURL: String) -> Void {
if presentedViewController != nil { return }
self.performSegue(withIdentifier: "troissegue", sender: decodedURL)
}

Pass data to variable in swift

I want to assign button tag to variable. Then I trying send this data to SecondViewController and assign it to var.
#IBAction func goToProducts(_ sender: UIButton) {
performSegue(withIdentifier: TextStrings.ProfileAluVC.goToProducts, sender: self)
categoryNumber = sender.tag }
Pass data to ProductsViewController
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "goToProducts" {
let destinationController = segue.destination as! ProductsViewController
destinationController.categoryNumb = categoryNumber
}
}
At ProductsViewController I create var but there isn't this data. I print this variable and I get this:
categoryNumber: 373
categoryNumb: Optional(24)
Can you tell me haw can I do this?
You need to assign it first before performSegue
categoryNumber = sender.tag
performSegue(withIdentifier: TextStrings.ProfileAluVC.goToProducts, sender: self)
OR use sender
performSegue(withIdentifier: TextStrings.ProfileAluVC.goToProducts, sender: sender.tag)
Pass data to ProductsViewController
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "goToProducts" {
let destinationController = segue.destination as! ProductsViewController
destinationController.categoryNumb = sender as! Int
}
}

Cannot find `ViewController` in prepare for segue

I´m trying to pass data from one ViewController to another. But when I try to set the destinationVC Xcode doesn't recognize the ViewController. I get no autocomplete for the DataRecieverViewController I get autocomplete for the other ViewCOntrollers When I write it in manually, I get no errors, but no data is passed.
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == passDataSegue {
let destination = segue.destination as! DataRecieverViewController //Xcode doesn't recognize this VC. I get no autocomplete.
destination.data1 = data1
}
}
override func prepareForSegue(segue: UIStoryboardSegue!, sender: AnyObject!) {
if (segue.identifier == "identfiername")
{
let destination = segue.destination as! DataRecieverViewController
destination.data1 = data1
}
}
for you the identifier name is passDataSegue, When you work with segues please cross check the seguename and viewcontroller name once more to avoid erorrs.
About autocomplete try to save the file DataRecieverViewController and build your project it is a common bug of Xcode.
About data pass
Are you sure you are getting to the right instance of DataRecieverViewController, not the general class?
Is it embedded in a NavigationController ot UITabBarController ?
if Yes you have to get the right instance.
If embedded in UITabBarController
override func prepareForSegue(segue: UIStoryboardSegue!, sender: AnyObject!) {
if (segue.identifier == passDataSegue)
{
let tabBar = segue.destination as! UITabBarController
let controller = tabBar.viewControllers?.first(where: { $0 is DataRecieverViewController }) as? DataRecieverViewController
controller.data1 = data1
}
In case of UINavigationController
override func prepareForSegue(segue: UIStoryboardSegue!, sender: AnyObject!) {
if (segue.identifier == passDataSegue)
{
let NavBar = segue.destination as! UINavigationController
let NavC = NavBar.viewControllers?.first(where: { $0 is DataRecieverViewController }) as? DataRecieverViewController
let controller = NavC.rootViewController as? DataRecieverViewController
controller.data1 = data1
}

Add two guard let in a prepare for segue function

I'm trying to build my first app with three tableViews which are hierarchical. The middle VC has two guard let in one prepare for segue function.
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
guard let destination = segue.destination as? AddMemberViewController else {
return
}
destination.club = club
guard let Destination = segue.destination as? TransactionViewController, let selectedRow = self.tableViewMember.indexPathForSelectedRow?.row else {
return
}
Destination.member = members[selectedRow]
}
That's how it looks like. Can I fix this, that both func get used by my app, because it just uses the one on the top.
The problem is if you want to segue to TransactionViewController the function already returns because segue.destination is not AddMemberViewController.
Instead you should give your segues different identifiers and ask for them in prepareForSegue. Something like this:
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "AddMemberVCSegue" {
guard let destination = segue.destination as? AddMemberViewController else {
return
}
destination.club = club
}
if segue.identifier == "TransactionVCSegue" {
guard let Destination = segue.destination as? TransactionViewController, let selectedRow = self.tableViewMember.indexPathForSelectedRow?.row else {
return
}
Destination.member = members[selectedRow]
}
}
Just replace guards with if lets:
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if let destination = segue.destination as? AddMemberViewController {
destination.club = club
} else if let destination = segue.destination as? TransactionViewController, let selectedRow = self.tableViewMember.indexPathForSelectedRow?.row {
destination.member = members[selectedRow]
}
}
prepare(for is called for one specific segue, so executing both guard let after another is unnecessarily expensive.
Usually you are checking the identifier of the segue with a switch statement, replace the literal identifiers with your real values
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
switch segue.identifier {
case "memberSegue":
let destination = segue.destination as! AddMemberViewController
destination.club = club
case "transactionSegue":
guard let selectedRow = self.tableViewMember.indexPathForSelectedRow?.row else { return }
let destination = segue.destination as! TransactionViewController
destination.member = members[selectedRow]
default: break
}
}

what is the collectionView form of indexPathForSelectedRow?

I'm attempting to create a variable "indexx" that will be segued to my next tableController. indexx will hold the indexPath of the selected cell from my collectionView, however, I am unsure which property to use. I am vastly more familiar with tableController properties so I'm having difficulty finding a successful solution to this piece of code. Would you happen to know my error?
As a quick note - foodcv is my collectionView and numberr is the variable in the second destination controller.
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if let destViewController = segue.destination as? foodtable {
let indexx = self.foodcv.indexPath(for: foodcell)
destViewController.numberr = indexx
}
}
let numberr : IndexPath = []
Maybe it's too late, but here you go:
Swift 4
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if let destViewController = segue.destination as? foodtable {
let indexPaths : NSArray = foodcv.indexPathsForSelectedItems! as NSArray
let indexx : IndexPath = indexPaths[0] as! IndexPath
destViewController.numberr = indexx[selectedindexPath.row]
}
}
Use this modified implementation,
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if let destViewController = segue.destination as? foodtable {
let indexx = self.foodcv.indexPathForCell(sender as! UICollectionViewCell)
destViewController.numberr = indexx
}
}