Right now I have a picker view that shows up when you press a label, and after you have selected anything from the picker view and hit done it will hide and the label will change to the value you selected.
But I want to implement another picker view, and that picker view will only display based on the value you selected in the first picker view.
So more or less like a relational dropdown that you can find on almost every website.
I want to be able to select category and subcategory. But its only about 10% of the categories that has an subcategory thats why I want to build it this way.
So my question now is if this will be against apples dev/design guidelines?
Or does anybody else have a good solution on how to display a category/subcategory selector for a search form in an iOS app?
Thanks in advance,
Just a rough sketch:
Say you pick a category from you PickerView. Your PickerView should then notify you parent ViewController that the user has picked a Category. The most convenient way to do this is to have a Delegate method, like:
self.delegate.userPickedCategory(pickedCategory: Category)
Now, I assume you Category object contains an array of subcategories:
class Category: NSObject {
var title: NSString!
var subCategories: NSMutableArray!
//some variable containing categories content
}
Say you named the button to your Sub Category Menu subCategoryButton. You should always set hidden = true or at least userInteractionEnabled = false, because you don't know whether the picked category has a sub.
If your parent ViewController receives the delegate method that your user picked a category, you might do:
func userPickedCategory(pickedCategory: Category) {
if pickedCategory.subcategories != nil || pickedCategory.subCategories.count != 0 {
//you now know the picked category has a subCategory
//so allow the user to pick that subCategory by enabling this button
self.subCategoryButton.hidden = false
self.subCategoryButton.userInteractionEnabled = true
}
Then, you need to make sure the subCategoryButton shows another picker view containing the subCategories of your picked Category
Check the following open source implementation of what you want.
kxmenu
Related
I am currently developing a subscription app.
I currently have 6 View Controllers to which have different options on.
For example - Name - Amount - Date etc.
Once the user completes these, I want it to create a new view controller with this information on it.
So once they go back onto the main screen, they will be able to swipe left and it will show their subscription.
All I want to know is, is there a way I can create a new view controller with their information on it once they press a complete button on the Date page?
Transferring data to a view controller is the easy part. I am more wondering on how I can create a new view controller when they try and add another Subscription, if that makes sense.
If not is there a way I can do this?
You have two options that I can think of. I am sure there are more, but these may help.
You could create a global instance of a struct that is your "subscription to add". You could then update the values for each property of this struct (e.g. name, amount, date, etc.) after each VC is dismissed
You can pass the data your user is generating to each VC through prepareForSegue. This is slightly redundant so I would recommend doing the first method. For the record, this segue method would look something like this:
VC 1 Dismissed: Pass Name to VC2
VC 2 Dismissed: Pass Name and Amount to VC3
VC 3 Dismissed: Pass Name, Amount, and Date to VC4 etc......
Once you have all of the information, you can show the last VC and just set the labels and such to the values of your struct, or the values you passed through the segue.
Edit: Further Information on Option 1
So if you made a struct like this:
struct UserToAdd {
static var name: String = String()
static var amount: Int = 0
static var date: Date = Date()
}
with all of the properties that you want a subscription to have, then you can store this information in this struct as you progress through each VC.
For example, if your first VC got the subscription name, then when your "prepareForSegue" function is called, as you are about to move to the second VC, then you could do something like this:
UserToAdd.name = "My Subscription Name"
Of course the string you assign to this name would depend on the data the user enters and such, but I hope it illustrates my point. It would be a similar process for each initial VC, but then once you have all of the data you need, you could just then call this data when your last VC loads, and set it to the text of a label or something like this:
var nameLabel = UILabel()
nameLabel.text = UserToAdd.name
self.view.addSubview(nameLabel);
Let me know if you have any further questions.
This question already has answers here:
How to create custom view programmatically in swift having controls text field, button etc
(5 answers)
Closed 6 years ago.
i want to create new view every time on click of button.
like when i first press button it will execute this code
which i tried
let userResizableView1 = ZDStickerView()
so now when i click next time on this button it should be create new view again with name userResizableView2 so how can i do this?
While you can't create a new name for the variable per se, what you could do is create a new view each time that has a new identifier.
So you could do something like this:
var views: [ZDStickerView] = [ZDStickerView]()
func buttonPressed(sender: UIButton){
let view = ZDStickerView()
view.accessibilityIdentifier = "userResizableView" + String(views.count + 1)
views.append(view)
}
This way you find this specific view again.
For reference, it might be easier if you are willing to use the .tag property which would look more like:
view.tag = views.count + 1
However, you could have problems with this if you were to alter the array later on since the tags would not be in order. I don't know how you are applying this, but just be wary.
The scenario you have explained above in your question is not possible because you are requiring dynamic name of a variable which is not supported by iOS.
But the logic can be developed for your requirement as:
1) You can create an array, globally to store your view.
2) When you click on a button then add a view in the globally declared array.
3) When you require your view, you can access the same by passing an relavent index.
for example,
you click your button for the first time, add a view in the array.
Now your array contain 1 object.
When you click your button 2nd time, your array contain 2 objects, and so on...
Now,
When you require your 2nd view then you can access
[<"YourArrayName"> objectAtIndex:2];
I'm looking for a way to show a UIView "InventoryView" in 2 view controllers.
I'm working on an inventory system for my game that I trying to make but I need to be able to access it from my main view, where it will go to a InventoryViewController (in this ViewController is my InventoryView) but I also need to be able to access the InventoryView from my BattleViewController where it does not go to my InventoryViewController but where it print the InventoryView on my BattleViewController so I can access everything durning the battle.
Example:
(evrything is dragand drop, the UIView and the UIButtons)
InventoryViewController
class InventoryViewController: UIViewController {
class InventoryView: UIView {
//here are some UIButtons and labels
}
}
BattleViewController
class BattleViewController: UIViewController {
class InventoryView: UIView {
//it should print the Inventory Screen on my BattleViewController
//here are the same properties as it shows in the InventoryViewController
}
}
This is a great example to look at the way OOP programming works best.
Ask yourself the following questions:
What is the purpose of the view?
Are the interactions on the view homogenous across all the instances? (touch events, specific behavior, etc...)
What is the minimum amount of information you need to make the view look the way you want?
Once you have those answers, you can approach the concept of reusability of views safely.
The way to go about it is to subclass UIView, create the necessary elements of your view, setup your constraints (still in the view, either in a nib or programmatically), and implement any behavior that will be consistent across views (For example if the view is a segmented control, every time you click a segment all the others go grey and the one you clicked go blue. Since that's the primary purpose of the segmented control, the code for it should belong to the segmented control).
Chances are you will find the docs very useful: https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIView_Class/
Lastly write a setup method that takes all the information you need and sets up all your graphical elements accordingly. Remember, views should never own their data (they should be templates, the controller will provide the data).
I have no idea what you view looks like but I assume the inventory is represented as an object. Then something like could be a good start:
class InventoryView: UIView {
var inventory: Inventory? {
didSet {
if let newInventory = inventory { //in case we set it to nil
setup(withInventory: newInventory)
}
}
}
private func setup(withInventory inventory: Inventory) {
//do your setup here
}
}
Then in your controller you can call:
let inventoryView = InventoryView()
inventoryView.inventory = myPlayer.inventory
You cannot use a view in two places, at least not within the UI. Every view can be added to only one super view at a time.
If you need the same contents to be displayed twice, create a UIViewController class which's view contains the common UI, create two of those and add them to your UI.
I am newbie in iOS with swift. What I need to do right now is that I have a textfield for user to enter his/er username. And once he clicks the other textfields, say password section, the application will automatically check whether this name exists or not and display a "V" or "X" image in a imageView. But I don't know how to do that or what method or action I should deal with. In Android, I could detect the focus of that textfield.Once the textfield loses the focus and if the text isn't empty, I can retrieve the text and request to my server to verify whether it exists or not. In iOS, I'm totally confused how to detect this, and is this related with first responder? Thx for advice in advance!
Use UITextFieldDelegates.
class XXX : YOURCONTROLLER, UITextFieldDelegate {
override func viewDidLoad() {
super.viewDidLoad()
self.PASSCODE.delegate = self
}
func textFieldDidEndEditing(textField: UITextField) {
if textField == PASSCODE {
//update stuffs
}
}
}
On iOS, you generally create the interface in Interface Builder (a graphical tool you can use to place UI elements on screen and define many of their properties), and then you link them to your code using what's called IBOutlets/IBActions.
The code your link those elements to is often the view controller ; the view controller is the object that is responsible for managing a view and the events it receives.
To make an IBOutlet, go to your interface builder file (1), select the view controller you are interested by (2, for you it will be the one it which your form is present) open the assistant editor (3), the assistant editor should show to code corresponding to your view controller. Then control drag to the inside of your view controller class definition (4).
Once you do that there will be a little "popup" asking you wether you want an outlet or an action (5), if you just want a reference to the given UI object, then select an outlet.
If the object does an action (for example a button) and you want a method to be called when this action occurs, then you would choose action.
From there this should be pretty straightforward, what I would do would be to make an outlet to the textfield containing the password, and an action for the "Send/Connect" button, whether the method linked to this event would be called, I would check if the password is right or wrong.
EDIT : Maybe I added to much details and you already know a lot about what I told you, but when beginning, those things aren't always that much easy.
I have created a timer class in swift and when a user clicks a button I segue to another view and pass data between the two views. The time class uses 3 separate labels for hour, minute and second however I would like to pass all 3 in a single variable.
My question is, how do I access the text inside a label. If I use "\(hourLabel.text)" (for example) I get a message "Optional(00)".
If you're trying to access another view controller's view objects (a UILabel, for example) don't do that. It violates the principle of encapsulation, and also often doesn't work.
If try to evaluate hourLabel.text where hourLabel is an outlet in your current view controller, the outlet link is probably broken (and nil.)
Post the actual code you are trying to use.
Use this...
if hourLabel.text != "" {
println("\(hourLabel.text!)")
}
Why don't you try this...
if(!hourText.text){
// Do something...
}