Not able to assign text to UITextView - ios

error when try to assign a string to the UITextView.
coding as below:-
class ViewController: UIViewController{
#IBOutlet weak var txtResult: UITextView!
override func viewDidLoad() {
super.viewDidLoad()
txtResult.text = "ABC"
}
}

Go to your code and delete the #IBOutlet line of code that you created
Delete this in Storyboard:
I think you have few referencing outlets there for 1 object. You need to have only 1. So better to remove all of them.
Add new outlet again.
It should work.

Providing more error details might help to solve your issue. But you can still solve it by checking few things-:
1) Check for UITextView outlet in storyboard-:
2) Check if you have given correct class to controller . In my case it's UnderlineViewController check for yours. Your controller should have same class.
2.a) Go to storyboard click on controller and select identity inspector.
2.b) check class name.
Now add text in controller class-:
import UIKit
class UnderlineViewController: UIViewController {
#IBOutlet weak var textViewData: UITextView!
override func viewDidLoad() {
super.viewDidLoad()
textViewData.text = "Tushar"
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
OUTPUT-:
MAKE SURE YOU HAVE NO MORE THEN 1 OUTLET FOR ANY VIEW IN SIMILAR
CONTROLLER.

Related

Place references to IBOutlet and IBAction inside viewDidLoad or just before?

I understand that viewDidLoad is where you are supposed to put any set up code in relation to buttons, color, and other view related code. However, in a code sample I have just seen, a reference to IBOutlet and to IBAction are not written inside of viewDidLoad but rather, just before this method, as below. Are these not set up related code, as in creating a label and a method to manipulate it?
import UIKit
class ViewController: UIViewController {
#IBOutlet weak var nameLabel: UILabel!
#IBAction func showName(sender: AnyObject) {
nameLabel.text = "my name is Cyril"
}
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
ViewDidLoad is only one method of setup. You can put some code here, local variables, ets. #IBOutlet and #IBAction are links of Interface Builder and nameLabel. And this outlet and action suppose to be global, so you could use them in other functions, not only in viewDidLoad.
import UIKit
class ViewController: UIViewController {
#IBOutlet weak var nameLabel: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
nameLabel.text = "my name is Cyril"
}
}

Storing input from field as variable with swift 3

I realize this question has been asked numerous times before, but I can't quite get the solutions to work, even by just copying and pasting them, and suspect that most swift documentation spans the three versions since swift's release.
I'm attempting to do something as simple as storing a variable from a field input and not having much luck.
import UIKit
class ViewController: UIViewController {
#IBOutlet var userNumber: UILabel!
#IBOutlet var userField: UITextField!
#IBAction func userButton(_ sender: UIButton) {
let userInput = userField.text
//some action
}
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.
}
}
You should check whether you have set your textfield's delegate with respect to parent view controller.
Go to storyboard.
Select textfield.
Right click on it.
set delegate from textfield to view controller

Simple button on second view controller crashing app/ Swift

I have a very simple app so far. Two view controllers. I've set up a new .swift file for the second view. On each view I have a button that when pressed, changes a label to say "Pressed". Pretty simple.
On the first view controller everything works as expected. However, on the second view controller the app crashes when I press the button. I've set up IBOutlets and actions for all appropriate parts.
Does anyone have any insight?
code:
import UIKit
class PlayViewController: UIViewController {
#IBOutlet weak var newCardButton: UIButton!
#IBOutlet weak var labelTest: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
#IBAction func newCardButtonPressed(sender: UIButton) {
self.labelTest.text = "Pressed"
}
}
Screenshot:
Screenshot after crash- http://i.imgur.com/CHt8kA5.png
I think you should change the sender part like this.
#IBAction func newCardButtonPressed(sender: AnyObject) {
self.labelText.text = "Pressed"
}
If your connections are not set properly your app also crash. Delete them and reconnect it.From Utilities/connections inspector.

Create many UISliders that update a related label using one function

I have an experience where a user is rating a product, they do this by dragging sliders. Each slider has a related UILabel for the title and a UILabel for the value. I would like to avoid creating 12 functions, one for each slider and the associated label. I am new to development generally. I am guessing a class or an Array would be useful here, but am not sure how to use either. Here's the code that just updates the one value, and I know why that is, I'm just hoping to avoid declaring 12 variables for the value and 12 functions for each one.
import UIKit
class ViewController: UIViewController {
#IBOutlet weak var slider1: UISlider!
#IBOutlet weak var value1: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
}
#IBAction func sliderSlide(sender: UISlider) {
value1.text = round(sender.value*100).description
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
}
Well,you can use Outlet Collection and 'tag',
Drag every label into a same Outlet Collection
Drag every Slider IBAction into a same function
Then set the tag of slider as the index of label in Outlet Collection.
For example,you first drag labelA to Collection,then the tag is 0
Then all the code
import UIKit
class ViewController: UIViewController {
#IBOutlet var labels: [UILabel]!
override func viewDidLoad() {
super.viewDidLoad()
}
#IBAction func sliderSlide(sender: UISlider) {
let index = sender.tag
let label = labels[index]
label.text = round(sender.value*100).description
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
}
You can connect more than one slider to the same function. That's the purpose of the sender argument. You can do a switch statement on the pointer in swift, or by a set tag of the slider if you prefer. If it's all updating the same label, why even care which slider is updating?
I can think of two ways of doing this. Make a custom view controller that looks exactly the same as the code you have. It will have the slider and label as subviews. Then you'd only have 12 container views on your storyboard (not the greatest).
The other way would be using IBOutletCollections. These are what they sound like, collections of IBOutlets. Assign each slider that you place on the storyboard a unique tag from 0-11 (this is done under the attributes inspector). The tag will act as an index for the values array. Make sure that when you add each label to the collection, you do it in the correct order (it does matter!). Using IBOutletCollections, your code would look like this:
class ViewController: UIViewController {
#IBOutlet weak var sliders: [UISlider]!
#IBOutlet weak var values: [UILabel]!
override func viewDidLoad() {
super.viewDidLoad()
}
#IBAction func sliderSlide(sender: UISlider) {
values[sender.tag].text = round(sender.value*100).description
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
}

iOS - Pop-up custom subview after click

Maybe this is a simple problem, but I spend some time try to solve it and so far I failed.
I want to show custom view with a few of button after I clicked a block of text. I try to add and remove this view form subview but it's doesn't work.
Can you give me some tips about my problem?
Thank you for your help.
my code
class ViewController: UIViewController, UITextFieldDelegate {
#IBOutlet weak var myTextField: UITextField!
#IBOutlet weak var simpleView: SimpleView!
override func viewDidLoad() {
super.viewDidLoad()
self.myTextField.delegate = self
self.simpleView.removeFromSuperview()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func textFieldDidBeginEditing(textField: UITextField!) {
println("works")
self.view.addSubview(simpleView)
}
func textFieldDidEndEditing(textField: UITextField!) {
println("works2")
}
}
You'd better set the hidden property of simpleView instead of removing and adding the view.
put a container view in your main view and set the visibility hidden. when you want to show the popup, set the visibility to visible and load the view inside the container. To get the popup
look and feel ,you can use a transformation to animate.

Resources