Question

Create a button in Swift (Xcode) that will create a charge, create a charge using Stripe's...

Create a button in Swift (Xcode) that will create a charge, create a charge using Stripe's API.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Creating your first charge using Xcode and Sublime with Stripe on a Swift iPhone 6


In iOS app development sometime we’ve to create Buttons programmatically or dynamically.
Let’s create a new Single View iOS application in Xcode. Name the project “MyDynamicButton”. Select swift as programming language.
Now go to ViewController.swift. In ViewDidLoad method we’ll write some codes that will add one button to the screen automatically at run time.
Let’s create a new method createButton and write some codes inside the method.
   func createButton () {
let button = UIButton();
button.setTitle("Add", for: .normal)
button.setTitleColor(UIColor.blue, for: .normal)
button.frame = CGRect(x: 15, y: 50, width: 200, height: 100)
self.view.addSubview(button)
}

Explanation:
First we initialize a button object and keep it in button variable
1   let button = UIButton();
Then we set title and color of title of it.
button.setTitle("Add", for: .normal)
button.setTitleColor(UIColor.blue, for: .normal)

In the following line of code, we add position and size of the button.

button.frame = CGRect(x: 15, y: 50, width: 200, height: 100)

Add the button to view.

   self.view.addSubview(button)

Finally let’s call the createButton in viewDidLoad method.
   override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
createButton()
}

Now if you run the project (cmd + R) you can see a button created. If you tap on the button, nothing will happen at this moment. To call a method when button is pressed, we can write:
button.addTarget(self, action: "buttonPressed:", for: .touchUpInside)
Do not forget to add ‘:’ after action method name (i.e. buttonPressed:). In action parameter of .addTarget, you set the method name that should be called. Now write buttonPressed method.
func buttonPressed(sender: UIButton!) {
let alertView = UIAlertView();
alertView.addButton(withTitle: "OK");
alertView.title = "Alert";
alertView.message = "Button Pressed!!!";
alertView.show();
}
The buttonPressed: method simply displays an alert view. Now run the project and view result.


The whole ViewController.swift will look like this:
   import UIKit

class ViewController: UIViewController {

override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
createButton()
}

override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
  
  
func createButton () {
let button = UIButton();
button.setTitle("Add", for: .normal)
button.setTitleColor(UIColor.blue, for: .normal)
button.frame = CGRect(x: 15, y: 50, width: 200, height: 100)
self.view.addSubview(button)
}
  
func buttonPressed(sender: UIButton!) {
let alertView = UIAlertView();
alertView.addButton(withTitle: "OK");
alertView.title = "Alert";
alertView.message = "Button Pressed!!!";
alertView.show();
}
}

Add a comment
Know the answer?
Add Answer to:
Create a button in Swift (Xcode) that will create a charge, create a charge using Stripe's...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT