I thought that I have solved the following error with inheriting from NSObject. The frustrating thing is that id did work once :-)
The beneth code should run(not wok) in any ViewController.
The problem is that the action(tapped) is not called: Can you see why not?
import Foundation
import UIKit
public class TestErrorClass: NSObject {
var errorView: UIView?
public override init() {}
public func showErrorMessage(errorMessage: String) {
let APPDELEGATE = UIApplication.shared.delegate as UIApplicationDelegate?
if let window = APPDELEGATE?.window{
let v = window?.subviews[0]
self.errorView = UIView(frame: (v?.frame)!)
self.errorView?.backgroundColor = UIColor.red
let tapGesture = UITapGestureRecognizer(target: self, action: #selector(tapped(_:)))
tapGesture.isEnabled = true
errorView!.addGestureRecognizer(tapGesture)
v?.addSubview(self.errorView!)
}
}
#objc func tapped(_ sender: UITapGestureRecognizer) {
errorView!.removeFromSuperview()
}
}
Used in this code:
import UIKit
class NetKitSampleAppViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
}
override func viewDidLayoutSubviews() {
let b = TestErrorClass()
b.showErrorMessage(errorMessage: "Bla bal")
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
Related
I am following MVP architecture for developing an iOS app.The app is quite simple in which onViewDidLoad() I call a web service which returns me some data and I display that data in a table view.
ViewController:
class A : UIViewController{
var presenter : MyPresenter?
override func viewDidLoad() {
presenter = MyPresenter(delegate:self)
presenter.callWS()
}
}
extension A : Mydelegate{
func onSuccess(){
//this doesnt allow my viewcontroller to deint
tablview.delegate=self
tableview.datasource=self
tableview.reloadData()
}
}
protocol MyDelegate : class{
func onSuccess()
}
class MYPresenter {
weak var delegate : MyDelegate?
init(MyDelegate) {
self.delegate=delegate
}
func callWS(){
delegate.onSuccess()
}
}
This onSucces of MyDelegate does not allow my A viewcontroller to deint
Please let me know what i am doing wrong?
I've slightly modified version of your code and run it in a playground:
import UIKit
import PlaygroundSupport
class A : UITableViewController {
var presenter : MyPresenter?
override func viewDidLoad() {
presenter = MyPresenter(delegate:self)
presenter?.callWS()
let gesture = UITapGestureRecognizer(target: self, action: #selector(dismissOnTap))
view.addGestureRecognizer(gesture)
}
func dismissOnTap() {
dismiss(animated: true)
}
deinit {
print("Bye VC")
}
}
extension A : MyDelegate {
func onSuccess(){
//this doesnt allow my viewcontroller to deint
tableView.delegate = self
tableView.dataSource = self
tableView.reloadData()
}
}
protocol MyDelegate : class {
func onSuccess()
}
class MyPresenter {
weak var delegate : MyDelegate?
init(delegate: MyDelegate) {
self.delegate = delegate
}
func callWS() {
delegate?.onSuccess()
}
deinit {
print("Bye")
}
}
class B: UIViewController {
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(true)
let gesture = UITapGestureRecognizer(target: self, action: #selector(showOnTap))
view.addGestureRecognizer(gesture)
}
func showOnTap() {
let vc = A(style: .plain)
present(vc, animated: true)
}
}
let b = B()
b.view.frame = CGRect(x: 0, y: 0, width: 400, height: 600)
PlaygroundPage.current.liveView = b.view
And everything is deallocating properly. I guess the retain cycle is somewhere else and it's hard to find based on provided code.
I'm building simple theme engine and would like have an extension which adds UISwipeGestureRecognizer to UIViewController
Here is my code:
protocol Themeable {
func themeDidUpdate(currentTheme: Theme) -> Void
}
extension Themeable where Self: UIViewController {
func switchCurrentTheme() {
Theme.switchTheme()
themeDidUpdate(Theme.currentTheme)
}
func addSwitchThemeGestureRecognizer() {
let gestureRecognizer = UISwipeGestureRecognizer(target: self, action:#selector(Self.switchCurrentTheme))
gestureRecognizer.direction = .Down
gestureRecognizer.numberOfTouchesRequired = 2
self.view.addGestureRecognizer(gestureRecognizer)
}
}
Of course compiler can't find #selector(Self.switchCurrentTheme) as it isn't exposed via #objc directive. Is it possible to add this behaviour to my extension?
UPDATE: Theme is a Swift enum, so I can't add #objc in front of Themeable protocol
The cleanest, working solution I could come up with was to define a private extension on UIViewController with the method in question. By limiting the scope to private, access to this method is isolated to within the source file where the protocol is defined in. Here's what it looks like:
protocol Themeable {
func themeDidUpdate(currentTheme: Theme) -> Void
}
fileprivate extension UIViewController {
#objc func switchCurrentTheme() {
guard let themeableSelf = self as? Themeable else {
return
}
Theme.switchTheme()
themeableSelf.themeDidUpdate(Theme.currentTheme)
}
}
extension Themeable where Self: UIViewController {
func addSwitchThemeGestureRecognizer() {
let gestureRecognizer = UISwipeGestureRecognizer(target: self, action:#selector(switchCurrentTheme))
gestureRecognizer.direction = .Down
gestureRecognizer.numberOfTouchesRequired = 2
self.view.addGestureRecognizer(gestureRecognizer)
}
}
I found a solution. May be not the perfect one, but it works.
As I can't define Themeable protocol as #objc because it uses Swift-only enum I decided to move method I want to call to "parent" protocol and define this protocol as #objc. It seems like it works but I don't really like it to be honest...
#objc protocol ThemeSwitcher {
func switchCurrentTheme()
}
protocol Themeable: ThemeSwitcher {
func themeDidUpdate(currentTheme: Theme) -> Void
}
extension Themeable where Self: UIViewController {
func switchCurrentTheme() {
Theme.switchTheme()
themeDidUpdate(Theme.currentTheme)
}
func addSwitchThemeGestureRecognizer() {
let gestureRecognizer = UISwipeGestureRecognizer(target: self, action:#selector(switchCurrentTheme))
gestureRecognizer.direction = .Down
gestureRecognizer.numberOfTouchesRequired = 2
self.view.addGestureRecognizer(gestureRecognizer)
}
}
Have you considered creating a wrapper to let you call your non-#objc function from an #objc one?
#objc class Wrapper: NSObject {
let themeable: Themeable
init(themeable: Themeable) {
self.themeable = themeable
}
func switchCurrentTheme() {
Theme.switchTheme()
themeable.themeDidUpdate(Theme.currentTheme)
}
}
protocol Themeable {
func themeDidUpdate(currentTheme: Theme) -> Void
}
extension Themeable where Self: UIViewController {
func addSwitchThemeGestureRecognizer() {
let wrapper = Wrapper(themeable: self)
let gestureRecognizer = UISwipeGestureRecognizer(target: wrapper, action:#selector(Wrapper.switchCurrentTheme))
gestureRecognizer.direction = .Down
gestureRecognizer.numberOfTouchesRequired = 2
self.view.addGestureRecognizer(gestureRecognizer)
}
}
Here is a similar use-case, you can call a method through a selector without using #objc as in swift by using the dynamic keyword. By doing so, you are instructing the compiler to use dynamic dispatch implicitly.
import UIKit
protocol Refreshable: class {
dynamic func refreshTableData()
var tableView: UITableView! {get set}
}
extension Refreshable where Self: UIViewController {
func addRefreshControl() {
tableView.insertSubview(refreshControl, at: 0)
}
var refreshControl: UIRefreshControl {
get {
let tmpAddress = String(format: "%p", unsafeBitCast(self, to: Int.self))
if let control = _refreshControl[tmpAddress] as? UIRefreshControl {
return control
} else {
let control = UIRefreshControl()
control.addTarget(self, action: Selector(("refreshTableData")), for: .valueChanged)
_refreshControl[tmpAddress] = control
return control
}
}
}
}
fileprivate var _refreshControl = [String: AnyObject]()
class ViewController: UIViewController: Refreshable {
#IBOutlet weak var tableView: UITableView! {
didSet {
addRefreshControl()
}
}
func refreshTableData() {
// Perform some stuff
}
}
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 am a beginner of xcode programming. I am trying to do an action, when i click button from A view, the button of B view will be hidden. I already know i can use button.hidden = true; for self view controller but I don't know how to control button from other view.
Thanks
#IBAction func TestBut(sender: UIButton) {
setting.hidden = false
}
Before, you create a custom view with button, button action and a protocol as:
protocol CustomViewDelegate {
func buttonPressed (sender: AnyObject)
}
class CustomView: UIView {
#IBOutlet weak var button: UIButton!
var delegate: CustomViewDelegate!
override func awakeFromNib() {
super.awakeFromNib()
}
class func loadViewFromXib() -> CustomView {
return NSBundle.mainBundle().loadNibNamed("CustomView", owner: self, options: nil)[0] as! CustomView
}
#IBAction func buttonPressed(sender: AnyObject) {
self.delegate.buttonPressed(sender)
}
}
In your ViewController.
class ViewController: UIViewController, CustomViewDelegate {
var firstView: CustomView?
var secondView: CustomView?
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
self.firstView = CustomView.loadViewFromXib()
self.secondView = CustomView.loadViewFromXib()
firstView!.frame = CGRectMake(0, 0, 100, 100)
secondView!.frame = CGRectMake(0, 200, 100, 100)
firstView!.delegate = self
secondView!.delegate = self
self.view.addSubview(firstView!)
self.view.addSubview(secondView!)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func buttonPressed(sender: AnyObject) {
if (sender as! UIButton) == self.firstView!.button {
self.secondView?.button.hidden = true
}else {
self.firstView?.button.hidden = true
}
}
}
the button has to be a property of the view( or view controller).
A call would look like this:
view.button.hidden = true
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