Swift UITableView Controller error: Type "ViewController" does not conform to protocol "UITableViewDataSource" - uitableviewsectionheader

import UIKit
class ViewController:UIViewController {
var tableView:UITableView?
override func viewDidLoad() {
super.viewDidLoad()
tableView = UITableView(frame:view.bounds,style: .Plain)
if let theTableView = tableView {
theTableView.registerClass(UITableViewCell.classForCoder(),forCellReuseIdentifier:"identifier")
theTableView.dataSource = self
theTableView.autoresizingMask = .FlexibleWidth | .FlexibleHeight
view.addSubview(theTableView)
}
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
}
"theTableView.dataSource = self" error message Type "ViewController" does not conform to protocol "UITableViewDataSource"

You need to declare at the top that you're implementing the data source and delegate protocol, and then you need to actually implement those methods (at least numberOfRowsInSection and cellForRowAtIndexPath).
class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate

Related

NSUnknownKeyException on a model variable

'[ setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key text.'
I'm getting an NSUnknownKeyException at the text variable of my Message model when I attempt to access it in my second tableView() function (at the end of this code). I am still pretty new to swift and don't know how to resolve this. Through my google searches, I found that it could be something to do with storyboards, but I've actually opted to create my UI programmatically so I'm not sure how that could be causing this.
import UIKit
import Firebase
class ViewController: UITableViewController {
override func viewDidLoad() {
super.viewDidLoad()
navigationItem.leftBarButtonItem = UIBarButtonItem(title: "chat", style: .plain, target: self, action: #selector(handleChat))
observeMessages()
}
#objc func handleChat() {
let chatController = ChatController(collectionViewLayout: UICollectionViewLayout())
navigationController?.pushViewController(chatController, animated: true)
//present(chatController, animated: true, completion: nil)
}
var messages = [Message]()
func observeMessages() {
let ref = Database.database().reference().child("FalconsVPackers").child("messages")
ref.observe(.childAdded, with: { (snapshot) in
if let dictionary = snapshot.value as? [String: AnyObject] {
let message = Message()
message.setValuesForKeys(dictionary)
self.messages.append(message)
DispatchQueue.main.async {
self.tableView.reloadData()
}
}
}, withCancel: nil)
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return messages.count
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = UITableViewCell(style: .subtitle, reuseIdentifier: "called")
let message = messages[indexPath.row]
cell.textLabel?.text = message.text
return cell
}
}
My Message model is very simple...
import UIKit
class Message: NSObject {
var text: String?
}
Any help or advice would be greatly appreciated!
You are using NSKeyValueCoding to populate Message object so you must add #objc to text property.
message.setValuesForKeys(dictionary) // NSKeyValueCoding
.
class Message : NSObject {
#objc var text: String?
}
If you just want to populate your message object, better just use properties directly instead of setValuesForKeys. And in this case you don't have to use #objc too.
let message = Message()
message.text = dictionary["text"]

shouldAutorotate not working with navigation controllar swift 2

my app uses several libraryies. Bassically KYDrawerController. I want my App to use only potraite Orientation but for one ViewControllar i need both landscape and Potrait.
I have search about allmost every solution on the internet. every solution was to subclass the NavigationControllar and Override ShouldAutoRotate method. but non of them worked for me.
and here is the closer View of the entire StoryBoard
the gray color View is the KYDrawerControllar view which is an libry uses to work as NavigationDrawer like Android.
I have created a custom Class for Navigation Controllar and subclass it to the requierd ViewControllar's Navigation Controllar.
here is the Code
class SettingsNavigation: UINavigationController {
override func viewDidLoad() {
super.viewDidLoad()
NSLog("visibleControllar", self.visibleViewController!.debugDescription)
// Do any additional setup after loading the view.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
override func shouldAutorotate() -> Bool {
//return self.visibleViewController!.shouldAutorotate()
return false
}
override func supportedInterfaceOrientations() -> UIInterfaceOrientationMask {
return UIInterfaceOrientationMask.Portrait
}
override func preferredInterfaceOrientationForPresentation() -> UIInterfaceOrientation {
return UIInterfaceOrientation.Portrait
}
/*
// MARK: - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
// Get the new view controller using segue.destinationViewController.
// Pass the selected object to the new view controller.
}
*/
}
here is the ViewControllar
class Settings: UIViewController {
// MARK: - Outlets
#IBOutlet var profileImage: UIImageView!
#IBOutlet var profileName: UILabel!
#IBOutlet var profileRegistrationType: UILabel!
#IBOutlet var logOutButton: FUIButton!
#IBOutlet var subscriptionType: UILabel!
#IBOutlet var subscriptionRegistrationType: UILabel!
#IBOutlet var upgradeSubscriptionButton: FUIButton!
override func shouldAutorotate() -> Bool {
/* if (UIDevice.currentDevice().orientation == UIDeviceOrientation.LandscapeLeft ||
UIDevice.currentDevice().orientation == UIDeviceOrientation.LandscapeRight ||
UIDevice.currentDevice().orientation == UIDeviceOrientation.Unknown) {
return false;
}
else {
return true;
}*/
return false
}
override func supportedInterfaceOrientations() -> UIInterfaceOrientationMask {
return UIInterfaceOrientationMask.Portrait
} }
and i am using StoryBoard segues to present ViewControllars.
please someone help me with this.
Here is my way:
in your appdelegae.swift:
var shouldSupportAllOrientation = false
func application(application: UIApplication, supportedInterfaceOrientationsForWindow window: UIWindow?) -> UIInterfaceOrientationMask {
if (shouldSupportAllOrientation == true){
return UIInterfaceOrientationMask.All
}
return UIInterfaceOrientationMask.Portrait
}
in your entry view which enter the all orientation view (change to support all orientation, here I use a button as an example):
#IBAction func next(sender: UIButton) {
let appdelegate = UIApplication.sharedApplication().delegate as! AppDelegate
appdelegate.shouldSupportAllOrientation = true
self.performSegueWithIdentifier("next", sender: self)
}
in your entry view which enter the all orientation view (change the orientation to support only Portrait):
override func viewWillAppear(animated: Bool) {
super.viewWillAppear(animated)
let appdelegate = UIApplication.sharedApplication().delegate as! AppDelegate
appdelegate.shouldSupportAllOrientation = false
}
finally, you may find this works on all iPhone device and iPad ipad2 except iPad air iPad pro; you should check the "requires full screen" in your project general info to ensure you all orientation view can enter landscape.

ViewController does not conform to protocol MSBandClientManagerDelegate

I am trying to use Swift to implement the Microsoft Band SDK. I keep getting this error when trying to set up my code.
class ViewController: UIViewController, UITableViewDelegate, UIImagePickerControllerDelegate, UINavigationControllerDelegate, MSBClientManagerDelegate, UIScrollViewDelegate {
I have never seen this before, but I have also never tried to convert an Objective C sample to Swift.
Any help would be appreciated!
EDIT: Here is the protocol from Objective C
#protocol MSBClientManagerDelegate<NSObject>
- (void)clientManager:(MSBClientManager *)clientManager clientDidConnect:(MSBClient *)client;
- (void)clientManager:(MSBClientManager *)clientManager clientDidDisconnect:(MSBClient *)client;
- (void)clientManager:(MSBClientManager *)clientManager client:(MSBClient *)client didFailToConnectWithError:(NSError *)error;
#end
EDIT 2: After using suggested Swift Helper class
This is how I am trying to set up the connection.
var clients:NSArray = bandHelper.attachedClients()!
var firstClient: MSBClient = clients[0] as MSBClient
if (clients.count == 0){
println("The band is not detected")
return
}
I have no clue how this should be set up
bandHelper.connectClient(firstClient, {completion: (connected:true -> void in)})
println("Please wait...connecting to band")
Then, when trying to send a photo to the band, this function does not work
bandHelper.client?.personalizationManager.updateMeTileImage(bandScaledImage, { (completionHandler: NSError!) -> Void in
NSLog("%#", NSError())})
I am getting thrown off by using the helper class. Any help would be appreciated!
Sample Project
I linked a sample Swift project for Microsoft Band Kit iOS that can send a haptic to the band. Find the link here: http://droolfactory.blogspot.com/2015/03/ios-swift-example-of-connecting-with.html
Microsoft Band Bridging Header
First to convert the Objective-C classes to be used with Swift, create a Bridging Header. Mine look like this for just the MicrosoftBandKit-iOS framework:
#ifndef ModuleName_Bridging_Header_h
#define ModuleName_Bridging_Header_h
#import <MicrosoftBandKit_iOS/MicrosoftBandKit_iOS.h>
#endif
Make sure to replace the ModuleName with the name of your apps Module. Find more on Bridging Header files at: https://developer.apple.com/library/ios/documentation/Swift/Conceptual/BuildingCocoaApps/MixandMatch.html
Band Helper Class
Next I wrapped the MSBClientManagerDelegate in a helper class (BandManager) which uses a singleton to manage the Band. I have a gist for it here (https://gist.github.com/mthistle/8f6eb30c68a918fc6240)
The code for this the gist is:
import Foundation
let kConnectionChangedNotification = "kConnectionChangedNotification"
let kConnectionFailedNotification = "kConnectionFailedNotification"
private let _SharedBandManagerInstance = BandManager()
class BandManager : NSObject, MSBClientManagerDelegate {
private(set) var client: MSBClient?
private var connectionBlock: ((Bool) -> ())?
private var discoveredClients = [MSBClient]()
private var clientManager = MSBClientManager.sharedManager()
class var sharedInstance: BandManager {
return _SharedBandManagerInstance
}
override init() {
super.init()
self.clientManager.delegate = self
}
func attachedClients() -> [MSBClient]? {
if let manager = self.clientManager {
self.discoveredClients = [MSBClient]()
for client in manager.attachedClients() {
self.discoveredClients.append(client as! MSBClient)
}
}
return self.discoveredClients
}
func disconnectClient(client: MSBClient) {
if (!client.isDeviceConnected) {
return;
}
if let manager = self.clientManager {
manager.cancelClientConnection(client)
self.client = nil
}
}
func connectClient(client: MSBClient, completion: (connected: Bool) -> Void) {
if (client.isDeviceConnected && self.client == client) {
if (self.connectionBlock != nil)
{
self.connectionBlock!(true)
}
return;
}
if let connectedClient = self.client {
self.disconnectClient(client)
}
self.connectionBlock = completion;
self.clientManager.connectClient(client)
}
func clientManager(clientManager: MSBClientManager!, clientDidConnect client: MSBClient!) {
if (self.connectionBlock != nil) {
self.client = client
self.connectionBlock!(true)
self.connectionBlock = nil
}
self.fireClientChangeNotification(client)
}
func clientManager(clientManager: MSBClientManager!, clientDidDisconnect client: MSBClient!) {
self.fireClientChangeNotification(client)
}
func clientManager(clientManager: MSBClientManager!, client: MSBClient!, didFailToConnectWithError error: NSError!) {
if error != nil {
println(error)
}
NSNotificationCenter.defaultCenter().postNotificationName(kConnectionFailedNotification, object: self, userInfo: ["client": client])
}
func fireClientChangeNotification(client: MSBClient) {
NSNotificationCenter.defaultCenter().postNotificationName(kConnectionChangedNotification, object: self, userInfo: ["client": client])
}
}

Adding observer for KVO without pointers using Swift

In Objective-C, I would normally use something like this:
static NSString *kViewTransformChanged = #"view transform changed";
// or
static const void *kViewTransformChanged = &kViewTransformChanged;
[clearContentView addObserver:self
forKeyPath:#"transform"
options:NSKeyValueObservingOptionNew
context:&kViewTransformChanged];
I have two overloaded methods to choose from to add an observer for KVO with the only difference being the context argument:
clearContentView.addObserver(observer: NSObject?, forKeyPath: String?, options: NSKeyValueObservingOptions, context: CMutableVoidPointer)
clearContentView.addObserver(observer: NSObject?, forKeyPath: String?, options: NSKeyValueObservingOptions, kvoContext: KVOContext)
With Swift not using pointers, I'm not sure how to dereference a pointer to use the first method.
If I create my own KVOContext constant for use with the second method, I wind up with it asking for this:
let test:KVOContext = KVOContext.fromVoidContext(context: CMutableVoidPointer)
EDIT: What is the difference between CMutableVoidPointer and KVOContext? Can someone give me an example how how to use them both and when I would use one over the other?
EDIT #2: A dev at Apple just posted this to the forums: KVOContext is going away; using a global reference as your context is the way to go right now.
There is now a technique officially recommended in the documentation, which is to create a private mutable variable and use its address as the context.
(Updated for Swift 3 on 2017-01-09)
// Set up non-zero-sized storage. We don't intend to mutate this variable,
// but it needs to be `var` so we can pass its address in as UnsafeMutablePointer.
private static var myContext = 0
// NOTE: `static` is not necessary if you want it to be a global variable
observee.addObserver(self, forKeyPath: …, options: [], context: &MyClass.myContext)
override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey: Any]?, context: UnsafeMutableRawPointer?) {
if context == &myContext {
…
}
else {
super.observeValue(forKeyPath: keyPath, of: object, change: change, context: context)
}
}
Now that KVOContext is gone in Xcode 6 beta 3, you can do the following. Define a global (i.e. not a class property) like so:
let myContext = UnsafePointer<()>()
Add an observer:
observee.addObserver(observer, forKeyPath: …, options: nil, context: myContext)
In the observer:
override func observeValueForKeyPath(keyPath: String!, ofObject object: AnyObject!, change: [NSObject : AnyObject]!, context: UnsafePointer<()>) {
if context == myContext {
…
} else {
super.observeValueForKeyPath(keyPath, ofObject: object, change: change, context: context)
}
}
Swift 4 - observing contentSize change on UITableViewController popover to fix incorrect size
I had been searching for an answer to change to a block based KVO because I was getting a swiftlint warning and it took me piecing quite a few different answers together to get to the right solution. Swiftlint warning:
Block Based KVO Violation: Prefer the new block based KVO API with keypaths when using Swift 3.2 or later. (block_based_kvo).
My use case was to present a popover controller attached to a button in a Nav bar in a view controller and then resize the popover once it's showing - otherwise it would be too big and not fitting the contents of the popover. The popover itself was a UITableViewController that contained static cells, and it was displayed via a Storyboard segue with style popover.
To setup the block based observer, you need the following code inside your popover UITableViewController:
// class level variable to store the statusObserver
private var statusObserver: NSKeyValueObservation?
// Create the observer inside viewWillAppear
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
statusObserver = tableView.observe(\UITableView.contentSize,
changeHandler: { [ weak self ] (theTableView, _) in self?.popoverPresentationController?.presentedViewController.preferredContentSize = theTableView.contentSize
})
}
// Don't forget to remove the observer when the popover is dismissed.
override func viewDidDisappear(_ animated: Bool) {
if let observer = statusObserver {
observer.invalidate()
statusObserver = nil
}
super.viewDidDisappear(animated)
}
I didn't need the previous value when the observer was triggered, so left out the options: [.new, .old] when creating the observer.
Update for Swift 4
Context is not required for block-based observer function and existing #keyPath() syntax is replaced with smart keypath to achieve swift type safety.
class EventOvserverDemo {
var statusObserver:NSKeyValueObservation?
var objectToObserve:UIView?
func registerAddObserver() -> Void {
statusObserver = objectToObserve?.observe(\UIView.tag, options: [.new, .old], changeHandler: {[weak self] (player, change) in
if let tag = change.newValue {
// observed changed value and do the task here on change.
}
})
}
func unregisterObserver() -> Void {
if let sObserver = statusObserver {
sObserver.invalidate()
statusObserver = nil
}
}
}
Complete example using Swift:
//
// AppDelegate.swift
// Photos-MediaFramework-swift
//
// Created by Phurg on 11/11/16.
//
// Displays URLs for all photos in Photos Library
//
// #see http://stackoverflow.com/questions/30144547/programmatic-access-to-the-photos-library-on-mac-os-x-photokit-photos-framewo
//
import Cocoa
import MediaLibrary
// For KVO: https://developer.apple.com/library/content/documentation/Swift/Conceptual/BuildingCocoaApps/AdoptingCocoaDesignPatterns.html#//apple_ref/doc/uid/TP40014216-CH7-ID12
private var mediaLibraryLoaded = 1
private var rootMediaGroupLoaded = 2
private var mediaObjectsLoaded = 3
#NSApplicationMain
class AppDelegate: NSObject, NSApplicationDelegate {
#IBOutlet weak var window: NSWindow!
var mediaLibrary : MLMediaLibrary!
var allPhotosAlbum : MLMediaGroup!
func applicationDidFinishLaunching(_ aNotification: Notification) {
NSLog("applicationDidFinishLaunching:");
let options:[String:Any] = [
MLMediaLoadSourceTypesKey: MLMediaSourceType.image.rawValue, // Can't be Swift enum
MLMediaLoadIncludeSourcesKey: [MLMediaSourcePhotosIdentifier], // Array
]
self.mediaLibrary = MLMediaLibrary(options:options)
NSLog("applicationDidFinishLaunching: mediaLibrary=%#", self.mediaLibrary);
self.mediaLibrary.addObserver(self, forKeyPath:"mediaSources", options:[], context:&mediaLibraryLoaded)
NSLog("applicationDidFinishLaunching: added mediaSources observer");
// Force load
self.mediaLibrary.mediaSources?[MLMediaSourcePhotosIdentifier]
NSLog("applicationDidFinishLaunching: done");
}
override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) {
NSLog("observeValue: keyPath=%#", keyPath!)
let mediaSource:MLMediaSource = self.mediaLibrary.mediaSources![MLMediaSourcePhotosIdentifier]!
if (context == &mediaLibraryLoaded) {
NSLog("observeValue: mediaLibraryLoaded")
mediaSource.addObserver(self, forKeyPath:"rootMediaGroup", options:[], context:&rootMediaGroupLoaded)
// Force load
mediaSource.rootMediaGroup
} else if (context == &rootMediaGroupLoaded) {
NSLog("observeValue: rootMediaGroupLoaded")
let albums:MLMediaGroup = mediaSource.mediaGroup(forIdentifier:"TopLevelAlbums")!
for album in albums.childGroups! {
let albumIdentifier:String = album.attributes["identifier"] as! String
if (albumIdentifier == "allPhotosAlbum") {
self.allPhotosAlbum = album
album.addObserver(self, forKeyPath:"mediaObjects", options:[], context:&mediaObjectsLoaded)
// Force load
album.mediaObjects
}
}
} else if (context == &mediaObjectsLoaded) {
NSLog("observeValue: mediaObjectsLoaded")
let mediaObjects:[MLMediaObject] = self.allPhotosAlbum.mediaObjects!
for mediaObject in mediaObjects {
let url:URL? = mediaObject.url
// URL does not extend NSObject, so can't be passed to NSLog; use string interpolation
NSLog("%#", "\(url)")
}
}
}
}

how to capture camera with UIImagePickerController in swift?

I'm trying use UIImagePickerController in swift but isn't work...
my ViewController:
class ViewController: UIViewController {
#IBOutlet var imag : UIView = nil
#IBAction func capture(sender : UIButton) {
println("Button capture")
if UIImagePickerController.isSourceTypeAvailable(UIImagePickerControllerSourceType.Camera)
{
var imag = UIImagePickerController()
imag.delegate = self
imag.sourceType = UIImagePickerControllerSourceType.Camera;
imag.mediaTypes = kUTTypeImage
imag.allowsEditing = false
self.presentViewController(imag, animated: true, completion: nil)
}
}
}
I have errors in following line of code
imag.delegate = self
(Type'ViewControlles does confoorm to protocol 'UIImagePickerControllerDelegate')
imagePicker.mediaTypes = kUTTypeImage
(use of unresolved identifier kUTTypeImage)
I have read that kUTTypeImage cant use in swift.but don't know, i am using bad this functions. Any help?
Thanks!!
You should also import MobileCoreServices in the controller:
import MobileCoreServices
and then put the type inside square brackets like this:
image.mediaTypes = [kUTTypeImage]
Swift 2.0 and Higher
image.mediaTypes = [kUTTypeImage as String]
Swift 2.0
In Swift 2.0 (Xcode 7), you need to explicitly cast kUTTypeImage (a CFString) to String:
picker.mediaTypes = [kUTTypeImage as String]
And you still need to import Mobile Core Services for this symbol to be defined:
import MobileCoreServices
That said, the default value of mediaTypes is [kUTTypeImage] anyway, so you don't need to set it if that's what you want.
also you should add UINavigationControllerDelegate to the protocols list of the ViewController
and one of the optional delegate functions (if you a planing to get a picture)
This is the working code for your issue:
import UIKit
import MobileCoreServices
class ViewController: UIViewController, UINavigationControllerDelegate, UIImagePickerControllerDelegate {
func imagePickerController(picker: UIImagePickerController!, didFinishPickingImage image: UIImage!, editingInfo: NSDictionary!){
println("i've got an image");
}
#IBAction func capture(sender : UIButton) {
if UIImagePickerController.isSourceTypeAvailable(UIImagePickerControllerSourceType.Camera){
println("Button capture")
var imag = UIImagePickerController()
imag.delegate = self
imag.sourceType = UIImagePickerControllerSourceType.Camera;
imag.mediaTypes = [kUTTypeImage]
imag.allowsEditing = false
self.presentViewController(imag, animated: true, completion: nil)
}
}
}
From Your Piece of code its very clear that you are making mistakes at two place one is setting delegate and second is setting Media type imag.mediaTypes = kUTTypeImage
First One:If you look into the delegate definition of UIImagePickerController it requires to confirm two protocol UINavigationControllerDelegate and UIImagePickerControllerDelegate so you have to adopt these two protocols in your viewcontroller class like as
class ViewController: UIViewController,UINavigationControllerDelegate, UIImagePickerControllerDelegate
second error:If you look into the definition part of mediaTypes it clearly requires array of media types to passed so do like this
imag.mediaTypes = [kUTTypeImage]
Apart from this, I have written a very descent class for the same task
It is easy to understand and integrate.
Here you go
//Declare property
var imagePicker:ImageVideoPicker?
//Call below line of code properly, it will return an image
self.imagePicker = ImageVideoPicker(frame: self.view.frame, superVC: self) { (capturedImage) -> Void in
if let captureImage = capturedImage{
//you did it.....
}
}
You have to conform to the delegate like this
class ViewController: UIViewController, UIImagePickerControllerDelegate
Per documentation, by default the media types is set to image so you can go ahead and delete that line since you are only setting it to image.
Do not forget to implement the protocol methods which are outlined in the documentation:
documentation
Try this
import UIKit
import AVFoundation
class ViewController: UIViewController, UINavigationControllerDelegate, UIImagePickerControllerDelegate {
var imagePicker: UIImagePickerController!
#IBOutlet weak var ImageView: UIImageView!
override func viewDidLoad() {
super.viewDidLoad()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
#IBAction func takeImage(sender: AnyObject) {
imagePicker = UIImagePickerController()
imagePicker.delegate = self
imagePicker.sourceType = .Camera
presentViewController(imagePicker, animated: true, completion: nil)
}
func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject]) {
imagePicker.dismissViewControllerAnimated(true, completion: nil)
ImageView.image = info[UIImagePickerControllerOriginalImage] as? UIImage
}
}
swift 1.2 syntax:
func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [NSObject : AnyObject]) {
let image = info[UIImagePickerControllerOriginalImage]
}

Resources