Giter Site home page Giter Site logo

mironal / shorthandalert Goto Github PK

View Code? Open in Web Editor NEW
6.0 2.0 0.0 1.85 MB

A helpful UIAlertController extension and Builder for the slothful human.

Home Page: https://mironal.github.io/ShortHandAlert/

License: MIT License

Objective-C 16.26% Swift 82.84% Shell 0.90%
swift objective-c ios uialertcontroller

shorthandalert's Introduction

ShortHandAlert ๐Ÿ“๐Ÿ’จ

A very simple extension of UIAlertController for shortening your source.


Why

Hmmmm... I want to display Alert or ActionSheet to users. Let's write the code immediately.

let alert = UIAlertController(title: "title", message: nil, preferredStyle: .actionSheet)

// I want...

// How many times have you written this UIAlertAction in my whole life? ๐Ÿ˜ญ๐Ÿ˜ญ
 alert.addAction(UIAlertAction(title: "Some", style: .default, handler: { _ in
     print("do some thing...")
 }))

 alert.addAction(UIAlertAction(title: "Another", style: .default, handler: { _ in
     print("do some thing...")
 }))

 //              โ†“ Why do I have to write UIAlertAction many times? ๐Ÿ˜ฑ๐Ÿ˜ฑ
 alert.addAction(UIAlertAction(title: "Cancel", style: .cancel))

 present(alert, animated: true)

From today you can write shorter. ๐Ÿคฃ

Usage

Action Sheet

// Without action handler.
UIAlertController(title: "Title", message: nil, preferredStyle: .actionSheet)
    .default(".default style title")
    .destructive(".destructive style title")
    .cancel()
    .present(in: self)

// With action handler.
UIAlertController(title: "Title", message: nil, preferredStyle: .actionSheet)
    .default(".default style title") { _ in
        print("select default")
    }.destructive(".destructive style title") { _ in
        print("select destructive")
    }.cancel("custom cancel title") { _ in
        print("select cancel")
    }.present(in: self, animated: false) { // custom present behavior.
        print("do some thing after the presentation finishes.")
    }

Alert

 // Without action handler.
UIAlertController(title: "Alert title", message: nil, preferredStyle: .alert)
    .ok()
    .cancel()
    .present(in: self)

// With action handler and text field.
UIAlertController(title: "Alert title", message: "some message.", preferredStyle: .alert)
     // Add TextField with default text and placeholder.
    .textField("default text", placeholder: "placeholder")
    // add custom button.
    .default("(ใฃ๏ผžฯ‰๏ผœc)") { (_, textFields) in
        print("do some thing with", textFields?.first?.text ?? "")
    }
    // add cancel button.
    .cancel()
    .present(in: self)

Conditional add action

#6

UIAlertController(title: "Alert title", message: nil, preferredStyle: .alert)
    .default("with action handler", addAction: true) { _ in }
    .ok(addAction: true) { _ in }
    .destructive("with action handler", addAction: true) { _ in }
    .cancel(addAction: true)
    .present(in: self)

Alert Builder

AlertBuilder makes it easy to create commonly used alerts.

AlertBuilder()
    .confirm(title: nil, message: "Some message") // or .confirm(error: error)
    .approve()
    .present(in: self)
AlertBuilder()
    .suggest(title: nil, message: "Do you want to retry?") // or .suggest(error: error)
    .cancel()
    .approve(title: "Retry") { _ in

    }
    .present(in: self)

Using AlertBuilder makes the suggestion of completion very concise.

AlertBuilder gif

Requirements

  • iOS 9.3+
  • Support Swift 4+ & Objective-C

Installation

Using Carthage

Add github "mironal/ShortHandAlert to you Cartfile.

github "mironal/ShortHandAlert"

Don't forget.

carthage1 carthage2

Demo

for Swift

see /Demo directory.

for Objective-c

see /DemoObjc directory.

Demo

API document

The API Document is Here

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.