I begin to learn swift language and i've a problem.
I try to display a label when i put the button. I show you :
#IBAction func nextRule(sender : UIButton) {
rule = Rules(game: Int(randomNumber))
}
#IBOutlet var lTitle: UILabel!
#IBOutlet var lRule: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
lTitle.text = rule.title
lRule.text = rule.rule
Rules(game: Int) contain a model whith "title" And "rule"
When i press the button, i want to display a new Rule, is it possible by doing something like his?
Thanks for your answer.
How about assign your rule to lTitle and lRule like this
#IBAction func nextRule(sender : UIButton) {
rule = Rules(game: Int(randomNumber))
lTitle.text = rule.title
lRule.text = rule.rule
}
Related
I have a ViewControllertheir is a button at top right side. When i click that button i want to Show/Hide a UIView at bottom with size half of ViewControlleron same ViewController . Anyone help me. Thanks in advance.
If only Show/Hide a UIView is your requirement, below code will work:
class ViewController: UIViewController
{
#IBOutlet weak var viewHeightConstraint: NSLayoutConstraint!
#IBOutlet weak var customView: UIView!
override func viewDidLoad()
{
super.viewDidLoad()
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
self.viewHeightConstraint.constant = self.view.bounds.height / 2
}
#IBAction func onShowHideButtonPress(_ sender: UIButton)
{
self.customView.isHidden = !self.customView.isHidden
}
}
Storyboard:
I have a problem with my delegate use in a protocol, many person face the same problem but no answer works for me.
My first class is FavorisHeaderTableView
import UIKit
protocol FavorisHeaderDelegate {
func changeFavoris(sender: FavorisHeaderTableViewCell)
}
class FavorisHeaderTableViewCell: UITableViewCell {
#IBOutlet weak var lblTitle: UILabel!
#IBOutlet weak var txtFavoriteNameInput: UITextField!
#IBOutlet weak var lblIcon: UILabel!
#IBOutlet weak var lblTime: UILabel!
#IBOutlet weak var lblDescription: UILabel!
#IBOutlet weak var btnFavHeart: UIButton!
#IBOutlet weak var btnFavHome: UIButton!
#IBOutlet weak var btnFavShop: UIButton!
#IBOutlet weak var btnFavWork: UIButton!
#IBOutlet weak var btnFavGolf: UIButton!
var myDelegate: FavorisHeaderDelegate? = nil
var defaultIcon:FavIconType = .heart
var selectedIcon:UIButton? = nil {
didSet {
selectedIcon!.backgroundColor = Design.Palette.red
selectedIcon?.layer.cornerRadius = 3
selectedIcon?.tintColor = Design.Palette.white
}
willSet {
if selectedIcon != nil {
selectedIcon!.backgroundColor = UIColor.clear
selectedIcon?.tintColor = UIColor(red:0.671, green:0.651, blue:0.635, alpha:1)
}
}
}
#IBAction func didSelectIcon(_ sender: UIButton) {
selectedIcon = sender
self.myDelegate?.changeFavoris(sender: self)
}
#IBAction func changeTitle(_ sender: Any) {
txtFavoriteNameInput.text = "gare centro"
print("delegate: ",myDelegate)
if myDelegate != nil {
myDelegate?.changeFavoris(sender: self)
}
}
override func awakeFromNib() {
super.awakeFromNib()
// Initialization code
}
override func setSelected(_ selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
// Configure the view for the selected state
}
}
The second class who use the protocol is
addFavoriteViewController: UIViewController {
// MARK properties
let defaultLocalizer = AMPLocalizeUtils.defaultLocalizer
var favorisName:String? = nil
var defaultIcon:FavIconType = .heart
var delegate:handleFavorite? = nil
#IBOutlet weak var favTableView: UITableView!
var headerCell:FavorisHeaderTableViewCell?
override func viewDidLoad() {
super.viewDidLoad()
// Localization of labels
//lblAddToFavorite.text = defaultLocalizer.stringForKey(key: "str_favorites_addTitle")
//lblFavoriteName.text = defaultLocalizer.stringForKey(key: "str_favorites_nameInput")
favTableView.delegate = self
favTableView.dataSource = self
self.headerCell?.myDelegate = self
}
override func viewWillAppear(_ animated: Bool) {
// Text Field
//favorisName.clearButtonMode = .whileEditing
}
var place:PlaceModel? = nil
var itinerary:(source:PlaceModel, destination:PlaceModel)? = nil
let db = DataController.shared
var favorite:FavoritesMO? = nil
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
extension UGOaddFavoriteViewController: FavorisHeaderDelegate {
func changeFavoris(sender: FavorisHeaderTableViewCell) {
defaultIcon = sender.defaultIcon
favorisName = sender.txtFavoriteNameInput.text
}
}
When I try this code "myDelegate" is always nil and I don't understand what's wrong despite of all topic read about this problem.
You are setting self as the delegate of the wrong cell!
Here:
self.headerCell?.myDelegate = self
you set the headerCell's delegate to self, but headerCell is never actually displayed on the screen!
You need to actually set the delegates of the cells on the screen, not the delegate of a random cell that you created.
The best place to do this is cellForRowAtIndexPath:
let cell = tableView.dequeueResusableCell(withIdentifier:...)!
// configure your cell
cell.delegate = self // set the delegate here!
I wanted to programmatically control the menu view so I tried the following:
import UIKit
class ViewController: UIViewController {
#IBOutlet weak var menuView: UIView!
override func viewDidLoad() {
super.viewDidLoad()
}
#IBAction func menuBtn(_ sender: UIButton) {
print("menuBtn")
print(type(of: menuView!)) // <- `Thread 1:EXC_BAD_INSTRUCTION (code=EXC_I386_INVOP, subcode=0x0)`
}
}
And click the button, console says
fatal error: unexpectedly found nil while unwrapping an Optional value (lldb)
Both are connected to ViewController.
How can I get print(type(of: menuView!))?
===============================================================
ps.
I've simplified my real problem. this is my real code:
button on ViewController.Swift
#IBAction func openModal(_ sender: UIButton) {
print("open btn clicked")
let vc = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "menuViewController") as! MenuViewController
self.addChildViewController(vc)
self.view.addSubview(vc.contentView!) /// <- This cause problem
}
MenuViewController.swift
import UIKit
class MenuViewController: UIViewController {
#IBOutlet weak var contentView: UIView!
override func viewDidLoad() {
super.viewDidLoad()
view.backgroundColor = UIColor.clear
view.isOpaque = false
}
#IBAction func closeBtn(_ sender: UIButton) {
print("close btn clicked")
self.view.removeFromSuperview()
}
}
and Why I wan't using it:
I can't scrolling when menuView appeared.. I want to use the buttons in the menu view and scroll at the same time.
I don't know why I can't access the text from the UITextField.
Here is my code:
import UIKit
import Firebase
import FirebaseAuth
class MessagesVC: UIViewController {
#IBOutlet weak var emailTextInput: UITextField! //Email Text Input
#IBOutlet weak var passwordTextInput: UITextField! //Password Text Input
var emailText: emailTextInput.text; //I get the error 'Use of undeclared type emailTextInput here.'
override func viewDidLoad() {
super.viewDidLoad()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
#IBAction func login(_ sender: Any) {
} //Login
#IBAction func signUp(_ sender: Any) {
Auth.auth().createUser(withEmail: , password: ) { (user, error) in
} //Authentication Create User
} //Sign Up
} //Class
This is the editor error I get:
Because you are declaring variable with value of textfield. You can assign textfield value to that string variable.
you can write it like,
First declare it with,
var emailText = ""
Then you can assign its value inside viewDidLoad like below,
self.emailText = self.emailTextInput.text!
I'm making an App with swift 3, xCode 8.2 and IOS10. The interface has a thermal vision screen (FLIR ONE). My question is, how can I take a photo? My code is:
import UIKit
class ThermalCameraVC: UIViewController, FLIROneSDKImageReceiverDelegate, FLIROneSDKStreamManagerDelegate {
//MARK: OUTLETS
#IBOutlet weak var imageView: UIImageView!
#IBOutlet weak var labelStatusCamera: UITextField!
#IBOutlet weak var labelCargeCamera: UITextField!
#IBOutlet weak var icnCancelPicture: UIButton!
#IBOutlet weak var icnUploadPicture: UIButton!
#IBOutlet weak var iconTakePicture: UIButton!
//MARK: VARIABLES
var simulatorStatus = false
var cameraBussy = false
override func viewDidLoad() {
super.viewDidLoad()
FLIROneSDKStreamManager.sharedInstance().addDelegate(self)
FLIROneSDKStreamManager.sharedInstance().imageOptions = FLIROneSDKImageOptions(rawValue: FLIROneSDKImageOptions.blendedMSXRGBA8888Image.rawValue)!
icnCancelPicture.isHidden = true
icnUploadPicture.isHidden = true
}
//MARK: CANCEL PICTURE
#IBAction func cancelPicture(_ sender: Any) {
cameraBussy = false
}
//MARK: UPLOAD PICTURE AMAZON S3
#IBAction func uploadPicture(_ sender: Any) {
}
func flirOneSDKDelegateManager(_ delegateManager: FLIROneSDKDelegateManager!, didReceiveBlendedMSXRGBA8888Image msxImage: Data!, imageSize size: CGSize){
let image = FLIROneSDKUIImage(format: FLIROneSDKImageOptions.blendedMSXRGBA8888Image, andData: msxImage, andSize: size)
//HERE I NEED TO STOP THE DELEGATED FUNCTION TO SAVE THE IMAGE !!
if self.cameraBussy{
//cameraBussy = false
}else{
DispatchQueue.main.async{
self.imageView.image = image
}
}
}
#IBAction func takeThermalPicture(_ sender: Any) {
cameraBussy = true
icnCancelPicture.isHidden = false
icnUploadPicture.isHidden = false
iconTakePicture.isHidden = true
}
}
How can I stop the flow of data in this delegated function? Because it is continually calling.
Set the delegate value to nil.
#IBAction func takeThermalPicture(_ sender: Any) {
FLIROneSDKStreamManager.sharedInstance().addDelegate(nil)
cameraBussy = true
icnCancelPicture.isHidden = false
icnUploadPicture.isHidden = false
iconTakePicture.isHidden = true
}