I am wondering if there is a way to get UIScrollview to scroll horizontally and vertically in storyboard instead of of just one direction?
Selecting any option will automatically load the page
Post
Replies
Boosts
Views
Activity
I'm trying to transfer a tableview cell from my hockeydetailVC to my favouritesVC using a button and I can't transfer it due to some issue.
My hockeydetailVC
protocol addThis {
func addFav(data: CurrentPlayers)
}
I'm trying to transfer my item object
var item: CurrentPlayers?	
var delegate: addThis? = nil	
func sendData() {
if delegate != nil {
let data = item!
delegate?.addFav(data: data)
print("This is the data being transferred: \(data)")
}
}
@IBAction func addToFav(_ sender: Any) {
sendData()
print("Favourite button Pressed")
}
My favouritesVC
var currentFav: CurrentPlayers?
func addFav(data: CurrentPlayers) {
currentFav = data
}
I have a segue for my button so I have this
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "r" {
let hockeyDetailVC: HockeyDetailVC = segue.destination as! HockeyDetailVC
hockeyDetailVC.delegate = self
}
}
If the transfer works then my sendData() method up top would print a statement but my addToFav only prints "favourite button pressed".
So I am using core data and I have to make my class Decodable to use the decoder for my get request. The errors I get are 'self' used in property access before 'super.init' call in my CoreDataClass each of my decode values has the error.
CoreDataProperties
extension CurrentPlayers {
@nonobjc public class func fetchRequest() - NSFetchRequestCurrentPlayers {
return NSFetchRequestCurrentPlayers(entityName: "CurrentPlayers")
}
@NSManaged public var photoUrl: String?
@NSManaged public var firstName: String?
@NSManaged public var lastName: String?
@NSManaged public var position: String?
@NSManaged public var team: String?
@NSManaged public var yahooName: String?
@NSManaged public var status: String?
@NSManaged public var jerseyNumber: Int64
}
extension CurrentPlayers : Identifiable {
}
CoreDataClass
@objc(CurrentPlayers)
public class CurrentPlayers: NSManagedObject, Decodable {
enum CodingKeys: String, CodingKey {
case photoUrl = "PhotoUrl"
case firstName = "FirstName"
case lastName = "LastName"
case position = "Position"
case team = "Team"
case yahooName = "YahooName"
case status = "Status"
case jerseyNumber = "JerseyNumber"
}
required public init(from decoder: Decoder) throws {
let values = try decoder.container(keyedBy: CodingKeys.self)
photoUrl = try values.decode(String.self, forKey: CodingKeys.photoUrl)
firstName = try values.decode(String.self, forKey: CodingKeys.firstName)
lastName = try values.decode(String.self, forKey: CodingKeys.lastName)
position = try values.decode(String.self, forKey: CodingKeys.position)
team = try values.decode(String.self, forKey: CodingKeys.team)
yahooName = try values.decode(String.self, forKey: CodingKeys.yahooName)
status = try values.decode(String.self, forKey: CodingKeys.status)
jerseyNumber = Int64(try values.decode(Int.self, forKey: CodingKeys.jerseyNumber))
}
}
I downloaded the pod "youtube-ios-player-helper", "~ 1.0.3" and minutes after importing it I get the error No such module 'youtube_ios_player_helper'. Did I install the pod correctly?
podfile
target 'LearnHockey' do
use_frameworks!
pod "youtube-ios-player-helper", "~ 1.0.3"
end
I'm trying to find a way to remove instances of empty names in my array. I was trying to find the indices to remove them but it does not print. I could use a little help.
func filterArr() {
for (index, player) in cPlayerArr.enumerated() {
if (player.yahooName == "") {
print("Found \(player) at \(index)")
}
}
}
extension CurrentPlayers {
@nonobjc public class func fetchRequest() - NSFetchRequestCurrentPlayers {
return NSFetchRequestCurrentPlayers(entityName: "CurrentPlayers")
}
@NSManaged public var photoUrl: String?
@NSManaged public var firstName: String?
@NSManaged public var lastName: String?
@NSManaged public var position: String?
@NSManaged public var team: String?
@NSManaged public var yahooName: String?
@NSManaged public var status: String?
@NSManaged public var jerseyNumber: Int64
}
extension CurrentPlayers : Identifiable {
}
I'm trying to find a way to remove instances with names as empty strings from my array that receives the data. how do I modify my filter to do that.
self.cPlayerArr = fillPlayers.filter(){
$0.yahooName != ""
}
func parseJSON(completed: @escaping () - ()) {
let url = URL(string: "")
let decoder = JSONDecoder()
let appDelegate = UIApplication.shared.delegate as! AppDelegate //- Intentionally using forced casting.
decoder.userInfo[.managedObjectContext] = appDelegate.persistentContainer.viewContext
URLSession.shared.dataTask(with: url!) { (data, response, error) in
if error == nil {
do {
let fillPlayers = try decoder.decode([CurrentPlayers].self, from: data!)
self.cPlayerArr = fillPlayers.filter(){
$0.yahooName != ""
}
let json = try JSONSerialization.jsonObject(with: data!, options: [])
print(json)
DispatchQueue.main.async {
completed()
}
} catch {
print("JSON Error: ", error)
}
}
}.resume()
}
extension CurrentPlayers {
@nonobjc public class func fetchRequest() - NSFetchRequestCurrentPlayers {
return NSFetchRequestCurrentPlayers(entityName: "CurrentPlayers")
}
@NSManaged public var photoUrl: String?
@NSManaged public var firstName: String?
@NSManaged public var lastName: String?
@NSManaged public var position: String?
@NSManaged public var team: String?
@NSManaged public var yahooName: String?
@NSManaged public var status: String?
@NSManaged public var jerseyNumber: Int64
}
extension CurrentPlayers : Identifiable {
}
I'm trying to pass data forward and I am using storyboard but I can't use a segue because it will dismiss my initial view controller. What technique should I do to pass it forward without pushing my initial viewcontroller? I tried using storyboardID to initialize the destination viewcontroller but that does not work either.
I'm trying to pass data forward from my HdVC to my FaVC with Notification Center using a button called addToFav and it does not seem to pass my data I call var item. I need a little help figuring it out a way to modify the code so my item can be passed and set to a variable currentFav I have in my FaVC.
HdVC
class HockeyDetailVC: UITableViewController {
let imageController = FetchImage()
var item: CurrentPlayers? /* This object value I want in FaVC */
override func viewDidLoad() {
super.viewDidLoad()
NotificationCenter.default.addObserver(self,
selector: #selector(handleFavNotification(notification:)),
name: .passFavNotification,
object: nil)
}
@objc func handleFavNotification(notification:Notification){
if let theFav = notification.object as? CurrentPlayers {
self.item = theFav
}
}
@IBAction func addToFav(_ sender: Any) {
let alert = UIAlertController(title: "Favourite Added 💙", message: "\(name.text ?? "") is added to favourites", preferredStyle: .alert)
alert.addAction(UIAlertAction(title: "OK", style: UIAlertAction.Style.default, handler: nil))
self.present(alert, animated: true, completion: nil)
print("Favourite button Pressed")
let passDataVC = FavouritesVC() /* I think passDataVC is supposed to be different because I use storyboard but I don't know what else it's supposed to be. */
self.present(passDataVC, animated: true, completion:nil)
}
FaVC
class FavouritesVC: UITableViewController {
var currentFav: CurrentPlayers? /*this is the variable I want to set my item too. */
var favArr = [CurrentPlayers]()
override func viewDidLoad() {
super.viewDidLoad()
if currentFav == nil {
self.tableView.separatorStyle = UITableViewCell.SeparatorStyle.none
} else {
NotificationCenter.default.post(name: .passFavNotification,
object: self.currentFav)
favArr.append(currentFav!)
print(currentFav!)
}
}
I'm trying to figure out how to add a searchbar and a scope as a header to my UIViewController. The issue is I don't know the code to add it as a header.
func customizeSearch() {
let searchBar = UISearchBar(frame: CGRect(x:0, y:0, width: (UIScreen.main.bounds.width), height: 70))
searchBar.backgroundColor = UIColor.black
searchBar.delegate = self
searchBar.scopeButtonTitles = ["All", "Forwards", "Defensemen", "Goalies"]
//self.tableView.tableHeaderView = searchBar if I had a tableview, I have a vc.
}
I'm trying to use CoreData to save my favourites so that they will be accessed when the program terminates but I am not sure how to make my object persistent to do that.
@IBAction func addToFav(_ sender: Any) {
let alert = UIAlertController(title: "Favourite Added 💙", message: "\(name.text ?? "") is added to favourites", preferredStyle: .alert)
alert.addAction(UIAlertAction(
title: "OK",
style: UIAlertAction.Style.default)
{ _ in
if let favPlayer = self.item /*this needs to be persistent*/ {
FavouriteManager.shared.add(favPlayer)
PersistenceService.saveContext()
}
})
self.present(alert, animated: true, completion: nil)
print("Favourite button Pressed")
}
I have a PersistenceService class I can more easily access my CoreData values.
class PersistenceService {
private init() {}
static var context: NSManagedObjectContext {
return persistentContainer.viewContext
}
// MARK: CoreData
static var persistentContainer: NSPersistentContainer = {
let container = NSPersistentContainer(name: "playerModel")
container.loadPersistentStores(completionHandler: { (storeDescription, error) in
if let error = error as NSError? {
// Replace this implementation with code to handle the error appropriately.
// fatalError() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development.
fatalError("Unresolved error \(error), \(error.userInfo)")
}
})
return container
}()
static func saveContext () {
let context = persistentContainer.viewContext
if context.hasChanges {
do {
try context.save()
} catch {
// Replace this implementation with code to handle the error appropriately.
// fatalError() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development.
let nserror = error as NSError
fatalError("Unresolved error \(nserror), \(nserror.userInfo)")
}
}
}
}
I'm trying to filter my players using searchBar with scope in a collectionview using a switch statement and my case 0 works but the issue is my other cases I can type into the searchBar but I can't seem to filter (nothing shows up) nor can I delete all the characters when I type.
I have 2 arrays my cPlayerArr is my original array when data is recieved and my allTextArr is my filtered array when any kind of filtering is done.
func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) {
allTextArr = cPlayerArr.filter({ player - Bool in
switch searchBar.selectedScopeButtonIndex {
case 0:
if searchText.isEmpty {
fetchAllPlayers()
} else {
return
player.yahooName!.lowercased().contains(searchText.lowercased())
}
case 1:
if searchText.isEmpty {
fetchForwards()
} else {
print(searchText)
return player.yahooName!.lowercased().contains(searchText.lowercased()) && player.position == "C" &&
player.position == "RW" && player.position == "LW"
}
case 2:
if searchText.isEmpty {
fetchDefense()
} else {
return player.yahooName!.lowercased().contains(searchText.lowercased()) && player.position == "D"
}
case 3:
if searchText.isEmpty {
fetchGoalies()
} else {
return player.yahooName!.lowercased().contains(searchText.lowercased()) && player.position == "G"
}
default:
return false
}
return true
})
collections.reloadData()
}
func searchBar(_ searchBar: UISearchBar, selectedScopeButtonIndexDidChange: Int) {
if (searchBar.selectedScopeButtonIndex == 0) {
cPlayerArr.removeAll()
allTextArr.removeAll()
fetchAllPlayers()
} else if (searchBar.selectedScopeButtonIndex == 1) {
allTextArr.removeAll()
fetchForwards()
} else if (searchBar.selectedScopeButtonIndex == 2) {
allTextArr.removeAll()
fetchDefense()
} else {
allTextArr.removeAll()
fetchGoalies()
}
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) - Int {
if (searchBar.text != "" || searchBar.selectedScopeButtonIndex 0) {
return allTextArr.count
} else {
return cPlayerArr.count
}
}
I have an issue with my SearchBar with scope working on my emulator but not on my iPhone properly. When I type the name of a specific player the iPhone shows me duplicates of the player but my emulator does not. What causes this issue? (I made the scoped bar programmatically but I don't see how that could cause an issue).
I have an issue where the emulator behaves differently then the actual iPhone when running my Xcode app, (it has nothing to do with email). What could be the issue?
I have an alert that I want to go to a specific site but after the user presses okay and for it to show only once. I can't figure out the part where the site is delayed because the user did not press okay.
var justOnce: Bool = true
@IBAction func playStats(_ sender: Any) {
//show alert only once
if justOnce {
let alert = UIAlertController(title: "Stats Important", message: "You can copy and paste the name here when you see the searchbar", preferredStyle: .alert)
alert.addAction(UIAlertAction(title: "OK", style: UIAlertAction.Style.default, handler: nil))
self.present(alert, animated: true, completion: nil)
justOnce = false
gotoSite()
}
}
I am not sure how to transfer data a name from a button in my UITableViewCell to a UIViewController. I have a prepare function but it doesn't work gives me an unrecognized selector error. Is there a way to use it for this instance?
func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "hs" {
let destinationController = segue.destination as! HighlightsVC
destinationController.playerName = (item.yahooName!)
}
}