My app was crashed when i touched on any button - ios

How to use the selector in Utility Class and there definition? My code create some error. PLease tell me how to resolve this error?
Error : My app crashed when I touched on any button.
class BarButton{
static func items(navigationItem: UINavigationItem) {
//Hide Back Button
navigationItem.hidesBackButton = true
//Add Label to Left of NavigationBar
let leftLabel = UIBarButtonItem(title: "", style: .plain, target: self, action: nil)
leftLabel.tintColor = UIColor.init(red: 30/255, green: 104/255, blue: 140/255, alpha: 1)
navigationItem.setLeftBarButton(leftLabel, animated: false)
//List Button
let btnList = UIButton(frame: CGRect(x: 0, y: 0, width: 35, height: 22))
btnList.setImage(UIImage(named:"list-icn"), for: .normal)
btnList.addTarget(self, action: #selector(listBtn), for: .touchUpInside)
let rightBarBtn1 = UIBarButtonItem.init(customView: btnList)
rightBarBtn1.tintColor = UIColor(red: 30/255, green: 104/255, blue: 140/255, alpha: 1)
//Notification Button
let btnList2 = UIButton(frame: CGRect(x: 0, y: 0, width: 22, height: 22))
btnList2.setImage(UIImage(named:"notification-icn"), for: .normal)
btnList2.addTarget(self, action: #selector(notificationBtn), for: .touchUpInside)
let rightBarBtn2 = UIBarButtonItem.init(customView: btnList2)
rightBarBtn2.tintColor = UIColor(red: 30/255, green: 104/255, blue: 140/255, alpha: 1)
navigationItem.setRightBarButtonItems([rightBarBtn1, rightBarBtn2], animated: true)
}
#objc func listBtn() {
print("List Btn")
}
#objc func notificationBtn() {
print("Notification Btn")
}
}

Try this and see:
let btnList = UIButton(frame: CGRect(x: 0, y: 0, width: 35, height: 22))
btnList.setImage(UIImage(named:"list-icn"), for: .normal)
// Update selector
btnList.addTarget(self, action: #selector(BarButton.listBtn(sender:)), for: .touchUpInside)
let rightBarBtn1 = UIBarButtonItem.init(customView: btnList)
rightBarBtn1.tintColor = UIColor(red: 30/255, green: 104/255, blue: 140/255, alpha: 1)
//Notification Button
let btnList2 = UIButton(frame: CGRect(x: 0, y: 0, width: 22, height: 22))
btnList2.setImage(UIImage(named:"notification-icn"), for: .normal)
// Update selector
btnList2.addTarget(self, action: #selector(BarButton.notificationBtn(sender:)), for: .touchUpInside)
let rightBarBtn2 = UIBarButtonItem.init(customView: btnList2)
rightBarBtn2.tintColor = UIColor(red: 30/255, green: 104/255, blue: 140/255, alpha: 1)
navigationItem.setRightBarButtonItems([rightBarBtn1, rightBarBtn2], animated: true)
// Try using static
#objc static func listBtn(sender: Any)
{
print("List Btn")
}
// Try using static
#objc static func notificationBtn(sender: Any)
{
print("Notification Btn")
}

Your listBtn and notificationBtn methods are instance methods, but you try to invoke them on a class, not class instance - your items method is static, so self inside of it is of AnyObject.Type type.
To fix it, make listBtn and notificationBtn methods static.

Related

I’m trying to make two buttons that print two different things in swift playground

I’m new to swift and coding in general and I’m trying to make a truth or dare game but when I run the code using step through my code and press the red button it is running the code from Responder and not from responder1. I think the problem might be that I maid the 2nd button just by copying the for the 1st button and adding 1, and then swift thinks that the button 1 is the same as button and it runs the code for the 1st button.
import PlaygroundSupport
import UIKit
import SpriteKit
//
let view = UIView()
view.backgroundColor = #colorLiteral(red: 0.0, green: 0.7522757053375244, blue: 0.780606746673584, alpha: 1.0)
view.frame = CGRect(x: 0, y: 0, width: 400, height: 300)
let lbl = UILabel(frame: CGRect(x: 50, y: 0, width: 200, height: 50))
lbl.text = "Hello, World!"
view.addSubview(lbl)
lbl.textColor = .blue
let button = UIButton(frame: CGRect(x: 50, y: 100, width: 100, height: 50))
button.backgroundColor = #colorLiteral(red: 0.721568644046783, green: 0.886274516582489, blue: 0.592156887054443, alpha: 1.0)
//button.setTitleColor(#colorLiteral(red: 0.0, green: 0.0, blue: 0.0, alpha: 1.0), for: UIControlState.selected)
button.setTitle("button", for: UIControl.State.selected)
button.layer.cornerRadius = 10
view.addSubview(button)
let button1 = UIButton(frame: CGRect(x: 250, y: 100, width: 100, height: 50))
button1.backgroundColor = #colorLiteral(red: 1.0, green: 0.1893465518951416, blue: 0.1893465518951416, alpha: 1.0)
//button1.setTitleColor(#colorLiteral(red: 0.0, green: 0.0, blue: 0.0, alpha: 1.0), for: UIControlState.selected)
button1.setTitle("button1", for: UIControl.State.selected)
button1.layer.cornerRadius = 10
view.addSubview(button1)
class Responser: NSObject
{
//Method to be called
#objc func printname()
{
lbl.text = "aha"
}
}
let responder = Responser()
button.addTarget(responder, action: #selector(Responser.printname), for:.touchUpInside)
class Responser1: NSObject
{
//Method to be called
#objc func printname()
{
lbl.text = "UAh"
}
}
let responder1 = Responser()
button1.addTarget(responder1, action: #selector(Responser1.printname), for:.touchUpInside)
PlaygroundPage.current.liveView = view

Swift playgrounds UIButton not working correctly

I've got this UIButton called 'start button' in swift playgrounds. This is the setup for it.
func setupStartButton() {
startButton.frame = CGRect(x: 15, y: 550, width: 125, height: 50)
startButton.backgroundColor = UIColor(ciColor: CIColor(red: 255/255, green: 0/255, blue: 77/255, alpha: 1.0))
startButton.layer.cornerRadius = 20
startButton.setTitle("Start", for: .normal)
startButton.setTitleColor(.white, for: .normal)
startButton.addTarget(self, action: #selector(startButtonTapped), for: .touchUpInside)
view.addSubview(startButton)
I call this in viewDidLoad().
override func viewDidLoad() {
view.frame = CGRect(x: 0, y: 0, width: 524, height: 766)
view.backgroundColor = .white
setupStartButton()
PlaygroundPage.current.liveView = view
PlaygroundPage.current.needsIndefiniteExecution = true
}
But when I tap on a button it doesn't run the function called 'startButtonTapped' which just prints "Hello" to the console.
This is the function 'startButtonTapped'.
#objc func startButtonTapped() {
print("Hello")
}
Why does it not do anything when I tap the button?
How can I fix this?
Thank you
I can't see the rest of your code. I just quickly created one so that you could compare with your own code.
import UIKit
import PlaygroundSupport
class VC: UIViewController {
let startButton = UIButton(type: .system)
override func viewDidLoad() {
super.viewDidLoad()
setupStartButton()
}
func setupStartButton() {
startButton.frame = CGRect(x: 15, y: 550, width: 125, height: 50)
startButton.backgroundColor = UIColor(ciColor: CIColor(red: 255/255, green: 0/255, blue: 77/255, alpha: 1.0))
startButton.layer.cornerRadius = 20
startButton.setTitle("Start", for: .normal)
startButton.setTitleColor(.white, for: .normal)
startButton.addTarget(self, action: #selector(startButtonTapped), for: .touchUpInside)
view.addSubview(startButton)
}
#objc func startButtonTapped() {
print("Hello")
}
}
let vc = VC()
PlaygroundPage.current.liveView = vc

how to bring button inFront of tableviewController in swift 4? [duplicate]

This question already has answers here:
Add view over tableview (UITableViewController)
(7 answers)
Closed 5 years ago.
I have a table view controller and the only way for me is to using that so please don't tell me use viewController instead of tableviewController here is my codes for adding a button and this Button is shown in the last cell But I want when user scrolling table view the button be at the bottom of screen without moving
override func viewDidLoad() {
super.viewDidLoad()
let submitButton = UIButton(frame: CGRect(x: 5, y: view.frame.origin.y + view.frame.size.height, width: self.view.frame.width - 10 , height: 50))
submitButton.backgroundColor = UIColor.init(red: 180/255, green: 40/255, blue: 56/255, alpha: 1.0)
submitButton.setTitle("Submit", for: .normal)
submitButton.titleLabel?.font = UIFont(name: "Arial", size: 15)
submitButton.titleLabel?.textColor = .white
submitButton.addTarget(self, action: #selector(submit), for: .touchUpInside)
submitButton.layer.cornerRadius = 5
view.addSubview(submitButton)
Try this approach if it suits your scenario:
var submitButton = UIButton()
override func viewDidLoad() {
super.viewDidLoad()
submitButton.frame = CGRect(x: 5, y: view.frame.origin.y + view.frame.size.height, width: self.view.frame.width - 10 , height: 50)
submitButton.backgroundColor = UIColor.init(red: 180/255, green: 40/255, blue: 56/255, alpha: 1.0)
submitButton.setTitle("Submit", for: .normal)
submitButton.titleLabel?.font = UIFont(name: "Arial", size: 15)
submitButton.titleLabel?.textColor = .white
submitButton.addTarget(self, action: #selector(submit), for: .touchUpInside)
submitButton.layer.cornerRadius = 5
self.view.window?.addSubview(submitButton)
}
But in this scenario you have to remove the button from superview in viewWillDisappear :
override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
self. submitButton.removeFromSuperview()
}
Try this
declare button as a variable
var submitButton:UIButton!
and in viewDidLoad
submitButton = UIButton(frame: CGRect(x: 5, y: UIScreen.main.bounds.size.height - 50 , width: UIScreen.main.bounds.size.width - 10 , height: 50))
submitButton.backgroundColor = UIColor.init(red: 180/255, green: 40/255, blue: 56/255, alpha: 1.0)
submitButton.setTitle("Submit", for: .normal)
submitButton.titleLabel?.font = UIFont(name: "Arial", size: 15)
submitButton.titleLabel?.textColor = .white
submitButton.addTarget(self, action: #selector(submit), for: .touchUpInside)
submitButton.layer.cornerRadius = 5
self.view.addSubview(submitButton)
and implement this method
override func scrollViewDidScroll(_ scrollView: UIScrollView) {
submitButton.frame = CGRect.init(x: submitButton.frame.origin.x, y: UIScreen.main.bounds.size.height + scrollView.contentOffset.y - 50 , width: submitButton.frame.width, height: submitButton.frame.height)
}

Creating a button in the Playground app on iPad

I am working with the new beta version of the Playground app in iOS 10.
I want to get some UIKit elements working in a playground.
Swift 3 is just different enough from Swift 2, I am having some problems.
I got a label and text box working, but I cannot get a button working.
I have the button showing up but cannot get text in it or an action working on touch-up.
Here is my code I have.
import PlaygroundSupport
import UIKit
//
let view = UIView()
view.backgroundColor = #colorLiteral(red: 0.909803926944733, green: 0.47843137383461, blue: 0.643137276172638, alpha: 1.0)
PlaygroundPage.current.liveView = view
let lbl = UILabel(frame: CGRect(x: 50, y: 0, width: 200, height: 50))
lbl.text = "Hello, World!"
view.addSubview(lbl)
let txt = UITextField(frame: CGRect(x: 150, y: 200, width: 200, height: 50))
//txt.placeholder = "Enter text here"
//txt.font = UIFont.systemFont(ofSize: 15)
txt.borderStyle = UITextBorderStyle.roundedRect
view.addSubview(txt)
func buttonPressed(sender: UIButton)
{
//sender.backgroundColor = #colorLiteral(red: 0.725490212440491, green: 0.47843137383461, blue: 0.0980392172932625, alpha: 1.0)
}
let button = UIButton(frame: CGRect(x: 50, y: 100, width: 100, height: 50))
button.backgroundColor = #colorLiteral(red: 0.721568644046783, green: 0.886274516582489, blue: 0.592156887054443, alpha: 1.0)
button.setTitleColor(#colorLiteral(red: 0.0, green: 0.0, blue: 0.0, alpha: 1.0), for: UIControlState.focused)
button.setTitle("button", for: UIControlState.focused)
button.layer.cornerRadius = 10
button.addTarget(button, action: "buttonPressed", for: UIControlEvents.touchUpInside)
view.addSubview(button)
I've been trying the same and the following code works for me:
import UIKit
import PlaygroundSupport
class Receiver {
#objc func buttonClicked() {
print("click")
}
}
let view = UIView()
view.backgroundColor = #colorLiteral(red: 1.0, green: 1.0, blue: 1.0, alpha: 1.0)
let receiver = Receiver()
let button = UIButton(frame: CGRect(x: 10, y: 10, width: 100, height: 50))
button.setTitle("TestButton", for: .normal)
button.setTitleColor(#colorLiteral(red: 0.474509805440903, green: 0.839215695858002, blue: 0.976470589637756, alpha: 1.0), for: .normal)
button.addTarget(receiver, action: #selector(Receiver.buttonClicked), for: .touchUpInside)
PlaygroundPage.current.liveView = view
view.addSubview(button)
I think the important difference is that you add the button as target of the .touchUpInside event. Unfortunately, the button does not implement your buttonPressed method (it's just defined in the Playground, not as part of the UIButton class). As there is no self in a general playground (to refer to the function you wrote), I wrapped it into a new class and used an instance of this class to be the receiver of the .touchUpInside event.

Custom UIView Class - Swift

I've builded a view which appears from the bottom and hides after sometime and it works well,but i want to make it in UIView class as a modal, i looked over the internet but i couldn't get or understand how to do that.
snake = UIView(frame: CGRect(x: 0 , y: self.view.frame.size.height-66, width: self.view.frame.size.width, height: 66))
snake.backgroundColor = UIColor(red: 50/255, green: 50/255, blue: 50/255, alpha: 1.0)
let label = UILabel(frame: CGRect(x: 12, y: 8, width: snake.frame.size.width-90, height: 50))
label.text = "Connection error please try again later!!"
label.textColor = UIColor.whiteColor()
label.numberOfLines = 0
label.font = UIFont.systemFontOfSize(14)
snake.addSubview(label)
let button = UIButton(frame: CGRect(x: snake.frame.size.width-87, y: 8, width: 86, height: 50))
button.setTitle("OK", forState: UIControlState.Normal)
button.setTitleColor(UIColor(red: 76/255, green: 175/255, blue: 80/255, alpha: 1.0), forState: UIControlState.Normal)
button.addTarget(self, action: "hideSnackBar:", forControlEvents: UIControlEvents.TouchUpInside)
snake.addSubview(button)
self.view.addSubview(snake)
How to start with this class I've no idea, i am creating the view programmatically with its frame and i need to set properties for the button for example or the label and create the view from any class.
class snake: UIView {
override init (frame : CGRect) {
super.init(frame : frame)
}
convenience init () {
self.init(frame:CGRect.zero)
}
required init(coder aDecoder: NSCoder) {
fatalError("This class does not support NSCoding")
}
}
code:
var label:UILabel!
var button:UIButton!
override init (frame : CGRect) {
super.init(frame : frame)
self.backgroundColor = UIColor(red: 50/255, green: 50/255, blue: 50/255, alpha: 1.0)
label = UILabel(frame: CGRect(x: 12, y: 8, width: self.frame.size.width-90, height: 50))
label.text = "Connection error please try again later!!"
label.textColor = UIColor.whiteColor()
label.numberOfLines = 0
label.font = UIFont.systemFontOfSize(14)
self.addSubview(label)
button = UIButton(frame: CGRect(x: self.frame.size.width-87, y: 8, width: 86, height: 50))
button.setTitle("OK", forState: UIControlState.Normal)
button.setTitleColor(UIColor(red: 76/255, green: 175/255, blue: 80/255, alpha: 1.0), forState: UIControlState.Normal)
button.addTarget(self, action: "hideSnackBar:", forControlEvents: UIControlEvents.TouchUpInside)
self.addSubview(button)
}
and use it:
let snakeView = snake(frame: CGRectMake(0 ,self.view.frame.size.height-66, self.view.frame.size.width, 66)))
and set data for snakeview:
snakeView.label.text = "hello"
But usually i 'll create a function to update data for view:
func updateData(title:String){
self.label.text = title
}
and call it when need:
snake.updateData("hello")
P/s: if you use xib, you must to implement awakeFromNib instead of init. And create snake with xib(remember set identifier for xib : "snakeView"):
let snakeView = NSBundle.mainBundle().loadNibNamed("snakeView", owner: nil, options: nil)[0] as snakeView

Resources