Play again button doesn't work - xcode

When I press the play again button it doesn't work. I just have this code involving the play again button. What should I add here? How can I add a function to playAgain button? Here is my code:
let playAgain: UIButton = UIButton(frame: CGRectMake(-10, 400, 400, 150))
playAgain.setTitle("Play Again", forState: UIControlState.Normal)
playAgain.titleLabel!.font = UIFont(name: "Helvetica", size: 50)
playAgain.addTarget(self, action: "buttonAction:", forControlEvents: UIControlEvents.TouchUpInside)
playAgain.tag = 1
self.view!.addSubview(playAgain)

Implement a function with any name you like, like this:
func buttonPressed(sender: UIButton) {
// do something
}
Then set this function as the action of your button:
playAgain.addTarget(self, action: "buttonPressed:", forControlEvents: .TouchUpInside)

Related

Swift, Xcode: Changing UIButton Background and Text on Click

I want to change the background and text of the button on click, I tried several SO solutions but they haven't worked, you can see what I tried in my project:
https://github.com/jzhang172/modalTest
I tried debugging it by putting a simple print statement and it looks like it doesn't ever go to it.
UIButton's have a method for setting the title color. So if you had a UIButton IBOutlet named myBtn:
myBtn.setTitleColor(UIColor.blackColor(), forState: .Highlighted)
and to change the text of the button on touch:
myBtn.setTitle("This button was touched", forState: .Highlighted)
As far as setting the background color, you could add an extension for your UIButton which allows you to do this:
extension UIButton {
private func imageWithColor(color: UIColor) -> UIImage {
let rect = CGRectMake(0.0, 0.0, 1.0, 1.0)
UIGraphicsBeginImageContext(rect.size)
let context = UIGraphicsGetCurrentContext()
CGContextSetFillColorWithColor(context, color.CGColor)
CGContextFillRect(context, rect)
let image = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
return image
}
func setBackgroundColor(color: UIColor, forUIControlState state: UIControlState) {
self.setBackgroundImage(imageWithColor(color), forState: state)
}
}
Then you could do:
myBtn.setBackgroundColor(UIColor.grayColor(), forUIControlState: .Highlighted)
Hope this helps!
SOLUTION:
1) Create an IBAction from your UIButton and also an IBOutlet called button.
EDIT: As per your request (How to trigger the even when the button is TOUCHED, not RELEASED?):
2) Do this:
#IBAction func changes (sender: UIButton) {
button.backgroundColor = UIColor.blueColor()
button.setTitle("Button Title", forState: UIControlState.Normal)
}
Your UIButton #IBOutlet closePop is hooked up to a single #IBAction of the exact same name (closePop()). That closePop() IBAction ONLY dismisses the helpViewController - it doesn't do anything about the text or button color.
Your other #IBAction function like, in which you try to set the color & print "Is this even working?", is not hooked up to the button, and is never called.

1 UIImagePickerController and 2 buttons

i have an UIImagePickerController and 2 buttons. When I tap on button 1 I want to set the image of button 1. And when I tap on button 2 I want to set the image of button 2. I've successfully set the image of button 1 using UIImagePickerController but failed to do so with button 2. Here's my code:
var whichButton: Int = 0
func displayImagePicker(){
let imagePicker = UIImagePickerController()
imagePicker.delegate = self
imagePicker.sourceType = UIImagePickerControllerSourceType.PhotoLibrary
imagePicker.allowsEditing = false
self.presentViewController(imagePicker, animated: true, completion: nil)
}
#IBAction func chooseImageOne(sender: AnyObject) {
displayImagePicker()
whichButton = 1
}
#IBAction func chooseImageTwo(sender: AnyObject) {
displayImagePicker()
whichButton = 2
}
func imagePickerController(picker: UIImagePickerController, didFinishPickingImage image: UIImage, editingInfo: [String : AnyObject]?) {
self.dismissViewControllerAnimated(true, completion: nil)
if whichButton == 1 {
self.imageOne.setImage(image, forState: UIControlState.Normal)
} else if whichButton == 2 {
self.imageTwo.setImage(image, forState: UIControlState.Normal)
}
}
As you can see I tried having a var to keep track of which button I'm tapping on. I'm not sure what I've done in the didFinishPickingImage func is right. I'm very new to swift, if anyone can shed some light it will really be great! thanks in advance
I have personally checked your code on xcode and its correct. Please see if you have given IBOutlet to self.imageTwo button and also check for the IBAction i.e touchupInside for the imageTwo button. else everything is fine
Your code is functionally correct. Looks like your #IBAction functions are not hooked up correctly, or the #IBOutlet of the second button. You could easily check this with print statements in the button handlers, or in the debugger.

How do I make a custom keyboard button to dismiss

How do I make a button to dismiss keyboard
the key is located above the keyboard?
custom keyboard
var button: UIButton = UIButton()
required init(globalColors: GlobalColors.Type?, darkMode: Bool, solidColorMode: Bool) {
super.init(globalColors: globalColors, darkMode: darkMode, solidColorMode: solidColorMode)
self.addSubview(self.button)
button.setTitle("Hide", forState: UIControlState.Normal)
self.button.addTarget(self, action: Selector("tappedbutton"), forControlEvents: UIControlEvents.TouchUpInside)
button.setTitleColor(UIColor(red: 24.0/100, green: 116.0/255, blue: 205.0/205, alpha: 1.0), forState: UIControlState.Normal)
self.updateAppearance()
}
override func layoutSubviews() {
super.layoutSubviews()
self.button.frame.origin = CGPointMake(self.button.frame.origin.x + self.button.frame.width + 1, self.button.frame.origin.y)
}

how to set action backBarButtonItem in swift?

How to to set action backBarButtonItem and still display left arrow?
I used this code but the arrow was not display
var barBack = UIBarButtonItem(title: "Reset", style: UIBarButtonItemStyle.Plain, target: self, action: "reset:")
self.navigationItem.leftBarButtonItem = barBack
but when I used this code the action not working
self.navigationController?.navigationBar.topItem?.backBarButtonItem = barBack
Thanks
As an alternative to setting an action, you can keep the backBarButtonItem and use isMovingFromParentViewController instead to handle logic for moving back on the navigation stack.
override func viewWillDisappear(animated: Bool) {
super.viewWillDisappear(animated)
if isMovingFromParentViewController() {
.. do something here
}
}
Try this
var barBack = UIBarButtonItem(title: "Reset", style: UIBarButtonItemStyle.Plain, target: self, action: "reset:")
self.navigationItem.leftBarButtonItem = barBack
Let me know if it works.
Try this:
In ViewDidLoad
let backButton = UIBarButtonItem(title: "< Home", style: UIBarButtonItemStyle.Plain, target: self, action: "goBack")
navigationItem.leftBarButtonItem = backButton
navigationItem.backBarButtonItem?.setTitleTextAttributes([NSFontAttributeName: UIFont(name: "the_font_you_want_to_use", size: size_of_the_font_this_should_be_integer)!], forState: UIControlState.Normal)
and implement the following method:
func goBack() {
self.navigationController?.popToRootViewControllerAnimated(boolean)
}
Just replace the the_font_you_want_to_use, size_of_the_font_this_should_be_integer and boolean with the values you want, and everything will be working.
*** EDIT
If you don't want to get back to Root View Controller, instead of
self.navigationController?.popToRootViewControllerAnimated(boolean)
you can say:
self.navigationController?.popViewControllerAnimated(boolean)
or even pop to some other ViewController with specifying the first parameter of the popToViewController to be the object of YourViewController class, and second the boolean value for animation.

Multiline Label On UIButton Swift

I've been trying to get a sync button to display Last Sync at (time&date). But All I get is one line with shortened text.
// Sync Button
syncBtn.frame = CGRectMake(15, height-60, 120, 40)
syncBtn.addTarget(self, action: "syncBtnPressed", forControlEvents: UIControlEvents.TouchUpInside)
syncBtn.setTitle("Last Sync: Never", forState: UIControlState.Normal)
syncBtn.setTitleColor(UIColor.whiteColor(), forState: UIControlState.Normal)
syncBtn.layer.borderColor = UIColor.whiteColor().CGColor
syncBtn.layer.borderWidth = 1
syncBtn.layer.cornerRadius = 5
syncBtn.titleLabel?.font = UIFont(name: "Avenir", size: 10)
syncBtn.alpha = 0.5
syncBtn.titleLabel?.sizeToFit()
syncBtn.titleLabel?.textAlignment = NSTextAlignment.Center
syncBtn.titleLabel?.numberOfLines = 2
self.view.addSubview(syncBtn)
This is the function to get and set the date on the label
func printTimestamp(){
var timestamp = NSDateFormatter.localizedStringFromDate(NSDate(), dateStyle: .ShortStyle, timeStyle: .ShortStyle)
self.syncBtn.titleLabel?.text = ("Last Sync at: " + timestamp)
}
Anyone able to solve this problem?
create a UIButton with Multiple lines of text
Possible programtically
override func viewDidAppear(animated: Bool){
super.viewDidAppear(animated)
btnTwoLine.setTitle("Mulitple line", forState: UIControlState.Normal)
btnTwoLine.titleLabel!.lineBreakMode = NSLineBreakMode.ByWordWrapping;
}
Use SetTitle method instead of .text , when you use .text it sets the title text but the constraints are not updated accordingly hence you need to use SetTitle method and set the adjustsFontSizeToFitWidth to true
self.syncBtn.setTitle(timeStamp, forState: UIControlState.Normal)
self.syncBtn.titleLabel?.adjustsFontSizeToFitWidth = true

Resources