I have seen other threads answering this question but my issue is I don't understand what is being said, being new to programming. My app has a User ID and Password text field to let the user log in with current credentials. When they data is entered, I want the user to be able to make the keyboard go away.
Here is what I have done. The problem is, nothing happens. Or, I don't understand what I have written and am not testing it correctly (although I am testing it as I would expect my novice users to, and it is not acting as expected). I appreciate any help you can give.
These text fields and this button are defined in the class ViewController: UIViewController. Then the function to hide the keyboard is in the ViewDidLoad method.
#IBOutlet var loginEntered: UITextField!
#IBOutlet var passwordEntered: UITextField!
#IBAction func loginButtonTapped(sender: AnyObject) {
override func viewDidLoad() {
super.viewDidLoad()
self.logInError = 1
func textFieldShouldReturn(passwordEntered: UITextField!) -> Bool {
passwordEntered.resignFirstResponder()
return true
}
Thank you again,
Greg
This is the step by step approach of your problem. I will show you mostly with storyboard.
Step 1: Go to the Project Navigator on left and select Main.stoyboard and select the ViewController in which you want to add your textfields and button.
and then add the 2 UITextfields and one UIButton by dragging and dropping from the object library in your ViewController in storyboard.
Step 2: Add your IBOutlets for the textfields and button in your ViewController-
Spet 3: Go back to your storyboard, select the view controller and click on the right most icon which is called the connection inspector. There you will see the your textfields and button names under the Outlet.
Now connect them by clicking and holding the outlet from the inspector and drag from there to the desired textfield or button. Do that for all. If you miss any connection then your code has no way to determine which variable corresponds to which component(TextFields or Button).
Step 4: Select your TextField, userID from the storyboard, then in the Connection inspector, you will see an Outlet called "delegate"(see the image below). It is this specific textfield's(userID) delegate which you need to assign to your ViewController. This tells your Textfield that, the View Controller will implement its delegate methods.
Do the same for the "password" TextField too. After you connect the password textField's delegate to the view controller, it will look something like this -
Now you are good to go.
Step 5:
Finally, go to your View Controller and add the "func textFieldShouldReturn(textField: UITextField!) -> Bool" delegate method.
This is the complete solution which I tested in my device too. Hope I have made it clear..
simply there are two steps you need to do
The class should conform to UITextFieldDelegate protocol by declaring beside the inherited superclass of what you use , UITextFieldDelegate so for example if your class inherits from UIViewController it will be like that
class ViewController: UIViewController , UITextFieldDelegate
since the class now conforms to the UITextFieldDelegate protocol put this delegate method somewhere in the class optional func textFieldShouldReturn(_ textField: UITextField) -> Bool
and return YES
Hey check the apple developer guide for it here
https://developer.apple.com/library/ios/documentation/UIKit/Reference/UITextFieldDelegate_Protocol/#//apple_ref/occ/intfm/UITextFieldDelegate/textFieldShouldReturn:
Related
Now I'm learning swift using Xcode, but I don't know where to drag and drop and why drag and drop "label" to somewhere
should I drop no.1? or no.2? or no.3? and why I should drop there?
If you drop it to 1, Xcode automatically generate an #IBOutlet for you. If you drop it to 2 or 3, then #IBAction. There's no other reason for that except that Xcode tries to be smart and help you organize code more nicely: properties with properties in the top of the class, methods – in the methods area. And you can also move declarations to another place later: except for code style matters, it doesn't matter, where exactly within your class you put declarations.
as you are trying to take an IBOutlet for a label , its nice to keep it on top of the class (where you mention 1).
normally Xcode gives suggestion you , if you drug on top side it will be #IBOutlet
or you on bottom like 2 or 3 it will be #IBAction like you already took a button action in your code .
Your Solution On Code:
import UIKit
class CodePresentViewController: UIViewController {
#IBOutlet weak var YourLabel: UILabel! // insert #IBOutlet type property here
override func viewDidLoad() {
super.viewDidLoad()
}
#IBAction func tapBackButton(_ sender: UIButton) {
//Yout Button COde
}
}
If you want to own the label in your code, and then make some configuration to the label by code, use "1". It will give you a #IBOutlet label object in your code.
If you want to set the label's action, use "2" or "3".
I am having a problem when trying to disable button's user interaction of 1 view controller in another view controller.
I have searched similar questions here, but some seems outdated or does not work for me:
How to access an IBOutlet from another class.
My scenario is as follows:
class ViewControllerA() {
#IBOutlet weak var btnFirst: UIButton!
#IBOutlet weak var btnSecond: UIButton!
#IBOutlet weak var btnThird: UIButton!
override func viewDidLoad() {
var vcB = ViewControllerB()
vcB.closure = {
// Meet some condition, want to disable buttons of ViewControllerA here
}
}
}
class ViewControllerB() {
var closure: () -> Void = {}
// Do something with closure here
}
My problem is that i set the breakpoint in the closure and try to use directly IBOulet in closure to disable buttons like:
btnFirst.isUserInteractionEnabled = false
Or try to set a property of ViewControllerA in closure of ViewControllerB and use property observer, whenever this property changes, enable or disable buttons of ViewControllerA.
My problem is that, i can still click the buttons as if it's enable. Sorry, i cannot post the code, please help me!
Thanks
You can post notification from second view controller and add observer for that particular posted notification in first view controller.
In that observer method, you can do your stuff like disabling user interaction for first view controllers' button.
Since you did not post any relevant code, I can only guess what might have happened:
I assume that you are not accessing the btnFirst of the correct viewcontroller. In ViewControllerA.viewDidLoad you are creating an new Instance of ViewControllerB and set the closure. Are you also showing exactly this view controller's view? Or how will the user navigate to B? If you are using storyboard segues, those will create a new B instances and show its view. Now when you execute the closure in A, this will disable the button of the first B, not of the B that is displayed.
But this is still just a guess...
At the moment I am setting up my buttons for my keyboard with the following code:
func setupButtons() {
for subview in self.view.subviews {
if subview.isKindOfClass(UIButton) {
setupButton(subview as! UIButton)
}
}
}
func setupButton(btn: UIButton) {
btn.addTarget(self, action: #selector(KeyboardViewController.keyPressed(_:)), forControlEvents: .TouchUpInside)
}
But is there anyway I can skip this and all the target inside the layout builder so I can save a little bit of time looping through buttons on each keyboard view?
Sure, there are two ways to connect objects to Interface Builder, through outlets and actions.
class ViewController: UIViewController {
#IBOutlet weak var doStuffButton: UIButton!
#IBAction func doStuff(sender: UIButton) {
//do stuff
}
}
After adding that code look in interface builder and click on the view controller for this class.
Click on the connections inspector
Under outlets you will now see doStuffButton, however over the circle to the right of that, press control and click with your mouse, and drag it over to the button you want to connect it to and release. Setting outlets is NOT required for enabling actions. I just wanted to show this to you as well.
In the same pane you will also see received actions and the doStuff: method. Click and drag in the same way and release on the button. Select which type of event you want to process this action (normal is touch up inside).
Once you're all hooked up it should look like this:
There are many other variations of how to do this, but I thought this would get you a quick start.
If you're trying to ask how to do this without coding anything, just go into the assistant editor view of Xcode and Ctrl-drag from your button to the controllers class file. Then when the pop up displays, change outlet to action and give it a method name. This creates the IBAction method for you.
But in reality, the way you are doing it now with the for loop is far better. Especially if you have many buttons.
I have a UIView with a TableView and a Button (Big Button). The TableView has a custom Cell. In this cell there is an "Add" button. I want to animate the first button when the user makes click on the Add button.
This is my schema:
This is my code:
class ProductsViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
#IBOutlet var tableView: UITableView!
#IBOutlet var bigButton: UIButton! <- I WANT TO ANIMATE THAT BUTTON
}
ProductCell class
class ProductCell: UITableViewCell {
#IBAction func addProduct(sender: AnyObject) {
//I WANT TO ACCESS THE BIG BUTTON FROM HERE
}
}
Screen example of my app
I've tried to get the parent controller or the superview to get the IBOutlet but the app is crashing allways
Add block properties to your cells which lets them notify your view controller when they have been clicked. In your view controller block code, you can then access the big button.
See my answer to a similar question. Simply replace the switch example with your button. So replace UISwitch with UIButton.
How can I get index path of cell on switch change event in section based table view
So rather than have the cell try and talk to another cell/button, have the cell notify the controller which can then manage the big button changes.
Although I made a comment about using alternate methods you could also employ a strategy below based on updates to a property stored in the current view controller class. You could just as well use property observation on the ProductsViewController but I assume you'd like to keep OOP focused and reduce the size of your controller.
Subclass the ViewController
One could subclass an existing UIViewController and then create a property in the super class that deals with the value that was changed (row tapped). In that subclass you could then do some animation. Because you would be subclassing you continue to obtain all the benefits and methods defined in your existing controller. In your identity inspector point your Class to the new subclass and create any functional updates to your UI using animation.
class ProductsViewController:... {
var inheritedProperty:UIView = targetView {
willSet {newValue } // is the newValue
didSet {oldValue} //is the old value
}
}
class AnimatedProductsViewController:ProductsViewController {
override var inheritedProperty:UIView {
//do something interesting if the property of super class changed
willSet {newValue } // is the newValue
didSet {oldValue} //is the old value
//you might want to call this method like so
// didSet { animate(newValue) }
}
func animate (view: UIView){
//do animation routine using UIView animation, UIDynamics, etc.
}
}
Property Observation
Whenever the didSelectCell... method is called just set a value to the inheritedProperty. Then add the property observers (see sample code) and react when the property changes (maybe pass a reference to the view you want to animate).
For example: Within the property observer you can just take that view and pass it to your animator function (whatever is going to do the animation). There are many examples on SO of how to animate a view so just search for (UIView animation, UIDynamics, etc).
The normal benefits of separation are encapsulation of functionality and reuse but Swift also guarantees that each set of property observers will fire independently. You'd have to give some more thought to this as to its applicability in this use case.
Do all this things in your viewController
Add target Method to cell's add button in cellForRowAtIndexPath Method
Like This
cell.add.addTarget(self, action: "addProduct:", forControlEvents: UIControlEvents.TouchUpInside)
Define method
func addProduct(button:UIButton)
{
// do button animation here
}
I am attempting to learn Apple's Swift. I was recently trying to build a GUI app, but I have a question:
How do I interact with GUI elements of my app? For instance, I used interface builder to make a UILabel, and I connected it to my viewcontroller by control-clicking, so that I get the #IBOUTLET thing. Now, how do I, while in my view controller, edit the text of this UILabel? To state it another way, what code can I use to programatically control the text of something on my storyboard? All methods I have found online only appear to work with a button generated in code, not a button generated on a storyboard.
I've seen code like
self.simpleLabel.text = "message"
If this is right, how do I link it with the label in question? In other words, how do I adapt this code to be connected with the IBOutlet (If that's what I do)
If you've successfully linked the control with an IBOutlet on your UIViewController class, then the property is added to it and you would simply replace simpleLabel with whatever you named your IBOutlet connection to be like so:
class MyViewController: UIViewController {
#IBOutlet weak var myLabel: UILabel!
func someFunction() {
self.myLabel.text = "text"
}
}
The outlet you created must've been named by you. The outlet belongs to your view controller. self.simpleLabel means 'fetch the value of my property named 'simpleLabel'.
Since different outlets have different names, using self.simpleLabel here won't work until your outlet is named 'simpleLabel'. Try replacing 'simpleLabel' with the name you gave to the outlet when you created it.
The correct way now would be:
self.yourLabelName.text = "message"
If you have something like this for an IBOutlet:
#IBOutlet var someLabel: UILabel!
then you could set the text property just like in your example:
someLabel.text = "Whatever text"
If you're having problems with this, perhaps you're not assigning the text property in the right place. If it's in a function that doesn't get called, that line won't execute, and the text property won't change. Try overriding the viewDidLoad function, and put the line in there, like this:
override func viewDidLoad() {
super.viewDidLoad()
someLabel.text = "Whatever text"
}
Then, as soon as the view loads, you'll set the text property. If you're not sure if a line of code is executing or not, you can always put a breakpoint there, or add some output. Something like
println("Checkpoint")
inside a block of code you're unsure about could really help you see when and if it runs.
Hope this helps.
You may trying to change a UI component not in the main thread, in that case, do this:
DispatchQueue.main.async {
someLabel.text = "Whatever text"
}