UIAlertController用于大量具有滚动功能的文本?

时间:2014-11-20 05:35:50

标签: ios iphone swift ios8

UIAlertController显示小字符串。当字符串的大小变大时。然后缺少此警报中的一些文本。是否有可用的滚动功能,以便滚动到剩余的文本?

var alert = UIAlertController(title: "title here", message: "about 30 line text here", preferredStyle: UIAlertControllerStyle.Alert)
    alert.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.Default, handler: nil))
    self.presentViewController(alert, animated: true, completion: nil)

如何发出警告,向我显示大于30行的文字。我使用swift。

2 个答案:

答案 0 :(得分:2)

它在Playgrounds中不费吹灰之力:

import UIKit
import PlaygroundSupport

PlaygroundPage.current.needsIndefiniteExecution = true
let window = UIWindow()
let viewController = UIViewController()
window.rootViewController = viewController
window.makeKeyAndVisible()

let message = (1...1000).reduce("") { $0 + "\($1)\n" }

let alert = UIAlertController(title: "title here", message: message, preferredStyle: .alert)
alert.addAction(UIAlertAction(title: "action here", style: .default, handler: nil))
viewController.present(alert, animated: true, completion: nil)
PlaygroundPage.current.liveView = window

我有1000条可滚动的行:

1000 scrollable lines

答案 1 :(得分:-2)

在Objective C中它对我来说很好:

 NSString* messageString = @"Enter your Long text here";

 UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"Title"
                                                                     message:messageString
                                                              preferredStyle:UIAlertControllerStyleAlert];

 alertController.view.frame = [[UIScreen mainScreen] applicationFrame];

 [alertController addAction:[UIAlertAction actionWithTitle:@“OK”
                                                style:UIAlertActionStyleDefault
                                              handler:^(UIAlertAction *action){
                                                  [self okButtonTapped];
                                              }]];
 [self presentViewController:alertController animated:YES completion:nil];

对于Swift,您可以使用此代码:

let alertController = UIAlertController(title: "Your Title", message: "Your Text", preferredStyle: .Alert)

let cancelAction = UIAlertAction(title: "Cancel", style: .Cancel) { (action) in
    println(action)
}
alertController.addAction(cancelAction)

let destroyAction = UIAlertAction(title: "Destroy", style: .Destructive) { (action) in
    println(action)
}
alertController.addAction(destroyAction)

self.presentViewController(alertController, animated: true) {
 // ...
}