how to pass data from one view controller to another and between the view controller there is a reveal view controller [duplicate] - swift

This question already has answers here:
Passing data between view controllers
(45 answers)
Closed 4 years ago.
Login to the page using email id and password and then i want to send data from login page to profile page, but my profile page is linked with the SWrevealviewcontroller as of to show side menu.
I am trying using default values.
Login View controller]2 form which i want to send data to profile view controller.
]4
Any suggestions what should i do?
Thank you!

You can make use of userDefaults, eg in LoginViewController save data to be used, as done below:
UserDefault.standard.setValue(name, forKey: "User_name")
UserDefault.standard.synchronize()
To use saved data on profile screen:
if let userName = UserDefault.standard.value(forKey: "User_name") as? String {
self.lblUserName.text = userName
}
Hope this helps out somehow.

One way to do this could just be using a global variable. A variable is made global if you put it above the class declaration. Example:
var globalInt = 7
class example....{
// blah blah blah, class code here
}
By doing this, if you change the variable in one screen, if another screen tries to access it after it has been modified, it will access the modified version. So for example if I were to change globalInt to 9 inside the class example, if I then accessed globalInt on another screen, it would be 9. Also, you can modify and access a global variable the same as if you were accessing any variable within the class.

Related

How to dynamically change view in Swift (e.g. for a settings page)?

I found a good settings page tutorial at https://www.simpleswiftguide.com/how-to-use-userdefaults-in-swiftui/ and implemented it into my app. However, the settings page is completely static and can't be changed based on the user's choices. Often a program needs to have constraints where the allowed choices for field B might change based on the user's choice of field A. How do I add an on-click listener to the settings which can call the UIViewController to somehow change the structure of the view after it has already loaded? I am using UIHostingController to wrap the SwiftUI View which is declared as a struct instead of a class. If I try to access the view via self.view I don't see any relevant functions that could give me access into the inner workings or fields of the view.
It doesn't look like there's an easy way to do this in Swift like there is in Android. I'll resort to using a sequence of UIAlertController each with multiple choices. Unlike the View struct with "var body" protocol, the alerts can be built programmatically depending on what the user has already set.
Some if else in the View
if $userSettings.username.wrappedValue == "username"{
SomeView()
}else{
SomeOtherView()
}

swift update textview after typing new text

I'm making an app in swift 3
I have a TableViewController, when I click on a row it opens a ViewController in which there is a TextView. When I click on it, I am able to edit it, but the changes are not "saved". when I go back to my list and re-click on the same row, the text is back to "Default", I've been on this problem all day, and I don't know how to solve it, I've tried solution from stackoverflow but only give solution to change the text colours.
So how can I do that ?
Based on what you've said so far, I can only assume that you are not storing the new text that's been entered into your UITextView.
The reason that the changes are not being saved is that every time you "open" a new ViewController by tapping on a TableViewCell, it is a brand new instance of that ViewController. That means that it has no input as far as what should be displayed in the TextView, and therefore displays the default text.
But how do I fix it?
This is going to require that you save your TextView's text every time that you leave or dismiss the ViewController. This is because when you leave the ViewController, it is being released from memory. This means that anything that was written there no longer exists. I would suggest storing the text in an array of strings on your TableViewController, which should allow for the text to persist over the life cycle of the app.
The text disappears when I close the app!
This is something that will require data persistence, and for that I would suggest reading up on how to use Core Data to store and persist data across multiple app life cycles.
There are many ways to achieve that, the idea is to set the text of the text view in its view controller's initialization.
So, you need a place to save the data entered by the user, and then read that data again in the view controller's initialization.
One way to do that is using a singleton class to save the data, like this for example:
class SharedData {
static let instance = SharedData()
var textEnteredByUser: String?
}
Now, when the user enters text, save that text in the string we have in 'SharedData' like this:
SharedData.instance.textEnteredByUser = textView.text!
Now, we have the data entered by the user saved, we need to set the data in the text view before it appears in the screen, and this can be achieved inside viewDidLoad method of your view controller like this:
override func viewDidLoad() {
super.viewDidLoad()
textView.text = SharedData.instance.textEnteredByUser
}
You can also save your data in user defaults instead of the singleton class like this:
UserDefaults.standard.set(textView.text!, forKey: "textEnteredByUser")
And then retrieve the saved text this way:
UserDefaults.standard.string(forKey: "textEnteredByUser")
Note: The singleton solution keeps the data shared between different view controllers, but does not persist data after closing the application, and this is what user default does.

Dynamically Updating A Tab Bar Item In the More ViewController Table

OK. I haven't actually seen how to do this anywhere. It may be a question of "You can't get there from here." or "Holy ##$! That is such a disgusting hack it should be taken behind the woodshed and shot!".
I have a tabbed Swift 3 iOS app that will dynamically update the Tab Bar images of selected pages as the page state changes.
I do that sort of like this:
if let navController = self.navigationController as? MyNavController {
navController.tabBarItem.image = navController.tabBarImage
navController.tabBarItem.selectedImage = navController.tabBarImage
}
The tabBarImage is actually a calculated property. This snippet is called within a UI callback that updates when the state changes.
This works great.
When in the MoreViewController, though, not so great. Those images remain stubbornly static, no matter what I do.
I have done some exploration of the MoreViewController. I can get at the tableView and the cells, but that smells like the kind of hack that will get my app taken behind the woodshed by the Blue Meanies at App Review.
Is there a proper way to do this?
You can use Notification and pass the images within a Dictionary as a Notification object. Then you can get different tabBarImage with different key value at once.
OK. I figured out how to do it.
I was crawling back along the navigation controller path; which worked fine for items not in the more space.
I fixed it by crawling forward from the tab bar controller, instead:
self.tabBarController?.viewControllers?[MySelectionIndex].tabBarItem.image = self.tabBarImage
self.tabBarController?.viewControllers?[MySelectionIndex].tabBarItem.selectedImage = self.tabBarImage

How to make one page pass on a message to another?

My problem is different users will have access to different products. Say User 1 has access to 3 whereas User 2 has access to all 4.
Can I have something upon Login where if User 1 logs in, it will tell the MainView to only show 3 cells? Something like ViewDidLoad in the MainView to read what the Login Page echo-ed?
Can that be done?
Thanks.
Store a global variable in appdelegate like numberOfCells.
Assign value to it by the condition of your login.
Once your MainView is about to display like in viewWillAppear, get the value of numberOfCells from appdelegate and chage your UI.
You can get the appdelegate reference as follows.
SampleAppDelegate *appdelegate=(SampleAppDelegate*)[[UIApplication sharedApplication] delegate];
If you keep track of the user who has 'logged-it' you can configure your views based on that user/profile.
why don't you use NSUserDefaults??
You can save alot of field in it and will be accessible throughout your project.

State management in MVC 2

I want to show a Session Variable data on View.
How can i achieve it.
I am storing some data in Session varaibles and noew i want to display it on View please help me.
I am new Asp.Net MVC 2
Please suggest.
use the ViewData and pass it to the view
in your controller set up something like ViewData["namehere"] = value
then in your View you can call the value of Viewdata["name here"] - think of the ViewData as a bag that you can put properties and their values into and not need to explicitly pass the viewdata to the view for the view to use it
view data is just like session in asp.net so in your controller you can create more than one view data then create you view ,if you want to create a partial control for the view it will alse see the result of the view data but do not forget to cast the result.