ios


Having issues with UILabel not updating like it should


I'm creating a simple app using Swift that tells you if the number you are entering is a prime number or not. I know the logic is correct, but when I run the app and enter a number and press the button my label is not updating like it should. Can someone help me?
import UIKit
class ViewController: UIViewController {
#IBOutlet weak var number: UITextField!
#IBOutlet weak var results: UILabel!
#IBAction func buttonPressed(sender: AnyObject) {
var numberInt = number.text.toInt()
if numberInt != nil {
var unwrappedNumber = numberInt!
var isPrime = true
if unwrappedNumber == 1 {
isPrime = false
}
if unwrappedNumber != 2 && unwrappedNumber != 1 {
for var i = 2; i < unwrappedNumber; i++ {
if unwrappedNumber % i == 0 {
isPrime = false
}
}
}
if isPrime == true{
results.text = "\(unwrappedNumber)Is Prime"
}else {
results.text = "\(unwrappedNumber)Is not Prime"
}
}else {
results.text = "Enter a number"
}
}
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
The problem in your project is you are performing a wrong action at wrong place like in your StoryBoard you right Click on TextField you find that you connected Editing Did End is connected with #IBAction func buttonPressed(sender: AnyObject) this action. because of this your action is not working.
Now the solution for your problem is remove your action connection from that textField by Clicking that cross button and it will look like :
after that connect ** buttonPressed** action with Run Test Button like this:
HERE I updated your code.
I recommended you to watch some tutorial on UI so you can batter understands how this all things works.

Related Links

Increase Badge Count From Client side not from Payload Receive count
How to update objects in core data?
Uploading video using AFNetworking causes memory issue
How whatsapp calling works in IOS?
Navigating to UITableViewController embedded with UINavigationController
Swift - return variable from within closure
CoreData: Observing Property Changes Behaves Differently
App sometimes crashed on start
How can I avoid the preview of password UITextField? [duplicate]
How to change background color of UIWebBrowserView in UIWebView in iOS8 Objective C
Store a variable in a conversation in iMessage Apps
IOS 10 UIImagePickerController Delegate Not Being Called
Podfile not downloading latest Swift 3 version of pods
Navigation Controller Shows Root View in Any Case
MKPolyline displays artifact/pixelation on map after zoom-in
Custom UITableViewCell isn't released or reused after navigation, memory leak

Categories

HOME
ios
actions-on-google
collections
configuration
spring-roo
afp
weight
jqgrid
tsql
httpbuilder-ng
ibm-midrange
sony
google-adwords
tin-can-api
telnet
cobol
obiee
scrollbar
deserialization
raspberry-pi2
android-dialogfragment
data-annotations
round-robin
html-email
windows-applications
nxbre
haml
pcap
aikau
rets
smallbasic
listbox
text-parsing
solarwindslem
file-manager
vue-router
webcenter
blockui
gradient-descent
jdom-2
vtable
microsoft-translator
sinon
sagepay
ibm-wcm
amazon-vpc
message-hub
nomenclature
using
jquery-masonry
tpl-dataflow
jquery-ui-accordion
zbar
breach-attack
zenity
android-alertdialog
tachyon
topojson
cakephp-2.3
android-chips
xceed
telecommunication
rsh
ng-annotate
kernighan-and-ritchie
geneos
activemodel
target-platform
okuma
airplay
id3
balana
java-gstreamer
dual-table
radix-sort
text-alignment
quasar
mtp
esky
emberfire
getuikit
json-web-token
office-2010
image-scaling
azimuth
disk-io
declspec
android-c2dm
micro-orm
geometry-surface
northwind
shebang
cocosbuilder
rgba
expression-encoder-sdk
bapi
katta
exact-synergy-enterprise
apache-config
indextank
webshop
utility
gendarme
ants
metaphone

Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App