I am a newby in ios development and I am facing the following problem.
I create a button at runtime in the viewDidLoad method:
class ViewController: UIViewController {
#IBOutlet weak var TestButton: UIButton!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
println("start");
// Create Button at runtime
var button = UIButton.buttonWithType(UIButtonType.System) as UIButton
button.frame = CGRectMake(100, 100, 100, 50)
button.backgroundColor = UIColor.redColor()
button.setTitle("Test Button", forState: UIControlState.Normal)
button.addTarget(self, action: "buttonAction:", forControlEvents: UIControlEvents.TouchUpInside)
self.view.addSubview(button)
func buttonAction(sender:UIButton!)
{
println("Button tapped.")
}
}
When I press the button in the simulator the app stops at line :
class AppDelegate: UIResponder, UIApplicationDelegate {
of AppDelegate.swift
Does anyone have any idea why it doesn't output "Button tapped." ?
If I get a problem like this, how can I report the errormessage to someone else ? I mean I do not see any errorcode or stacktrace in XCode. Where to find this ?
Function not in viewDidLoad.
example:
class ViewController: UIViewController {
#IBOutlet weak var TestButton: UIButton!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
println("start");
// Create Button at runtime
var button = UIButton.buttonWithType(UIButtonType.System) as UIButton
button.frame = CGRectMake(100, 100, 100, 50)
button.backgroundColor = UIColor.redColor()
button.setTitle("Test Button", forState: UIControlState.Normal)
button.addTarget(self, action: "buttonAction:", forControlEvents: UIControlEvents.TouchUpInside)
self.view.addSubview(button)
} // CLOSE
func buttonAction(sender:UIButton!)
{
println("Button tapped.")
}
}// CLOSE
You should extract your method for button outside your function like following:
class ViewController: UIViewController {
#IBOutlet weak var TestButton: UIButton!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
println("start");
// Create Button at runtime
var button = UIButton.buttonWithType(UIButtonType.System) as UIButton
button.frame = CGRectMake(100, 100, 100, 50)
button.backgroundColor = UIColor.redColor()
button.setTitle("Test Button", forState: UIControlState.Normal)
button.addTarget(self, action: "buttonAction:", forControlEvents: UIControlEvents.TouchUpInside)
self.view.addSubview(button)
}
func buttonAction(sender:UIButton!)
{
println("Button tapped.")
}
}
Why is this you might ask? It is because you will register your event which is in scope of the function. When function ends your function for event is not there any more. Since, the chrash
Try this:
override func viewDidLoad() {
super.viewDidLoad()
let button = UIButton.buttonWithType(UIButtonType.System) as UIButton
button.frame = CGRectMake(100, 100, 100, 50)
button.backgroundColor = UIColor.greenColor()
button.setTitle("Test Button", forState: UIControlState.Normal)
button.addTarget(self, action: "buttonAction:", forControlEvents: UIControlEvents.TouchUpInside)
self.view.addSubview(button)
}
func buttonAction(sender:UIButton!)
{
println("Button tapped")
}
Also import UIKit
Related
I would to do a programmed button without the storyboard. The problem is that I can not call the button in a separate function like I can when I drag and drop a UIButton from a storyboard into a view controller. I do not want to use the storyboard at all.
//Trying to Create a var for btn
override func viewDidLoad() {
super.viewDidLoad()
let btn = UIButton(type: .custom) as UIButton
btn.backgroundColor = .blue
btn.setTitle("Button", for: .normal)
btn.frame = CGRect(x: 100, y: 100, width: 200, height: 100)
btn.addTarget(self, action: #selector(clickMe), for: .touchUpInside)
self.view.addSubview(btn)
}
#objc func clickMe(sender:UIButton!) {
print("Button Clicked")
}
func place() {
//do something to btn.
}
Read about variable scopes. In the question you have declared your button inside the method/function which restricts the scope of its usage within the method. When you declare the variable within the scope of the class/struct you can use it within other methods/functions.
let btn = UIButton(type: .custom)
override func viewDidLoad() {
super.viewDidLoad()
btn.backgroundColor = .blue
// .. other settings here
self.view.addSubview(btn)
}
#objc
func clickMe(sender:UIButton) {
print("Button Clicked")
}
func place() {
btn.backgroundColor = .red
}
I am trying to set a listener function for my button but I keep getting error . Here is how've done it :
import UIKit
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
//Create the BackGround
self.view.backgroundColor = UIColor.black
let startButton = UIButton()
startButton.setTitle( " start ", for: UIControlState.normal)
startButton.setTitleColor(UIColor.blue, for: UIControlState.normal)
startButton.frame = CGRect(x: 10, y:40 , width: 250, height: 25)
self.view.addSubview(startButton)
startButton.addTarget(self, action: "buttonPressed:" , for: .touchUpInside)
}
func buttonPressed(sender: UIButton!) {
print("hello")
}
}
Does any one knows where am I making the mistake ? :)
You were close. Just replace
startButton.addTarget(self, action: "buttonPressed:" , for: .touchUpInside)
with this:
startButton.addTarget(self, action: #selector(ViewController.buttonPressed(sender:)) , for: .touchUpInside)
I have two view controllers, ViewControllerA and ViewControllerB, where ViewControllerB is a subclass of ViewControllerA. Here is the code implementation in ViewControllerA.
class ViewControllerA: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
createButton()
buttonPressed()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
func createButton () {
let button = UIButton();
button.setTitle("Add", forState: .Normal)
button.setTitleColor(UIColor.blueColor(), forState: .Normal)
button.frame = CGRectMake(200, 65, 46, 30) // X, Y, width, height
button.addTarget(self, action: "buttonPressed:", forControlEvents: .TouchUpInside)
self.view.addSubview(button)
}
func buttonPressed(sender: UIButton!) {
var alertView = UIAlertView();
alertView.addButtonWithTitle("Done");
alertView.title = "Alert!";
alertView.message = "Button Pressed!!!";
alertView.show();
}
In my ViewControllerB I would like to use createButton() but not the way it was used in ViewControllerA. Let's say I want to have red color not blue in ViewControllerB in createButton(). How can I override createButton() to be different in ViewControllerB?
The following would work.
class ViewControllerA: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
createButton()
buttonPressed()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
func createButton () {
let button = UIButton();
button.setTitle("Add", forState: .Normal)
button.setTitleColor(UIColor.blueColor(), forState: .Normal)
button.frame = CGRectMake(200, 65, 46, 30) // X, Y, width, height
button.addTarget(self, action: "buttonPressed:", forControlEvents: .TouchUpInside)
self.view.addSubview(button)
}
func buttonPressed(sender: UIButton!) {
var alertView = UIAlertView();
alertView.addButtonWithTitle("Done");
alertView.title = "Alert!";
alertView.message = "Button Pressed!!!";
alertView.show();
}
class ViewControllerB: ViewControllerA {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
override func createButton () {
let button = UIButton();
button.setTitle("Add", forState: .Normal)
button.setTitleColor(UIColor.blueColor(), forState: .Normal)
button.frame = CGRectMake(200, 65, 46, 30) // X, Y, width, height
button.addTarget(self, action: "buttonPressed:", forControlEvents: .TouchUpInside)
self.view.addSubview(button)
}
override func buttonPressed(sender: UIButton!) {
var alertView = UIAlertView();
alertView.addButtonWithTitle("Done");
alertView.title = "Alert!";
alertView.message = "Button Pressed!!!";
alertView.show();
}
Hopefully can help those who are also trying to understand the best way of view controller subclassing.
I'm trying to create a custom one-button AlertViewController in Swift, I use window.addSubview() to show the AlertView, but when touch the button on the AlertView, the func buttonTapped() is not working, below is my code, please tell me what's wrong here, thanks.
MyAlertViewController.swift
class MyAlertViewController: UIViewController {
var button: UIButton!
var contentView: UIView!
override func viewDidLoad() {
super.viewDidLoad()
setUp()
}
func setUp(){
contentView = UIView(frame: CGRectMake(0,0,200,300))
contentView.center = view.center
contentView.backgroundColor = UIColor.whiteColor()
contentView.layer.cornerRadius = 10
button = UIButton(type: .System)
button.frame = CGRectMake(50, 150, 100, 40)
button.setTitle("button", forState: UIControlState.Normal)
button.addTarget(self, action: #selector(buttonTapped(_:)), forControlEvents: UIControlEvents.TouchUpInside)
view.addSubview(contentView)
contentView.addSubview(button)
view.backgroundColor = UIColor(red: 0, green: 0, blue: 0, alpha: 0.1)
view.userInteractionEnabled = true
}
func buttonTapped(sender: AnyObject?){
print("button tapped")
}
func show(){
let window = UIApplication.sharedApplication().keyWindow! as UIWindow
view.frame = window.bounds
window.addSubview(view)
}
}
ParentViewController.swift (is the rootViewController of my window)
class ParentViewController: UIViewController {
var button: UIButton!
override func viewDidLoad() {
super.viewDidLoad()
view.backgroundColor = UIColor.whiteColor()
button = UIButton(type: .System)
button.frame = CGRectMake(110,269,100,30)
button.setTitle("show", forState: .Normal)
button.addTarget(self, action: #selector(buttonTapped(_:)), forControlEvents: .TouchUpInside)
view.addSubview(button)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
func buttonTapped(sender: AnyObject?){
MyAlertViewController().show()
}
}
I found out if I change ParentViewController.swift as below, the button on the alert view can work correctly.
class ParentViewController: UIViewController {
var button: UIButton!
var vc: MyAlertViewController!
override func viewDidLoad() {
super.viewDidLoad()
view.backgroundColor = UIColor.cyanColor()
button = UIButton(type: .System)
button.frame = CGRectMake(110,269,100,30)
button.setTitle("show", forState: .Normal)
button.addTarget(self, action: #selector(buttonTapped(_:)), forControlEvents: .TouchUpInside)
view.addSubview(button)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
func buttonTapped(sender: AnyObject?){
vc = MyAlertViewController()
vc.show()
}
}
But I intended to show this alert view each time the user get new messages,so it would show up at any time and on any ViewController , so I don't want to declare it in each ViewController, so How can I solve this?
TLDR;
You have to retain an instance of MyAlertViewController().
Change to this and it will work (as you've already done):
// keep some reference
var alert: MyAlertViewController?
func buttonTapped(sender: AnyObject?){
alert = MyAlertViewController()
alert?.show()
}
More explanation
The button.addTarget(self, ...) that is called inside MyAlertViewController does not retain self.
The last line of the doc of addTarget function said that:
// ... the action cannot be NULL. Note that the target is not retained.*
So there will be no self to send action to after leaving of this function:
func buttonTapped(sender: AnyObject?){
MyAlertViewController().show()
}
Another option,
is to keep self variable in MyAlertViewController:
// retain self manually
var mySelf: MyAlertViewController?
func setUp(){
...
// reference to self
mySelf = self
button.addTarget(mySelf, action: #selector(buttonTapped(_:)), forControlEvents: UIControlEvents.TouchUpInside)
}
I'm trying to make an application where i have to touch a button who is moving up the screen. I am using CADisplayLinkfor this. The problem in my code is that its creating a new button instead of using a specific one:
#IBOutlet var label: UILabel!
#IBOutlet var button2: UIButton!
#IBAction func button1(sender: UIButton) {
label.hidden = true
button2 = UIButton(type: UIButtonType.System) as UIButton
button2.frame = CGRectMake(120, 400, 100, 100)
button2.setTitle("Test Button", forState: UIControlState.Normal)
button2.addTarget(self, action: "buttonAction:", forControlEvents: UIControlEvents.TouchUpInside)
self.view.addSubview(button)
let displayLink = CADisplayLink(target: self, selector: "handleDisplayLink:")
displayLink.addToRunLoop(NSRunLoop.currentRunLoop(), forMode: NSDefaultRunLoopMode)
}
func handleDisplayLink(displayLink: CADisplayLink) {
var buttonFrame = button.frame
buttonFrame.origin.y += -2
button2.frame = buttonFrame
if button2.frame.origin.y <= 50 {
displayLink.invalidate()
}
}
func buttonAction(sender: UIButton) {
sender.alpha = 0
label.hidden = false
}
override func viewDidLoad() {
super.viewDidLoad()
label.hidden = true
}
Thank you.
Anton
You can delete
button2 = UIButton(type: UIButtonType.System) as UIButton
button2.frame = CGRectMake(120, 400, 100, 100)
button2.setTitle("Test Button", forState: UIControlState.Normal)
And just set these attributes in Interface Builder.
Also this line:
self.view.addSubview(button)
No longer makes sense after you edited your question, but it is also not necessary if your buttons are IBOutlets.
This line:
var buttonFrame = button.frame
Only makes sense if button is an IBOutlet declared elsewhere.