Custom programmatic Segue without losing the values of controls - ios

I have a view1 with all controls created programmatically.
Once i click on a textbook i will go to view2 which has a tableview with search bar. User can select the tableviewcell value and the value should be displayed in the View1 in the textbook control.
I want to use this view2 as user control whenever a dropdown value to be selected. I am doing this right ?
The problem here is i am able to programmatically push the view controller to view2 . But after selecting the value in View2 i am not able to segue back to View1 with the value selected.
Is this scenario possible at all? If not what would be the solution.
NOTE:
I have 2 views view1 to view2.
View1 acts like a data input form.
View2 acts like a dropdown.
On click of button in View1 i am doing this
func ButtonPressed()
{ print("Button Pressed!!")
// This will create the new instance of the view controller.
let vc = UIStoryboard(name:"Main", bundle:nil).instantiateViewController(withIdentifier: "Storage") as! testClass
self.navigationController?.pushViewController(vc, animated:true)
}
Once view 2 is loaded then i am selecting a value in view2 and i want to segue back to the same instance of View1 so that i don't loose the values.
How can i achieve this??
Thanks in advance.

Related

Embed view controller inside container view based on selection from tableview

I'm trying to change the content of my container view based on what has been chosen from TableViewController and I'm out of an idea.
The structure in my storyboard looks like this:
Currently, my container view has embed segue with Table View and that's working great. Now after select something from Table View for example Map I want to display MapViewController inside container view and keep my header and footer. How can I do this?
First disable the segue form your container view to a DestinationViewController in your storyboard.
Now load your viewController object based on your previous tableViewController selection.
//this controller will be change on tableView selection make your own logic here.
let controller = storyboard!.instantiateViewController(withIdentifier: "Second")
addChildViewController(controller)
//Set this value false if you want to set Autolayout programmatically or set it true if you want to handle it with `frame`
controller.view.translatesAutoresizingMaskIntoConstraints = false
containerView.addSubview(controller.view)
controller.didMove(toParentViewController: self)

Better method to make pop up view

Let's assume we want to show some popup view on our screen. When user clicks a button our view pops up. In popup view we have Close button that hide/remove view itself.
Which method should I use to do that kind of thing:
1.) After click on the button add popup view to my main view. Close button removes popup view from superview.
or
2.) Make popup view with alpha = 0 and after click a button change alpha = 1. Close button changes alpha to 0.
When I was adding and removing view by multiple clicking in buttons I have noticed that application started to slow down.
If you know good solution/method to do that kind of things (show view/views in another view) it would be very usefull.
Actually UIKit treats very-low alpha elements as hidden but hidden elements still participate in autoresizing and other layout operations associated with the view hierarchy. To save your performance i'd recommend to use addSubview/removeFromSuperview methods when show/hide your view.
You can also use ViewControllers with standard animation like:
let presentedVC: ViewController = self.storyboard?.instantiateViewControllerWithIdentifier("presentedVC") as ViewController
presentedVC.modalPresentationStyle = .OverCurrentContext
presentedVC.view.backgroundColor = UIColor.clearColor()
self.presentViewController(presentedVC, animated: true, completion: nil)
and when you tap Close inside your presentedVC use:
self.dismiss(animated: true, completion: nil)

Switch to another ContainerView from inside a ContainerView

I made an iOS app with Xcode and Swift.
One ViewController contains two ContainerViews. The user can switch between them with SegmentedControl.
But, how can I switch to the other ContainerView without the SegmentedControl, e.g. with a button?
Actually I have this code:
let vc : AnyObject! = self.storyboard!.instantiateViewControllerWithIdentifier("login")
self.showViewController(vc as! UIViewController, sender: vc)
This opens the other ContainerView, but as single VC, not inside the parent ViewController.
What can I do to reach the other ContainerView shown in the parent VC?
You must have to set the IBoutlets from ContainerViews viewControllers too to call them separately using uibuttons action.
assign same class to both parentViewcontroller and containerView using identity inspector see image.
connect IBOutlet as well like parentViewController see image
Now can access from class using uibutton action.
Iterate through your parent view controllers's child's like
parentViewController.childViewControllers.indexOf(self)
you got the index, means you can get the Controller then you can show it

Pop up a View from a Button containing information but keeping the current View behind it: Swift implementation

I am finishing my career project and I want to make some UI improvements on the application I developed in Swift for iOS.
The application does not have any explicit explanation of how it works and I'd like to detail it in a pop-up View called from an information button in the Navigation Bar. I'd like some kind of View similar to the Notification Alerts, which pop-up in front of the current view and they disappear by pressing another button. I give you an example image:
I want this button to be available from any Tab View (I've got three views). Any idea of how to manage that?
Thank you very much!
First, go to IB and create a new View Controller. In the Attributes Inspector, click on the dropdown next to Size and click Freeform. Set the size to what you want. Then click Use Preferred Explicit size.
Control-drag from your info button to the new VC you created. Click on the segue, then go to the attributes inspector and change the segue type to 'Present As Popover.' Change the segue identifier to whatever you want. I named it popover.
Create a new class for your popover view and assign it to the VC you created. It doesn't need any code at the moment.
In your presenting VC (Tab Bar), add UIPopoverPresentationControllerDelegate to your class definition. Then add this function:
func adaptivePresentationStyleForPresentationController(controller: UIPresentationController) -> UIModalPresentationStyle {
return UIModalPresentationStyle.None
}
Next, in your Tab Bar VC, add this code to your prepareForSegue method:
if segue.identifier == "popover" {
let controller = segue.destinationViewController as! PopoverVC // whatever you named the class
controller.modalPresentationStyle = UIModalPresentationStyle.Popover
controller.popoverPresentationController!.delegate = self
}
That should do it.

Get a UIButton's tag from the UIButtons which are in the popover presented by this UIButton

The headline seems lengthy but what I'm trying to do is quite simple.
I have a couple of identical buttons lined in a row and set their tags to 0...n. Clicking on any of them (the 2nd for example) would bring up a popover view in which there are several buttons representing different options (A, B, C, D). What I want to do is to turn the 2nd Button's title to B if we click on option B.
The problem is that the popover view does not know which Button presented it, since all popoverViews are instances of the same UIViewController class. So I am thinking of distinguishing the n buttons by setting their tags to different values. However, I don't know how to get the UIButton's tag from a button inside the popover this UIButton presented.
Many thanks in advance!
This is how I will solve this in swift. I will declare a delegate in the popoverViewController and have a method e.g
protocol popOverViewControllerDelegate: class {
func popOverViewController(controller: PopOverViewController,
didSelectItem buttonTitle: String)
}
then I will add a target action to the UIButton in the popOver
button.addTarget(self, action: "selected:",forControlEvents:.TouchUpInside)
the select method will have sender passed to it
func selected(sender:UIButton){
delegate?.popOverViewController(self, didSelectItem: sender.currentTitle)
//dismiss viewcontroller
}
remember to declare
weak var delegate: popOverViewControllerDelegate!
now have the viewcontroller that called the popOver, subclass to this delegate and implement it's method. Whenever a button in the popOver is selected, this method will be called and the currentTitle of the method will be passed. You can now use that to set the currentTitle of the button that called the popOver.
In case you need further help with the last part, please ask.
I've fixed the problem by adding a property tagNumberso that after instantiating the popoverViewController's class, I set the instance's tagNumber to the sender's tag. Then I send the tagNumber back together with sender.currentTitle. This way, the presenter of the popover could know both the title and tag number of the UIButton.

Resources