I'm using Xcode Swift 3.0, I have 5 view controllers, each controller have several textField. Once the information finish filled, press nextButton to next view controller. For example, finish fill in all textField in view controller 1, click nextButton and I will segue it to view controller 2, and so on.....
I want to get view controller 1,2,3,4 entered textField information only from view controller 5.
How am I gonna to do that without using Segue to pass data from vc1 to vc2, and from vc2 to vc3 and so on?
Please advice.
TempData can be used for passing value from Controller to View and also from Controller to Controller.
Find the example:
public class FirstController : Controller
{
// GET: First
public ActionResult Index()
{
TempData["Message"] = "Hello MVC!";
return new RedirectResult(#"~\Second\");
}
}
public class SecondController : Controller
{
// GET: Second
public ActionResult Index()
{
return View();
}
}
How am I gonna to do that without using Segue to pass data from vc1 to vc2, and from vc2 to vc3 and so on?
No matter whether you're using Swift or Objective-C, the right solution here is to use a data model. Each view controller gets a reference to a common data model from the object that creates it, and the view controller uses the model to populate its views. The view controller updates the model as the user makes changes. Since the other view controllers also refer to the same model, they all automatically get the user's changes.
Related
Hi have a scenario where in i have 3 views in the first view i have some textbox and then next button on click of next button the second view will be shown and the second view will also have some textbox and next button the third view is the final view which has few textboxes and submit/finish button. On click of submit/finish button all the data from view1,view2 and view3 should be saved to database. How to achieved it using ASP.NET MVC
You can use temp-data and their keep method to persist data in subsequent request.
Just keep all required field data into temp-data and get all this in one object(temp-data) and save them
Here is an eg. for it:
public ActionResult Add(Model model)
{
if(ModelState.IsValid)
{
db.Model.Add(model);
db.SaveChanges();
return RedirectToAction("Add2");
}
return View(model);
}
it redirects to the second view or a partial view, and the same with the 2nd to the 3rd. Hope it helps.
if you want to pass data from one view to another view then you can use TempData
ex.
TempData["Key"] = "test";
and can get value from tempdata like
#{
var abc = TempData["Key"];
}
Note: You can get tempdata value to next action after that it will be destroy.
I have three view controllers. Now I am in second view controller. I want to dismiss my current view controller and open third view controller. How can I do it? I can dismiss third view controller as well as second view controller by using the following code.
self.presentingViewController?.presentingViewController?.dismissViewControllerAnimated(false, completion: nil)
My problem here is when I use this function screen flickering. That means the second view controller showing and dismissed. So how can I do it?
I want to dissmiss the currentview cotroller and open new view
controller
It is possible in Android. So I hope it is possible in ios too. How can I achieve this. Please some one help me.
Edit 1:
I try to dissmiss the current view controller and open new view controller using the following code. Then my second and third both are closed.
self.dismissViewControllerAnimated(false) {
// go back to MainMenuView as the eyes of the user
presentingViewController.dismissViewControllerAnimated(false, completion: nil)
}
I found the solution
I change all the view controller to the child of navigation controller. And remove the history from the third view controller like the following
let tempVCA = self.navigationController!.viewControllers
for tempVC: UIViewController in tempVCA {
if (tempVC is SECONDVC) {
tempVC.removeFromParentViewController()
}
}
I am using this piece of code from the Third view controller's viewdidload func
I have a view controller which has embedded a navigation controller.
From this view controller I am being able to change the title of the navigation bar with navigationItem.title = "title".
Furthermore I have another tableview controller and I want to update the tile of the navigation controller also from here, this tableview controller acts as a Slide Out Menu for the first view controller.
Navigation controller and tableview controller are not connected directly but I use a library called SWRevealViewController to create and connect the slide out menu with first view controller.
I have tried these codes:ViewController().navigationItem.title = "secondTitle"
I have also tried to put the process of changing the title in first controller in function and create an instance like :ViewController().updateNavTitle(), also tried to crete a segues but without any result.
Create a String variable in your first class and access this property in your Slide Out controller and and update it. Now in your first controller's in viewWillAppear method update the title like below.
override func viewWillAppear(animated: Bool) {
super.viewWillAppear(animated)
if self.updatedTitle != nil {
self.navigationItem.title = self.updatedTitle//Create this String variable.
}
}
I currently have a navigation controller that contains one view controller and two table view controllers. When the user navigates to the second table view controller, they can press an '+' bar button item which modally presents another table view with a static cell that contains a text field. The user can enter text and press 'Done' to save it. When my delegate method is called, the view controller is dismissed and that user-entered string is appended to the array in the second view controller. The table view then displays that user-entered text in a table cell. The array has been passed up the stack via the prepareForSegue method. This is currently working because I can see the strings in the table view that the array was instantiated with in the first view controller.
My problem occurs when the user navigates back down the stack to any view controller prior to the second table view, which contains the user-entered strings displayed in cells. I have successfully been able to pass the data to the previous view controller, obviously, but after much research and testing, I am unable to successfully get the user-entered string to persist. When I navigate to previous view controllers and then back go the second table view, the user-entered data is gone, and the array contains only the initial data.
I understand that arrays in Swift are structures and the values are copied when passed up the stack. This makes sense as to why the user-entered data disappears when I navigate to a previous view controller. The previous view controller does not actually contain that user-entered data. Does this mean that I need to append the string to the array in the initial view controller instead of the array in the second table view controller? If that is the case, I am unsure of how to access the initial view controller from the third view controller.
All of the documents and tutorials that I have accessed are based on passing the data from the current view controller back to the previous view controller, which is the delegate that then dismisses the view controller that accepted the input.
How can I pass the user-entered string from the last view controller back to the initial view controller?
Actually, there are a lot of messaging mechanisms in Objective C, and in addition to Phillip's answer I can suggest you to use NSNotificationCenter by using which your can post notifications and catch them where needed. Posted notification can have custom object, string in your case, and name, for which you should add observer in order to receive that notification.
Registering for particular notification:
[[NSNotificationCenter defaultCenter] addObserver:{object_to_handle_notification} selector:#selector(notificationReceived:) name:#"my_notification" object:nil];
Posting notification:
[[NSNotificationCenter defaultCenter] postNotificationName:#"my_notification" object:userEnteredString];
Handling notification:
- (void)notificationReceived:(NSNotification *)notification
{
NSString *userEnteredString = notification.object;
//do whatever you want with the user entered string
}
Also, you should remove observer for the notification when you are done.
Hope this was helpful. Good Luck!
Create a class that is responsible for maintaining your data model. You can either make it a singleton or have an object of the class available through your application delegate (or some other option). When a controller wants information to be available to other parts of the app, it sends it to the data model for update. When a controller wants to display information, it gets it from the data model.
No more data-dependencies between controllers that have to figure out what to exchange.
What you can do is pass the firstViewController as a parameter to the ThirdViewController in the prepareForSegue method from the SecondViewController.
FirstViewController:
class FirstViewController: UIViewController {
var a:String!
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
var destination=segue.destinationViewController as? SecondViewController
destination?.firstViewCont=self
}
}
SecondViewController:
class SecondViewController: UIViewController {
var firstViewCont:FirstViewController!
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
var destination=segue.destinationViewController as? ThirdViewController
destination?.FViewCont=self.firstViewCont
}
}
ThirdViewController:
class ThirdViewController: UIViewController {
var FViewCont:FirstViewController!
#IBAction func textFieldEdited(sender: UITextField) {
FViewCont.a!=sender.text
}
}
This is one of the most impractical ways one would do it. I suggest that you learn about Navigation View Controllers. They work like a charm when doing something like this.
I am using the navigation controller to go back from one view to previous view using the code below.
ChildViewController.swift:
self.navigationController.popViewControllerAnimated(true)
I need a way to detect that the navigation controller went to the previous view in the actual previous view like below.
ParentViewController.swift:
func backWasPressed(viewControllerIdentifier: String!) {
// if back was pressed from this view controller and not from any other view
if viewControllerIdentifier == "ChildViewController" {
// do stuff here
}
}
Is there anyway to do this?
Take a look at UINavigationControllerDelegate
You don't need to know this. You may think you do, but you don't. This entire proposed architecture is specious:
func backWasPressed(viewControllerIdentifier: String!) {
// if back was pressed from this view controller and not from any other view
if viewControllerIdentifier == "ChildViewController" {
// do stuff here
}
}
If a pushed view controller has some info to communicate to a view controller further down the stack, that is the job of the pushed view controller when it is popped. It knows it is being popped, and it knows how to access the other view controller (and you can use a delegate architecture if there's any doubt about that), so the problem is properly solved in that way. It's exactly the same as when a presented view controller needs to communicate back to its presenter at dismissal time.