我想创建以下内容UIAlertAction:

@IBAction func buttonUpgrade(sender: AnyObject) {
let alertController = UIAlertController(title: "Title",
message: "Message",
preferredStyle: UIAlertControllerStyle.Alert)
let cancelAction = UIAlertAction(title: "Cancel", style: .Cancel) { (action) in
// ...
}
alertController.addAction(cancelAction)
}
Run Code Online (Sandbox Code Playgroud)
我知道a UIAlertController是用a初始化的title,message并且它是否更喜欢显示为警报或操作表.
按下按钮时,我想显示警报,但alert.show()不起作用.为什么我的代码不起作用?
有时我的应用程序会在两个UIAlertViews尝试同时出现时崩溃.如何UIAlertview在显示另一个之前检查另一个是否正在显示?
这是我尝试过的,但它不起作用.
注意:我正在使用swift进行这个项目.
if ViewController.isVisable == false {
self.presentViewController(AlertView, animated: true, completion: nil)
}
Run Code Online (Sandbox Code Playgroud) 我有这行代码:
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:title message:nil preferredStyle:UIAlertControllerStyleActionSheet];
UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:cancelTitle style:UIAlertActionStyleCancel handler:^(UIAlertAction * action) {
NSLog(@"cancel registration");
}];
[alertController addAction:cancelAction];
alertController.view.tintColor = [UIColor redColor];
Run Code Online (Sandbox Code Playgroud)
我想在选择时更改取消按钮颜色.我怎样才能做到这一点?请帮忙.
我想在Playground玩UIAlertController.是否有可能从XCPlaygroundPage获取ViewController - 能够调用presentViewController?
技术:Objective-C,xCode 7
@property (nonatomic, copy) NSString *notes;
@synthesize notes;
example.notes = @"Click <a href='http://www.google.com'>here</a>";
NSString *msg = [@"" stringByAppendingFormat:@"%@", myAnnotation.notes];
UIAlertController *alertViewController = [UIAlertController alertControllerWithTitle: @"Title of Alert Box" message: msg preferredStyle: UIAlertControllerStyleAlert];
[alertViewController addAction: [UIAlertAction actionWithTitle: @"Close" style: UIAlertActionStyleCancel handler: nil]];
[self presentViewController: alertViewController animated: YES completion: nil];
Run Code Online (Sandbox Code Playgroud)
试图使链接出现在警报框中,但没有运气。只是输出为纯文本。
这有可能吗?如果是这样,使用上面的示例,您将如何实现它?
如何更改 UIAlertController 中的一个 UIAlertAction 而不是所有操作按钮的文本颜色。
这是我的 UIAlertController 代码:我想将删除 UIAlertAction 文本颜色更改为红色。
//Alert to select more method
func moreAlert()
{
let optionMenu = UIAlertController(title: "Mere", message: "", preferredStyle: UIAlertControllerStyle.ActionSheet)
let Edit = UIAlertAction(title: "Rediger", style: .Default, handler: { action in
})
let Delete = UIAlertAction(title: "Slet", style: .Default, handler: { action in
})
let cancelAction = UIAlertAction(title: "Annullere", style: .Cancel, handler: { action in
print("Cancelled")
})
optionMenu.addAction(Edit)
optionMenu.addAction(Delete)
optionMenu.addAction(cancelAction)
self.presentViewController(optionMenu, animated: true, completion: nil)
}
Run Code Online (Sandbox Code Playgroud) 我打算使用,UIAlertController但是当我想展示它时,我看到了这个错误:
使用未解析的标识符“存在”
这是我的代码:
func showAllert(title: String, msg: String, vc: UIViewController){
let alert = UIAlertController(title: title, message: msg, preferredStyle: .alert)
let action = UIAlertAction(title: "ok", style: .default, handler: nil)
alert.addAction(action)
present(alert, animated: true, completion: nil)
}
Run Code Online (Sandbox Code Playgroud) UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"alert" message:nil preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *action = [UIAlertAction actionWithTitle:@"action" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
[self doSomething];
}];
[alert addAction:action];
[self presentViewController:alert animated:YES completion:nil];
Run Code Online (Sandbox Code Playgroud)
我理解这个循环.self保留UIAlertController,UIAlertController保留UIAlertAction,UIAlertAction保留self
我的意思是内部不能将这个类设计为在UIAlertActions运行之后释放所有内容吗?
-
为了澄清,我知道通过使用对自我的弱引用可以避免这个问题.
我要问的是,UIAlertController一旦用户选择了一个动作,为什么不会将所有动作(以及它们的处理程序块)都清零.这将打破周期,避免我们需要做的整个弱势舞蹈.
像这样......
@implementation UIAlertController
...
// An action button was pressed
- (void)actionSelectedIndex:(NSInteger)index
{
UIAlertAction *action = self.actions[index];
action.handler(action); // Run the action handler block
self.actions = nil; // Release all the actions
}
Run Code Online (Sandbox Code Playgroud) memory-management ios objective-c-blocks retain-cycle uialertcontroller
我不知道为什么文本被截断了UIAlertController.
我不知道我能做些什么.
我是否无意中更改了任何设置Xcode?
let title = "Erro ao verificar usuário no servidor"
let message = "Não foi possível atualizar os dados. Tente novamente mais tarde"
let alert = UIAlertController(title: title, message: message, preferredStyle: .alert)
let ok = UIAlertAction(title: "Ok", style: .default, handler: nil)
alert.addAction(ok)
present(alert, animated: true, completion: nil)
Run Code Online (Sandbox Code Playgroud)

我需要使用UIAlertContoller,因为 SwiftUIAlert不支持TextField.
由于各种原因(辅助功能、动态类型、深色模式支持等),我不能使用自定义创建的 AlertView。
基本思想是,SwiftUI 的警报必须保留TextField并且输入的文本必须反射回来以供使用。
view我通过遵循UIViewControllerRepresentable以下工作代码创建了 SwiftUI 。
struct AlertControl: UIViewControllerRepresentable {
typealias UIViewControllerType = UIAlertController
@Binding var textString: String
@Binding var show: Bool
var title: String
var message: String
func makeUIViewController(context: UIViewControllerRepresentableContext<AlertControl>) -> UIAlertController {
let alert = UIAlertController(title: title, message: message, preferredStyle: .alert)
alert.addTextField { textField in
textField.placeholder = "Enter some text"
}
let cancelAction = UIAlertAction(title: "cancel", style: .destructive) { (action) in
self.show = false …Run Code Online (Sandbox Code Playgroud) ios ×9
swift ×6
objective-c ×2
xcode7 ×2
retain-cycle ×1
swiftui ×1
uialertview ×1
uikit ×1
xcode ×1