0

Heloo all,

I am new to ios swift. I have one problem in my screen.

I have one screen with drop down list of option like : current bill,water bill., bike bill, loan bill, phone bill....so on

I have sone this. But what i need is, whenever i select any option from drop down list. I need to show some ui elements like label, text filed, text box, lie that dynamically for each each selction in drop list.

For example :

if i select water bill, then below my drop down, i need to show the two label ui with name , id.

if i select current bill , then below my drop down , i need to show one text filed with place holder ' enter current bill number'

So like wise when ever i select any option from drop down..i need to show some dynamic ui.

How can i achive that ??

ANY HELP WILL BE USE FULL

hybrid Dev
  • 529
  • 3
  • 14
  • 33
  • Can't you just simply hide all those views and if you click specific option just make visible elements for it? – Rajmund Zawiślak May 27 '17 at 08:01
  • @Raymond yes i too thought. But if i have 20 options in my drop down list..Then if i put 20 different ui in same screen ??.... then will it the good practice / standard to show to user ? – hybrid Dev May 27 '17 at 08:02
  • Hmm, I see. Another solution. Make nib file for each option and display it after selecting one. – Rajmund Zawiślak May 27 '17 at 08:06
  • use container view – Maddy May 27 '17 at 08:07
  • @Raymond oaky if i create some currentbill.xib,waterbill.xib, like on..Then in same screen at below drop down list i will have one common view...But how can i call that xib files , when i select any option from drop list.Then how can i show that xib in my ui screen – hybrid Dev May 27 '17 at 08:09
  • @Maddy container view?? can u explain it ..please – hybrid Dev May 27 '17 at 08:09
  • https://developer.apple.com/reference/foundation/bundle/1618147-loadnibnamed – Rajmund Zawiślak May 27 '17 at 08:14
  • @Raymond thanks, but i am new to ios. If you give me some code example or github that will be help full for me to understand – hybrid Dev May 27 '17 at 08:16
  • https://stackoverflow.com/questions/863321/how-to-load-a-uiview-using-a-nib-file-created-with-interface-builder , if you do not understand obj c, you can always convert this code in objc to swift converter. – Rajmund Zawiślak May 27 '17 at 08:21

2 Answers2

0

You can make use of container views to achieve that:

1- Add your drop down menu to any view controller you want.

2- Add a container view under that drop down menu and using auto layout make that container view fill the place that you want it to be switchable/dynamic.

3- Redo step 2 as many times as the items of the drop menu.

4- Create IBOutlets for each of your container views and and an IBAction for your drop down menu.

5- In the IBAction of your drop down menu, set isHidden property of one container view to false and set it to true for the others based on the user's selection from the drop down menu.

Here is a tutorial that explains that with a simple example.

Mo Abdul-Hameed
  • 6,030
  • 2
  • 23
  • 36
  • okay, which means that, i need to place more container on same screen like one above one.....And i need to make ibaction of drop down list . and i need to use some bool thing to show the container view based on my drop down selection..Right ?? – hybrid Dev May 27 '17 at 08:25
  • But its look like same think that we are doing like placing the uiview in same screen as one above right ?.. SO does it is the good practive to keep more container one by one above ?? – hybrid Dev May 27 '17 at 08:27
  • Yes, you'll have many container views above each other, and you'll control which one is visible from your drop down menu's `IBAction` based on the user's selection. Having many container views is not an issue, each container view will have a view controller embedded in it, so you'll manage everything related to each container view's view controller in a separate class. – Mo Abdul-Hameed May 27 '17 at 08:32
0

Just drag and drop viewcontrollers in your storyboard for your current bill,water bill., bike bill, loan bill, phone billetc.

And when ever you change the option from your drop down list use below code to add your required controller as child controller.

let currentBill  = self.storyboard?.instantiateViewController(withIdentifier: "currentBill") as! CurrentBillVC

// remove other views           
for k in 0..<self.parentView.subviews.count {
    self.parentView.subviews[k].removeFromSuperview()

}

self .addChildViewController(currentBill)

// Add view to your maincontroller i named it parent view in which you want to display other controller w.r.t your drop down list.

self.parentView .addSubview(currentBill.view) 

currentBill.didMove(toParentViewController: self)
Maddy
  • 1,660
  • 11
  • 24