How can I call a function that creates a button from another viewcontroller? - swift

I have a function in my TuesdayViewController.swift that creates a button.
Whenever it's Tuesday, I want that button to appear in my ViewController.swift
I have this function in my TuesdayViewController:
func tuesdayView(){
let button = UIButton(frame: CGRect(x: 16, y: 200, width: 343, height: 45))
button.backgroundColor = .red
button.setTitle("Test button", for: .normal)
self.view.addSubview(button)
}
If I call this function in Tuesday's viewDidLoad(), the button shows in the TuesdayViewController.
How can I make it so I call this function through my main ViewController and for the button to show in the main ViewController screen?
I have tried this in my main ViewController but the button doesn't appear:
override func viewDidLoad(){
super.viewDidLoad()
TuesdayViewController().tuesdayVew()
}
Any help would be appreciated!

If I understand your question right, you just want to add the same kind of button from your TuesdayViewController into your MainViewController, and not adding the button to the TuesdayViewController by calling it in the MainViewController.
To do that, you just can declare your tuesdayView as a public property of your TuesdayViewController, like so:
TuesdayViewController.swift
class TuesdayViewController: UIViewController {
public lazy var tuesdayView: UIButton = {
let button = UIButton(frame: CGRect(x: 16, y: 200, width: 343, height: 45))
button.backgroundColor = .red
button.setTitle("Test button", for: .normal)
return button
}()
override func viewDidLoad(){
super.viewDidLoad()
self.view.addSubview(self.tuesdayView)
}
}
And call that property like what you've been doing before.
MainViewController.swift
class MainViewController: UIViewController {
override func viewDidLoad(){
super.viewDidLoad()
let button = TuesdayViewController().tuesdayView
self.view.addSubview(button)
}
}

You need to use an observer with NotificationCenter:
First you need to create a Notification Name
extension Notification.Name {
static let notName = Notification.Name("notName")
}
TuesdayViewController:
override func viewDidLoad() {
NotificationCenter.default.addObserver(self, selector: #selector(tuesdayViewObserver), name: .notName, object: nil)
}
#objc func tuesdayViewObserver(_ notification: Notification) {
//Here you call the tuesdayView function
tuesdayView()
}
And in the controller in which you want to fire the method, you post the notification and the method will be executed:
override func viewDidLoad() {
super.viewDidLoad()
NotificationCenter.default.post(name: .notName, object: nil)
}
For further info, I recommend you to take a look to NotificationCenter and how it works. LINK

Related

How do I targeting selector with addTarget in another class

I try to understand how to control component form one class to another class but that's not working.
class VC:UIViewController {
override func viewDidLoad() {
let instance = Test()
self.view.addSubView(instance.button)
}
}
class Test:NSObject {
var button:UIButton!
override init() {
button = UIButton(frame:CGRect(x:0, y:0, width:100, height:40))
button.setTitle(("TEST", for:.normal)
button.addTarget(self, action: #selector(tapButton(_:)), for: .touchUpInside)
}
#objc func tapButton(_ sender:UIButton) {
print("TAP Button")
}
}
When I tapped the button, nothing happens !
I try to change
button.addTarget(self, action: #selector(tapButton(_:)), for: .touchUpInside)
with
button.addTarget(nil, action: #selector(tapButton(_:)), for: .touchUpInside)
That's not working !
How to resolve this problem ?
Thanks for your help.
You have 3 issues:
instance is a local variable, so it gets deallocated as soon as viewDidLoad() finishes. Make it a property of VC. The button only keeps a weak reference to the object, so when it is deallocated it becomes nil. In cases where the target is nil, UIKit will search up the responder chain for the action method. Since VC doesn't supply a tapButton method, nothing happens when the button is pressed.
You need to call super.init() so that self is available to be used with the button. self can't be created until all properties have been initialized. Because init is an override, you must call super.init() to initialize the properties that NSObject provides before you can use self in the button.
Your frame puts your button in an unaccessible region of the screen. I changed your frame to put the button inside the safe area of the iPhone 11. I also made the button .green so that I could see it.
class VC: UIViewController {
var instance = Test() // make instance a property
override func viewDidLoad() {
// let instance = Test() // this was a local variable that doesn't hang around
self.view.addSubview(instance.button)
}
}
class Test: NSObject {
var button: UIButton!
override init() {
super.init() // call this so that you can use self below
button = UIButton(frame:CGRect(x: 100, y: 100, width: 100, height: 40))
button.setTitle("TEST", for:.normal)
button.backgroundColor = .green
button.addTarget(self, action: #selector(tapButton(_:)), for: .touchUpInside)
}
#objc func tapButton(_ sender: UIButton) {
print("TAP Button")
}
// Add deinit to see when this object is deinitialized. When
// instance is local to viewDidLoad() this object gets freed
// when viewDidLoad() finishes.
deinit {
print("Oops, the Test object has been deinitialized")
}
}

How can I make sure that after declaring a delegate of a protocol in Swift 5.2, that delegate is not nil when it is called?

I am trying to clear a textfield in MainViewController from the DetailViewController. I have the following code in a Swift Playground.
import UIKit
import PlaygroundSupport
protocol DetailViewControllerDelegate: class {
func bar()
}
class DetailViewController: UIViewController {
var detailViewControllerDelegate: DetailViewControllerDelegate!
override func loadView() {
let view = UIView()
view.backgroundColor = .white
let button = UIButton()
button.frame = CGRect(x: 100, y: 200, width: 180, height: 20)
button.setTitle("Hello World!", for: .normal)
button.backgroundColor = .blue
button.addTarget(self, action: #selector(handlePress), for: .touchUpInside)
view.addSubview(button)
self.view = view
}
#objc func handlePress() {
print("\(#function)")
if let vrvc = detailViewControllerDelegate {
vrvc.bar()
} else {
print("detailViewControllerDelegate is NIL")
}
}
}
class MainViewController : UIViewController, DetailViewControllerDelegate {
func bar() {
print("Inside Bar")
}
override func loadView() {
let detailViewController = DetailViewController()
detailViewController.detailViewControllerDelegate = self
}
}
// Present the view controller in the Live View window
PlaygroundPage.current.liveView = DetailViewController()
When the button is pressed, detailViewController is nil. How can I make sure that detailViewController is NOT nil when the button is pressed?
You have two distinct DetailViewControllers:
override func loadView() {
let detailViewController = DetailViewController()
detailViewController now references a new instance of DetailViewController
detailViewController.detailViewControllerDelegate = self
}
}
// Present the view controller in the Live View window
PlaygroundPage.current.liveView = DetailViewController()
liveView now references a new instance of DetailViewController which is different from the one above and in which you have not set detailViewControllerDelegate.
If you need to create a DetailViewController in one place and reference it in another you need to store a reference to it in a property that is accessible in both places.
That said, the second instance of DetailViewController is being created in a statement which looks like an attempt to test code in the Playground so maybe you just need to think about how you are testing.

Cannot attach existing method to UIButton

I'm attempting to make a mock-up application, but I cannot seem to get the UIButton to hook up to the action that should be executed when I click it.
I'm not sure why it cannot find the method, the names are the same and adding in a (_:) to the #selector does nothing.
import UIKit
class LoginViewController: UIViewController {
var loginButton: UIButton?
#objc func onLoginButtonPress(sender: UIButton!) {
print("Login Button Press")
}
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
setup()
}
func setup() -> Void {
initLoginButton()
self.view.backgroundColor = .green
}
func initLoginButton() -> Void {
let loginButtonBounds = CGRect(x: 100, y: 100, width: 250, height: 250)
loginButton = UIButton(frame: loginButtonBounds)
loginButton!.backgroundColor = .red
loginButton!.setTitle("Login", for: .normal)
loginButton!.addTarget(self, action: #selector("onLoginButtonPress"), for: .touchUpInside)
self.view.addSubview(loginButton!)
}
loginButton!.addTarget(self, action: #selector(onLoginButtonPress(sender:)), for: .touchUpInside)
edit this line code, this should work for you. Its better to not use force-unwrapping so avoid using it.
Have you tried removing the parenthesis from "onLoginButtonPress"? I don't think they are needed.
loginButton!.addTarget(self, action: #selector("onLoginButtonPress"), for: .touchUpInside)

I have custom uinavigation class. In that custom delegate method declared. How to access that method to view controller

#objc protocol MyDelegate {
func buttonAction()
}
class CustomNavigationBar: UINavigationController {
var delegte : MyDelegate?
override func viewDidLoad() {
super.viewDidLoad()
let button = UIButton.init(frame: CGRectMake(200, 10, 50, 30))
button.setBackgroundImage(UIImage(named: "a.png"), forState: .Normal)
button.addTarget(self, action: "testing", forControlEvents: .TouchUpInside)
self.navigationBar.addSubview(button)
}
func testing(){
self.delegte?.buttonAction()
print("Pavan")
}
If i press this button, testing is calling.
But in viewcontroller calling delegate method but giving error
class ViewController: UIViewController,MyDelegate{
override func viewDidLoad() {
super.viewDidLoad()
self.navigationItem.title = "hi"
let vc = CustomNavigationBar()
vc.delegte = self
}
func buttonAction() {
print("Tupale")
}
would u mind to hint the error message?
updated:
In CustomNavigationBar class, you have to change var delegte : MyDelegate? to var delegte : UIViewController?.
then in ViewController class, you could set self which is an instance of UIViewController to the delegate of vc.
Have a try.

UIButton action generates a SIGABRT but no breakpoint triggers

I have this code to create a button on my storyboard
override func viewDidLoad() {
super.viewDidLoad()
...
signupButton.addTarget(self, action: "pressed:", forControlEvents: .TouchUpInside)
...
}
func pressed(sender:UIButton!) {
println("Pressed")
}
However, it throws a SIGABRT error when I press the button. So, I added a breakpoint on all exceptions. However, no breakpoint was ever it - it just threw the exception. How do I find out what's causing the error?
You have two issues here...
First: You placed the pressed function in the scope of viewDidLoad, when it needs to be outside of that function and in the scope of the class. Move the function pressed out of viewDidLoad().
Second: This is not what is causein the signal abort crash, but it is needed nonetheless. Add a semicolon to the action selector name.
signupButton.addTarget(self, action: "pressed:", forControlEvents: .TouchUpInside)
Or, if you don't need to pass any object to the function pressed, then change the function header to func pressed().
BASIC EXAMPLE: (tested and working in new project)
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
let signupButton = UIButton(frame: CGRectMake(0, 0, 100.0, 100.0))
signupButton.setTitle("BUTTON", forState: UIControlState.Normal)
signupButton.addTarget(self, action: "pressed:", forControlEvents: .TouchUpInside)
self.view.addSubview(signupButton)
}
func pressed(sender:UIButton!) {
println("Button pressed")
}
}