New to this, so please bear with me. I have set up an IBAction to allow for a URL to open in Safari when the button is tapped. The URL opens successfully, but then I receive error in AppDelegate.swift: unrecognized selector sent to instance. When troubleshooting, I set up the same code and same IBAction in the initial view controller, and i did NOT get the error, and was able to return back to the app (from Safari) successfully. It seems this is happening only in one specific View Controller, which is not the initial VC. I'm hoping this is just a rookie and/or trivial mistake. Thanks for any help :)
import UIKit
class WeekDetailViewController: UIViewController {
#IBOutlet weak var lessonCompleteBtn: UIButton!
#IBOutlet weak var stackOButton: UIButton!
#IBOutlet weak var stackExchButton: UIButton!
#IBOutlet weak var redditButton: UIButton!
#IBOutlet weak var facebookButton: UIButton!
override func viewDidLoad() {
super.viewDidLoad()
}
#IBAction func stackOButton(_ sender: Any) {
UIApplication.shared.open(URL(string: "https://www.google.com")! as URL, options: [:], completionHandler: nil)
}
}
Here is the problem:
Related
I am using the following UIView extension:
https://github.com/snoozelag/GoneVisible
I have successfully downloaded the file and added the Swift file and I am using the extension to hide (gone method) and show (visible method) buttons on the navigation bar. When the app first opens, I call this extension in an attempt to hide certain buttons if the user is already logged in. However, this has not been working. Strangely, it DOES work and hides the buttons after I segue to a different view and go back.
Here is the code:
import UIKit
import Parse
class ViewController: UIViewController {
#IBOutlet weak var signUpButton: UIButton!
#IBOutlet weak var logInButton: UIButton!
#IBOutlet weak var myAccountButton: UIButton!
#IBOutlet weak var bigGame: UIImageView!
private func setUpPage(){
let currentUser = PFUser.current()
if currentUser != nil {
// Do stuff with the user
self.myAccountButton.visible()
self.signUpButton.gone()
self.logInButton.gone()
} else {
// Show the signup or login screen
self.myAccountButton.gone()
self.signUpButton.visible()
self.logInButton.visible()
}
}
override func viewDidLoad() {
setUpPage()
super.viewDidLoad()
self.navigationItem.hidesBackButton = true;
}
override func viewWillAppear(_
animated: Bool) {
setUpPage()
}
My question is, how can I get this extension to fire when the app is first opened?
Thanks a lot for your help :)
Don't forget to call super.viewWillAppear(...) when you override the inherited implementation.
This might solve your issue - but even if not it is correct to do it.
Update:
try calling setUpPage() only once and only after you call super.viewDidLoad()
In ViewController :
protocol SearchResultDelegate {
func SendDataToResultController(_ result: [SkelbimasModel])
}
class ViewController: UIViewController, UISearchBarDelegate, CLLocationManagerDelegate, MKMapViewDelegate, AddSkelbimas {
#IBOutlet weak var Label: UILabel!
#IBOutlet weak var SearchBar: UISearchBar!
// #IBOutlet weak var boomFilter: BoomMenuButton!
// #IBOutlet weak var Label: UILabel!
#IBOutlet weak var CategoryView: UIView!
#IBOutlet weak var Map: MKMapView!
#IBOutlet weak var bmb: BoomMenuButton!
var Delegate:SearchResultDelegate?
.........
//ijungimiame lentele su paieskos rezultatais
if(result.count > 1) {
Delegate?.SendDataToResultController(result)
GoToSearchResultVC(result)
}
Function GoToSearchResultVC(result) is called it opens new controller
in that new controller code is :
import UIKit
class SearchResultTableViewController: UITableViewController, SearchResultDelegate {
.....
func SendDataToResultController(_ result: [SkelbimasModel]) {
print("result count: \(result.count)")
results = result
}
But this function is never called.
I used delegates before and was fine. Or i'm just tired and do not see where i did wrong...
Somewhere where you create the ViewController in your code in SearchResultTableViewController you have to set the value of the delegate:
let viewController = ViewController()
viewController.Delegate = self
// present viewController
If you are showing after SearchResultTableViewController from ViewController, isn't it easier to do something like:
func GoToSearchResultVC(result: Any) {
let searchVC = SearchResultTableViewController()
searchVC.results = results
self.present(searchVC, animated: true, completion: nil)
}
Delegate design patter is to receive events from other classes, if you only want to set a variable, this way is better for me
BTW checking your code, you are calling first the delegate before creating the SearchResultsVC
Sorry if this particular problem has been asked about, I followed the answers on other threads but none of them seemed to work, but I just started learning Swift so all of this is pretty new to me.
So, I have a text field in two View Controllers and I want the third View Control to display a result based on the input from the other two controllers when I press a button.
I followed this tutorial and placed the text fields, label and button like I said before.
I placed my code (which you can see below) inside ViewControl.swift.
The problem is that when I attempt to run it I get a "Thread 1 :EXC_BAD_INSTRUCTION (code=EXC_I386_INVOP, subcode=0x0)" error in the last two lines.
class ViewController: UIViewController {
var a: String = ""
var b: String = ""
#IBOutlet weak var aTextField: UITextField!
#IBOutlet weak var bTextField: UITextField!
#IBOutlet weak var calculateButton: UIButton!
#IBOutlet weak var resultLabel: UILabel!
#IBAction func calculateButtonPressed(_ sender: UIButton) {
let a = aTextField.text!;
let b = bTextField.text!;
I think that the error is from the data not passing between the views (because before I had everything in the same view and it worked fine), but since I only have one ViewController.swift file I couldn't figure out how to use a Segue.
Do not declare same variables multiple times. Remove let before a & b . You have already declared a & b globally and then tried to redeclare it inside IBAction
class ViewController: UIViewController {
var a: String = ""
var b: String = ""
#IBOutlet weak var aTextField: UITextField!
#IBOutlet weak var bTextField: UITextField!
#IBOutlet weak var calculateButton: UIButton!
#IBOutlet weak var resultLabel: UILabel!
#IBAction func calculateButtonPressed(_ sender: UIButton) {
a = aTextField.text!;
b = bTextField.text!;
Make sure your control outlets are setted properly.
In your two variables a & b are re-declared.Just update your code like below
#IBAction func calculateButtonPressed(_ sender: UIButton) {
self.a = aTextField.text!
self.b = bTextField.text!
}
I am using Xcode 8. In my code, I have certain items hidden, but when I launch the simulator to test, those same items show up. Is there something that I am missing?
import UIKit
class ViewController: UIViewController {
#IBOutlet weak var logoImg: UIImageView!
#IBOutlet weak var howManyTapsTxt: UITextField!
#IBOutlet weak var playBtn: UIButton!
#IBOutlet weak var tapBtn: UIButton!
#IBOutlet weak var tapsLbl: UILabel!
#IBAction func onPlayBtnPressed (sender: UIButton!) {
logoImg.isHidden = true
playBtn.isHidden = true
howManyTapsTxt.isHidden = true
tapBtn.isHidden = false
tapsLbl.isHidden = false
}
}
My code is above. The logo, howManyTapsTxt, and playBtn should be the only items shown when the simulator is launched. And when the playBtn is pressed, the tapsLbl and tapBtn should be the only items shown. But that is not the case. Any help/guidance is greatly appreciated. Thanks.
Your posted code makes no attempt to set the initial state of any of your views. The typical solution is to set the state in the viewDidLoad method.
override func viewDidLoad() {
super.viewDidLoad()
// Set the initial state of your views here
tapBtn.isHidden = true
tapsLbl.isHidden = true
}
The other option is to mark these views as hidden in Interface Builder.
I am trying to change the image of a button when clicked, they are stars like a rating system. when clicked the image remains the same and does not change when clicked. I set the default image in story board.
class DetailViewController: UIViewController {
#IBOutlet weak var detailTitleViewLbl: UILabel!
#IBOutlet weak var detailDescription: UILabel!
#IBOutlet weak var yearLabel: UILabel!
#IBOutlet weak var star1: UIButton!
#IBOutlet weak var star2: UIButton!
#IBOutlet weak var star3: UIButton!
#IBOutlet weak var star4: UIButton!
#IBOutlet weak var star5: UIButton!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
#IBAction func star1Pressed() {
star1.setImage(#imageLiteral(resourceName: "favorite (1).png"), for: UIControlState.selected)
}
func star2Pressed() {}
func star3pressed() {}
func star4pressed() {}
}
A button doesn't change to the selected state when clicked. You need to write code that sets the button's selected flag to true when it's clicked.
Note that you can connect more than one IBAction to a button, so you could have one action that sets the selected flag to true, and another that does custom "stuff", so you don't have to duplicate the "boilerplate" code to set the selected state in the IBAction of every such button.