I created a pod library in here
Library contains a protocol. When I install library from cocoapods and try to conform the protocol I got delegate is inaccessible due to 'internal' protection level error.
The protocol:
#objc public protocol TDDropdownListDelegate {
func listTapped(sender: UIButton)
}
The protocol function:
open func listTapped(sender: UIButton) {
activate(sender: sender)
delegate?.listTapped(sender: sender)
}
The code I got the error
import UIKit
import TDDropdownList
class ViewController: UIViewController,TDDropdownListDelegate {
let a = TDDropdownList(frame: CGRect(x: 100 , y: 100 , width: 200 , height: 75))
override func viewDidLoad() {
a.initialize(data: ["Emre","Duman"])
a.delegate = self // LINE I GOT THE ERROR
self.view.addSubview(a)
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
func listTapped(sender: UIButton) {
print(sender.currentTitle!)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
Related
I am trying to wrap my website inside a WKWebView but an anchor with href="tel:312133" is not showing any action when clicked on it.
import UIKit
import WebKit
class ViewController: UIViewController {
#IBOutlet var webView: WKWebView!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
let mUrl = URL(string: "https://www.example.com")
let mRequest = URLRequest(url: mUrl!)
webView.load(mRequest)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
I am using protocol for call method but my method does not call.Is there is any example which i use.
Here is my code:
ViewController
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
extension ViewController :ViewController1Delegate
{
func hello()
{
println("hbgyguyg");
}
}
In View Controller 1
import UIKit
#objc
protocol ViewController1Delegate
{
optional func hello()
}
class ViewController1: UIViewController {
var delegate: ViewController?
override func viewDidLoad() {
super.viewDidLoad()
delegate?.hello()
}
}
Please Help, I am new in Swift.Any help would be apperciated. Thanks in Advance
An example demo.
ViewController file
import UIKit
class ViewController: UIViewController, PopUpViewControllerDelegate
{
var popupVC: PopUpViewController!;
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
self.view!.backgroundColor = UIColor.whiteColor();
self.popupVC = PopUpViewController();
self.popupVC.delegate = self;
self.showPopUpVC();
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func popUpViewControllerDidPressOK(popUpVC: PopUpViewController) {
println("Yay?");
self.closePopUpVC();
}
func showPopUpVC()
{
let delayTime = dispatch_time(DISPATCH_TIME_NOW,
Int64(1.0 * Double(NSEC_PER_SEC)))
dispatch_after(delayTime, dispatch_get_main_queue()) {
self .presentViewController(self.popupVC, animated: true, completion: nil);
}
}
func closePopUpVC()
{
let delayTime = dispatch_time(DISPATCH_TIME_NOW,
Int64(1.0 * Double(NSEC_PER_SEC)))
dispatch_after(delayTime, dispatch_get_main_queue()) {
self.dismissViewControllerAnimated(true, completion: nil);
}
}
}
PopUpViewController file
import UIKit
protocol PopUpViewControllerDelegate
{
func popUpViewControllerDidPressOK(popUpVC: PopUpViewController);
}
class PopUpViewController: UIViewController {
var delegate: PopUpViewControllerDelegate!;
override func viewDidLoad() {
super.viewDidLoad()
self.view!.backgroundColor = UIColor.redColor();
// Do any additional setup after loading the view.
self.delegate!.popUpViewControllerDidPressOK(self);
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
Notice how in my ViewController viewDidLoad() method, I have a line that initialises the popUp view controller and then set its delegate to be the ViewController itself:
self.popupVC = PopUpViewController();
self.popupVC.delegate = self; // you're missing this line I believe ?
I don't use Interface Builder or Storyboard but maybe select your VC1 in your Storyboard and look in the connections inspector to see if you drag a line from "delegate" to your ViewController file owner thing.
In the end, you should see a red screen popup after 1 second, followed by the word "Yay?" logged into your Xcode console and finally the popupVC dismisses.
I have an array of buttons and at the beginning of the program I want the user to pick one button and then it'll change its background.
I've written a switch-statement but I don't know how can I implement it into override func viewDidLoad(), namely, I have no idea what should I write as a parameter instead of UIButton
class ViewController: UIViewController {
#IBOutlet var cardsArray: Array<UIButton> = []
override func viewDidLoad() {
super.viewDidLoad()
self.playerMove(sender: UIButton)
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
#IBAction func playerMove(sender: UIButton) {
switch (sender) {
case self.cardsArray[0]:
self.cardPressedAll(0)
case self.cardsArray[1]:
self.cardPressedAll(1)
case self.cardsArray[2]:
self.cardPressedAll(2)
default: break
}
}
func cardPressedAll (cardNumber: Int) {
self.cardsArray[cardNumber].enabled = false
self.cardsArray[cardNumber].setBackgroundImage(UIImage(named: "cross"), forState: UIControlState.Normal)
}
}
Instead of calling 'self.playerMove(sender: UIButton)' in your view did load you need to connect your buttons to your IBAction using storyboard.
I'm new in swift and I'm studying the delegate and protocols. In my test application I'm using this protocol declaration (in ViewController1) and I have this code:
import UIKit
//Protocol declaration
protocol viewController1Delegate
{
func didFinish(controller:ViewController1,text:String)
}
//ViewController1 class
class ViewController1: UIViewController {
//delegate declaration for viewController1
var delegate:ViewController1? = nil
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
valore.text=valoreInput
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
#IBAction func doneWithOK(sender: AnyObject) {
delegate?.didFinish(self,"done")
}
}
My problem is XCode show me an error: ViewController1 does not have a member named didFinisch. What is wrong in my code? Someone can help me to understand the problem?
delegate should be viewController1Delegate? and not ViewController1?.
I am Created 2 Views, One is and Used Protocol and Delegate. For first view the Delegate function is not called.
My FirstView Controller : Here I am Accessing the Delegate Function.
import UIKit
class NextViewController: UIViewController,DurationSelectDelegate {
//var secondController: DurationDel?
var secondController: DurationDel = DurationDel()
#IBAction func Next(sender : AnyObject)
{
let nextViewController = DurationDel(nibName: "DurationDel", bundle: nil)
self.navigationController.pushViewController(nextViewController, animated: true)
}
override func viewDidLoad() {
super.viewDidLoad()
secondController.delegate=self
}
func DurationSelected() {
println("SUCCESS")
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
My SecondView Controller : Here I Am creating Delegate.
import UIKit
protocol DurationSelectDelegate {
func DurationSelected()
}
class DurationDel: UIViewController {
var delegate: DurationSelectDelegate?
#IBAction func Previous(sender : AnyObject) {
//let game = DurationSelectDelegate()
delegate?.DurationSelected()
self.navigationController.popViewControllerAnimated(true)
}
override func viewDidLoad() {
super.viewDidLoad()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
To me, it looks like you're pushing a view controller that you haven't actually set the delegate for. If you change your "Next" function, to include the line
nextViewController.delegate = self
You should see that the delegation works. In doing this, you can also probably remove the creation of "secondController", as it looks like that's redundant.
The naming convention you have followed would confuse fellow developers in your team. The instance should have been
let durationDel = DurationDel(nibName: "DurationDel", bundle: nil)
And then as #Eagerod mentioned, the delegate you would set is
durationDel.delegate = self