V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
iOS 开发实用技术导航
NSHipster 中文版
http://nshipster.cn/
cocos2d 开源 2D 游戏引擎
http://www.cocos2d-iphone.org/
CocoaPods
http://cocoapods.org/
Google Analytics for Mobile 统计解决方案
http://code.google.com/mobile/analytics/
WWDC
https://developer.apple.com/wwdc/
Design Guides and Resources
https://developer.apple.com/design/
Transcripts of WWDC sessions
http://asciiwwdc.com
Cocoa with Love
http://cocoawithlove.com/
Cocoa Dev Central
http://cocoadevcentral.com/
NSHipster
http://nshipster.com/
Style Guides
Google Objective-C Style Guide
NYTimes Objective-C Style Guide
Useful Tools and Services
Charles Web Debugging Proxy
Smore
Elethom
V2EX  ›  iDev

兼容 iOS 7 的 Alert Controller

  •  
  •   Elethom ·
    Elethom · 2014-11-01 14:11:27 +08:00 · 3524 次点击
    这是一个创建于 3485 天前的主题,其中的信息可能已经有所发展或是发生改变。

    Features:

    • 和 UIAlertController 使用相同的 API;
    • 自帶 -show-dismiss, 不需要考慮從哪個 view controller present 的問題;
    • 自動忽略長度為 0 的 title, 不會在 iOS 8 出現一個難看的空 title.

    Known issues:

    • text field 在 iOS 7 和 iOS 8 的差別太大, 無法同時支持;
    • controller 繼承自 NSObject, 無法當做普通的 view controller 使用 (下個版本修改).

    https://github.com/Elethom/PRAlertController

    PRAlertController

    Cocoapods

    Alert controller with the same APIs as iOS 8 SDK (text field not supported), compatible with iOS 7.

    Installation

    With CocoaPods

    In your Podfile:

    pod 'PRAlertController'
    

    Usage

    It has exactly the same APIs as iOS 8 SDK does except the text field part. Besides, with -show, -dismiss methods you can pop up alerts wherever you want without having to worry about how to get current view controller's pointer.

    Example:

    PRAlertController *alertController = [PRAlertController alertControllerWithTitle:@"Title"
                                                                             message:@"This is the message."
                                                                      preferredStyle:PRAlertControllerStyleAlert];
    PRAlertAction *firstAction = [PRAlertAction actionWithTitle:@"First"
                                                          style:PRAlertActionStyleDefault
                                                        handler:^(PRAlertAction *action) {
                                                            [self doFirstAction];
                                                        }];
    PRAlertAction *secondAction = [PRAlertAction actionWithTitle:@"Second"
                                                           style:PRAlertActionStyleDefault
                                                         handler:^(PRAlertAction *action) {
                                                             [self doSecondAction];
                                                         }];
    PRAlertAction *destructiveAction = [PRAlertAction actionWithTitle:@"Destructive"
                                                                style:PRAlertActionStyleDestructive
                                                              handler:^(PRAlertAction *action) {
                                                                  [self doDestructiveAction];
                                                              }];
    PRAlertAction *cancelAction = [PRAlertAction actionWithTitle:@"Cancel"
                                                           style:PRAlertActionStyleCancel
                                                         handler:^(PRAlertAction *action) {
                                                             [self doCancelAction];
                                                         }];
    [alertController addAction:firstAction];
    [alertController addAction:secondAction];
    [alertController addAction:destructiveAction];
    [alertController addAction:cancelAction];
    [alertcontroller show];
    

    Easy as it seems.

    License

    This code is distributed under the terms and conditions of the MIT license.

    Donate

    You can support me by:

    :-)

    Contact

    13 条回复    2014-11-03 17:44:35 +08:00
    yellowV2ex
        1
    yellowV2ex  
       2014-11-01 16:43:09 +08:00
    输入帐号密码或文本的那种alert怎么写?
    xhacker
        2
    xhacker  
       2014-11-01 16:54:38 +08:00 via iPhone
    @Livid: 在 iOS 上代码的字体有问题。
    Elethom
        3
    Elethom  
    OP
       2014-11-03 16:22:54 +08:00
    @xhacker
    Mac 上也有問題.
    Elethom
        4
    Elethom  
    OP
       2014-11-03 16:25:40 +08:00
    @xhacker
    @Livid
    Fallback 中的字體我一個都沒有...
    Livid
        5
    Livid  
    MOD
       2014-11-03 16:29:16 +08:00
    @xhacker
    @Elethom

    收到,我现在调整。
    Elethom
        6
    Elethom  
    OP
       2014-11-03 16:31:46 +08:00
    @Livid
    建議調整為可左右滾動的, 折行很難看.
    Livid
        7
    Livid  
    MOD
       2014-11-03 16:31:47 +08:00
    @xhacker
    @Elethom

    现在加入了 Menlo
    Elethom
        8
    Elethom  
    OP
       2014-11-03 16:37:49 +08:00
    @Livid
    搜索了一下, 原來 Panic Sans 是 Coda 附帶的字體, 難怪叫做 "Panic". XD
    Livid
        9
    Livid  
    MOD
       2014-11-03 16:44:24 +08:00
    @Elethom 个人最爱的代码字体。
    Elethom
        10
    Elethom  
    OP
       2014-11-03 16:50:46 +08:00
    @Livid

    似乎沒有單獨的下載?

    試了一下, 給代碼部份的 CSS 加上:
    white-space: pre;
    overflow-x: scroll;
    這樣比較好, 可以橫向滾動, 換行不好看.
    Livid
        11
    Livid  
    MOD
       2014-11-03 16:52:33 +08:00
    @Elethom 好的,现在调整。
    Esay
        12
    Esay  
       2014-11-03 17:06:29 +08:00
    酷,给你补个截图(代码最后一行的 Controller 大小写写错了)

    Elethom
        13
    Elethom  
    OP
       2014-11-03 17:44:35 +08:00
    @Livid
    似乎還沒 update?

    @Esay
    Thanks! 已修改.
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   实用小工具   ·   5640 人在线   最高记录 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 25ms · UTC 08:23 · PVG 16:23 · LAX 01:23 · JFK 04:23
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.