I am trying to do something very basic, I have an outlet that is assigned with the YouTubePlayerView class and once loading an ID, it crashes.
Why is it crashing?
I checked that the outlet is connected properly and the debugger shows that videoId is defined correctly.
code:
import UIKit
import YouTubePlayer
class InfoViewController: UIViewController {
var videoId = "Bgh9u7x8i4Y"
#IBOutlet var youtubePlayer: YouTubePlayerView!
override func viewDidLoad() {
super.viewDidLoad()
self.youtubePlayer.loadVideoID(videoId)
}
#IBAction func exit(_ sender: UIButton) {
performSegue(withIdentifier: "unwindToMainFromInfo", sender: self)
}
}
Well, I am still not sure what causes the crash, I believe it is something related to the framework, and choosing the type of a UIView (and setting it as a YouTubePlayerView through the identity inspector) added in Storyboard caused a crash when trying to load the videoId.
The work around was to add the player programatically. I used a plain view in storyboard to make it easy to give my player a frame when initializing it. But otherwise programatically works smoothly.
code:
#IBOutlet weak var viewForPlayer: UIView!
override func viewDidLoad() {
super.viewDidLoad()
let frameForPlayer = viewForPlayer.frame
let player = YouTubePlayerView(frame: frameForPlayer)
player.loadVideoID(InstructionsVideoId)
player.play()
self.view.addSubview(player)
}
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()
I'm trying to build an app for school but I keep getting error messages. It's probably a really obvious mistake I made.
Basically I am trying to build a view that displays a UIWebView and changes to a 2nd view if a segment controller switch is pressed.
My Code is:
import UIKit
class ViewController: UIViewController {
#IBOutlet weak var myWebView: UIWebView!
override func viewDidLoad() {
super.viewDidLoad()
let url = URL(string: "http://google.de")
myWebView.loadRequest(URLRequest(url: url!))
}
#IBAction func Heute(_ sender: UISegmentedControl) {
performSegue(withIdentifier: "Switch", sender: self)
}
}
//Vertretung2
class Vertretung2: UIViewController {
#IBOutlet weak var UIWebView1: UIWebView!
override func viewDidLoad() {
super.viewDidLoad()
let url = URL(string: "http://google.de")
UIWebView1.loadRequest(URLRequest(url: url!))
}
#IBAction func Morgen(_ sender: UISegmentedControl) {
performSegue(withIdentifier: "Switch", sender: self)
}
}
My app keeps crashing when I switch from 1st view to 2nd view.
Is that UIWebView1 in Vertretung2 nil when viewDidLoad() is called on Vertretung2?
Ok, Sorry. I just read your crash log above, and it looks like you maybe forgot to set the Custom Class name in the storyboard for the viewController? Check that here.
The Class text entry field should have your custom class in it.
I have a UIViewController (Main Menu) that is displayed after my other UIViewController (Loading Screen) finishes downloading/syncing data from the web. My Main Menu is having problems as it doesn't always display all the UIButtons that are on the view when it shows up! There is an inconsistancy, 1 out of every 10 times the Main Menu loads, all the UIButtons will appear.
Here's how the view should look:
Here's how the view usually shows up:
I can put my finger where the UIButtons should be and move my finger away and they'll appear. I can also tap where they should be and my segues will still trigger, but the UIButtons dont just automatically show up. It looks like
I attempted to add a MainMenuView.setNeedsDisplay() to the ViewDidLoad function and it didn't help. I also created a test UIButton on the view that triggers a MainMenuView.setNeedsDisplay() command but the hidden UIButtons remain hidden.
If I leave the screen idle for 30 or so seconds, all the UIButtons will randomly appear without me having to manually make them appear.
The Main Menu view is normal and all UIButtons are visible if I segue to it from any part of the app aside from my Loading Screen.
Edit: Main Menu Code - (references are Strong)
import UIKit
import CoreData
class MainMenuViewController: UIViewController {
// MARK: References
#IBOutlet var btnGasManifolds: UIButton!
#IBOutlet var btnAirCompressors: UIButton!
#IBOutlet var btnVacuumPumps: UIButton!
#IBOutlet var btnUnitConversion: UIButton!
#IBOutlet var btnCFMCalculator: UIButton!
#IBOutlet var mainMenuView: UIView!
var userData = [NSManagedObject]()
// MARK: Functions
override func viewDidLoad() {
var name = String()
for duserData in userData {
name = duserData.value(forKey: "nameFull") as! String
}
print("Main Menu\n->\(name)")
backgroundSetup()
}
override func viewDidAppear(_ animated: Bool) {
mainMenuView.setNeedsDisplay()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
#IBAction func btnTest(_ sender: Any) {
mainMenuView.setNeedsDisplay()
}
override func prepare(for segue: UIStoryboardSegue, sender: Any!) {
if segue.identifier == "settingsSegue" {
let destination = segue.destination as! SettingsViewController
destination.userData = userData
}
}
// Background Setup
func backgroundSetup() {
let background = UIImage(named: "Data Screens")
var imageView : UIImageView!
imageView = UIImageView(frame: view.bounds)
imageView.contentMode = UIViewContentMode.scaleAspectFill
imageView.clipsToBounds = true
imageView.image = background
imageView.center = view.center
view.addSubview(imageView)
self.view.sendSubview(toBack: imageView)
}
}
The code which creates and shows the buttons after fetching the data from server needs to run on Main UI thread. If the code is running the background thread it will not properly display the UI elements and will take some time or manual scroll before showing the controls.
Run the code on Dispatch main queue.
dispatch_async(dispatch_get_main_queue(),{
//do some work
})
OR
Override one of the methods from ViewWillLayoutSubviews and ViewDidLayoutSubviews accordingly and run your code in these methods.
I'm new to xcode and am stuck on this problem, because after trying several sample code examples online, nothing works.
Could you help me figure out what the problem is?
My code below:
import UIKit
class ViewController: UIViewController {
#IBOutlet weak var Writingboard: UITextView!
#IBOutlet weak var Talkbutton: UIButton!
#IBAction func TalkbuttonTapped(_ sender: AnyObject){
Talkbutton.setTitle("Tap to Stop", for: .normal)
Writingboard.text="talking"
}
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
//Talkbutton.isEnabled=true
Writingboard.text="test the initializer"
}
}
you can try this bellow code you definetly get the output what you expected and make sure you connect the story board to your view controller class you button action and textfiled outlets can give IBActions and IBOutlets properly
output before not button tapped
After tapped the button
I am struggling with this awful project for two weeks now and nothing seems to work. I have to do an app that loads some words from a server and presents them as pins on a map(MKView). I have to cluster the pins when the user zooms-out and for that I have used a this-party library written in Objective-C, but I also had to make a custom callout view with a button. When the user presses the said button the app should go to a TableViewController and here is my problem: I can't make it to do it. I have used before the "performSegueWIthIdentifier" and it worked very well, but now I get the error "there is no segue with '---' identifier". I know there are many other threads with this, but none of the solutions there work for me. Moreover, I have tried to instantiate the ViewController programmatically, but this does not work either because I get the "unexpectedly found nil..." and I don't know what to do any more.
I know I am doing something wrong, most probably how I call these functions, but I don't know what. This is what I have tried until now:
in the .xib file I have this:
import UIKit
class MarkerInfoView: MKAnnotationView {
#IBOutlet weak var theButton: UIButton!
#IBAction func readIt(sender: AnyObject) {
ViewController.goToArticles()
}
}
and in the ViewController:
class func goToArticles(){
ViewController().reallyGoToArticles()
}
I did this because I could not find another way to be able to call performSegueWithIdentifier or presentViewController
func reallyGoToArticles(){
println("let's go!")
let theArticlesSB = UIStoryboard(name: "Main", bundle: nil)
let vc = theArticlesSB.instantiateViewControllerWithIdentifier("theArticles") as! articlesViewController
self.presentViewController(vc, animated: true, completion: nil)
self.performSegueWithIdentifier("showArticles", sender: self)
}
I have uncommented both options just to show you.
I have uploaded the project here
Thank you very much!
Update
I forgot to mention that if I put the line
self.performSegueWithIdentifier("showArticles", sender: self)
in viewDidLoad it works
I had a look at your project and found your issue.
In MarkerInfoView.swift, you call ViewController.goToArticles() which is a class function:
class func goToArticles(){
ViewController().reallyGoToArticles()
}
This class function creates a NEW instance of ViewController which has nothing to do with the storyboard (and is not aware of segues).
You have to call self.reallyGoToArticles() from an instance method like
func goToArticles(){
self.reallyGoToArticles()
}
You have to manage to call the existing ViewController from your MarkerInfoView
EDIT: Here is how to achieve it
MarkerInfoView.swift
class MarkerInfoView: MKAnnotationView {
var vc: ViewController!
#IBOutlet weak var placePhoto: UIImageView!
#IBOutlet weak var nameLabel: UILabel!
#IBOutlet weak var detailsLabel: UILabel!
#IBOutlet weak var theButton: UIButton!
#IBAction func readIt(sender: AnyObject) {
vc.goToArticles()
}
}
ViewController.swift
func mapView(mapView: MKMapView!, didSelectAnnotationView annotation: MKAnnotationView!)
{
if let pin = annotation.annotation as? CustomPointAnnotation{
if var infoView = UIView.viewFromNibName("MarkerInfoView") as? MarkerInfoView {
infoView.nameLabel.text = pin.theTitle
infoView.detailsLabel.text = pin.theDetails
infoView.vc = self
infoView.center = CGPointMake(self.view.bounds.width/2, self.view.bounds.height/2)
self.view.addSubview(infoView)
} else {
}
}
}
func goToArticles(){
self.reallyGoToArticles()
}