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];
Related
I am writing an iOS app using swift. The app is a Tour and takes the user through a series of steps.
Each step has generic properties like so:
class Step
{
var name : String;
var description : String;
}
The step will be displayed in the same way, with Name at the top and a "Next" button at the bottom which moves onto the next step. The standard UI back button will be available at the top as usual.
But, I also want to make certain steps perform certain functions, like:
show a video
show a map
take a picture
I'd like to consolidate each of these step types in to a reusable view or controller.
My question is, what is the best way to structure this in terms of Controllers and Views?
I'm currently using a StepViewController : UIViewController with a single View.
When I click NEXT, it creates a new instance of StepViewController, passes it the next step object and adds it to the stack using instantiateViewControllerWithIdentifier.
Which is the better method...
Create a new super-class ViewController of StepViewController for each Step Type, e.g. VideoStepViewController : StepViewController?
Create a new View for each Step Type, allowing the master StepViewController to dynamically load a particular View (e.g. VideoView) into the master controller.
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'm creating a registration form application. Users of the application will be able to add and delete questions.
So basically I have a class called questions and the methods for "modifying" questions will go under it. Right now I am working on the methods for adding a question to the form.
A question will consist of:
UILabel (What is your name?)
UITextField or UISegmentedControl
(Used for multiple choice question)
I am very new to Swift and still what I would consider a beginner at object-oriented programming. Right now I am trying to design the Questions class layout, should it look something like this?
class Question {
var label: UILabel
var multiChoice: UISegmentedControl
var textInput: UITextField
init(label: UILabel, multiChoice: UISegmentedControl, textInput: UITextField) {
self.label = label
self.multiChoice = multiChoice
self.textInput = textInput
}
// methods for adding question
}
So obviously the composition of a question will depend on actions (buttons pushed) the user takes. When a user selects to add a new question they will input a label, and decide whether they want to use a segmented control or text field.
Here's where my question comes in. After the user has decided on the parameters, my method for adding a question should create a new instance of a Question, which will be returned. This new instance of a Question should then be added to an array that stores all the created questions. Would this array be initialized in the action (pressing the add question button) or should it be initialized globally, so all future methods I add can access it.
The fewer global variables, the better.
Given the class above, I would expect your method for creating a Question will be a method of some UIViewController subclass and the array containing all the Questions should be a property of that same view controller sub-class.
The method that creates questions will have to add the label, segmentedControl and textInputs to some UIView (possibly the view controller's main view) so they can be visible.
Another quick note... If a Question can be either text input or multiple choice, but not both, then this one class should not have both fields in it. You will need two Question sub-classes (FillInQuestion and MultiChoiceQuestion perhaps,) possibly both derived from a Question base class.
I'm trying to select and set an instance variable that is part of another UIViewController, however, I don't know how to select another ViewController and access its contents without using a segue.
Whenever a user checks off a task, a percentage of tasks that are complete should be calculated and another view controller's instance variable should be set.
I realize I'm currently instantiating a new view controller instead of selecting the one I already have on the storyboard. I'm using a third party sidebar menu that resides behind my main view, although it really exists as a separate Scene/ViewController. It should be noted that this sidebar menu doesn't use a segue reveal itself. Is there any method to select another view controller and access it's instance variables?
#IBAction func checkOffTask(sender: UIButton) {
// Select sidebar view controller
let sidebarViewController = self.storyboard?.instantiateViewControllerWithIdentifier("sideBarScene") as! SideBarViewController
// Calculate percentage of completed tasks
// Select the count of all tasks
let allTasksCount = Float(firstDataSource.count + secondDataSource.count)
// Select the count of all completed tasks
let completedTasksCount = Float(secondDataSource.count)
// Divide the two to get a percentage
var completedTaskPercentage = completedTasksCount / allTasksCount
sidebarViewController.completedTaskPercentageTemporary = String(stringInterpolationSegment: completedTaskPercentage)
println(sidebarViewController)
println(sidebarViewController.completedTaskPercentageTemporary)
}
One trick could be find out current visible view controller and set properties on it. This thread may help you doing this.
Another way could be to get hold to targeted view controller's object when it is intialized by storyboard. For this you would need to rely on prepareForSegue:sender: method. Once you have saved the object you can pass it & use it to set the properties.
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...
}