我正在为我的ios应用程序开发一个谷歌登录教程,当我们无法登录用户到我的应用程序时,有一部分.
到目前为止,appDelegate.swift中的代码部分如下所示:
guard error == nil && data != nil else {
// check for fundamental networking error
print("error=\(error)")
//lets put popup here that says cant connect to server
GIDSignIn.sharedInstance().signOut()
return
}
Run Code Online (Sandbox Code Playgroud)
现在而不是打印错误我想放置警告弹出窗口.我试着在那里写:
let alert = UIAlertController(title: "Alert", message: "Message", preferredStyle: UIAlertControllerStyle.Alert)
alert.addAction(UIAlertAction(title: "Click", style: UIAlertActionStyle.Default, handler: nil))
self.presentViewController(alert, animated: true, completion: nil)
Run Code Online (Sandbox Code Playgroud)
但接着说我接到xcode错误self.presentViewController了value of type AppDelegate has no member presentViewController.
在这种情况下如何显示警告弹出窗口?
我有两个按钮,但我只想将一个
更改为红色.当我使用下面的功能时,它会全部变为红色.我只想改变一个按钮的颜色.我该怎么做?
alertController.view.tintColor = UIColor.redColor()
Run Code Online (Sandbox Code Playgroud) 我创建了一个带有操作表和两个按钮的警报控制器。由于操作表将具有圆形边缘,因此在屏幕上的四个角上会出现白色。我尝试更改警报控制器的背景颜色,但这使操作表成为具有锐边边框而不是圆形边框的矩形。我尝试将视图背景颜色设置为清晰颜色也尝试设置边框半径。这是我的行动表。我希望这些白色边缘不可见。
另外,如何更改取消的背景颜色。
let cancelAction = UIAlertAction(title: "Cancel".localize(), style: .cancel) { _ in
// this is where I am creating the cancelAction
}
Run Code Online (Sandbox Code Playgroud)
编辑 - 1:添加警报控制器代码
func showActionSheet(_ changeAction: UIAlertAction) {
let alertController = UIAlertController(title: "", message: "Here is my alert text".localize(), preferredStyle: .actionSheet)
alertController.view.tintColor = StyleKit.goldenColor
let attributedString = NSAttributedString(string: alertController.message!, attributes: [
NSForegroundColorAttributeName : StyleKit.whiteColor
])
alertController.setValue(attributedString, forKey: "attributedMessage")
if let subview = alertController.view.subviews.first, let alertContentView = subview.subviews.first {
for innerView in alertContentView.subviews {
innerView.backgroundColor = StyleKit.popoverDefaultBackgroundColor
}
} …Run Code Online (Sandbox Code Playgroud) 我想在任何 UIAlertController 由于用户点击警报按钮之一而自行关闭(动画完成)之前和之后执行一些操作并呈现一些 UI。
我怎样才能收到用户按下 UIAlertController 中的某个按钮并且它将被解雇然后又被解雇的通知?
在文档中,建议不要子类化 UIAlertController。我仍然尝试过子类化,认为也许它内部会调用func dismiss(animated flag: Bool, completion: (() -> Void)? = nil)自己。类似的self.dismiss(...,不过iOS10上好像不是这样。
我还尝试将“手动”解雇添加到 UIAlertAction 处理程序中:
let alert = UIAlertController.init(...
let defaultAction = UIAlertAction(title: "OK", style: .default, handler: { action in
alert.dismiss(animated: true, completion: {
print("Dismissed")
})
})
alert.addAction(defaultAction)
Run Code Online (Sandbox Code Playgroud)
但似乎警报在按下按钮后但在调用处理程序之前被解除。无论如何它也不太好用。即使它有效,记住将我的代码添加到每个 UIAlertAction 处理程序中也会有点麻烦。
我将不胜感激任何想法。
我似乎无法在视图启动时弹出警报视图。代码如下。
import UIKit
class StartController: UIViewController
{
override func viewDidLoad()
{
super.viewDidLoad()
self.view.backgroundColor = UIColor.white;
startTest();
}
func startTest()
{
let alerta = UIAlertController(title: "Invalid Test", message: "Testing alert controller", preferredStyle: UIAlertControllerStyle.alert);
alerta.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.default, handler: nil));
self.present(alerta, animated: true, completion: nil);
}
}
Run Code Online (Sandbox Code Playgroud) 我有一个协议来允许我的 ViewController 呈现警报。
import UIKit
struct AlertableAction {
var title: String
var style: UIAlertAction.Style
var result: Bool
}
protocol Alertable {
func presentAlert(title: String?, message: String?, actions: [AlertableAction], completion: ((Bool) -> Void)?)
}
extension Alertable where Self: UIViewController {
func presentAlert(title: String?, message: String?, actions: [AlertableAction], completion: ((Bool) -> Void)?) {
let generator = UIImpactFeedbackGenerator(style: .medium)
generator.impactOccurred()
let alertController = UIAlertController(title: title, message: message, preferredStyle: .alert)
actions.forEach { action in
alertController.addAction(UIAlertAction(title: action.title, style: action.style, handler: { _ in completion?(action.result) })) …Run Code Online (Sandbox Code Playgroud) 我知道 anAlert可以作为 a 的函数呈现Button,但是 anAlert可以在条件中呈现吗?如:
struct ContentView: View {
var body: some View {
Text("Hello World!")
}
}
if isValid {
//present alert
let alert = UIAlertController(title: "My Title", message: "This
is my message.", preferredStyle: UIAlertController.Style.alert)
alert.addAction(UIAlertAction(title: "OK", style:
UIAlertAction.Style.default, handler: nil))
self.present(alert, animated: true, completion: nil)
}
Run Code Online (Sandbox Code Playgroud)
有了这个我得到
“ContentView”类型的值没有成员“present”
我有以下功能会弹出一个 UIAlert,允许用户更新他们的触觉反馈设置:
- (void)requestHapticSetting{
UIWindow *alertWindow = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
alertWindow.rootViewController = [[UIViewController alloc] init];
alertWindow.windowLevel = UIWindowLevelAlert + 1;
[alertWindow makeKeyAndVisible];
if(isHapticOn){
hapticMessage = @"Haptic feedback is currently\nturned ON.\nPlease update preference.";
}
else {
hapticMessage = @"Haptic feedback is currently\nturned OFF.\nPlease update preference.";
}
UIAlertController* alert = [UIAlertController alertControllerWithTitle:@"Haptic Setting"
message:hapticMessage
preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction* onAction = [UIAlertAction actionWithTitle:@"TURN ON" style:UIAlertActionStyleDefault
handler:^(UIAlertAction * action) {
[self saveHapticSettingOn];
}];
UIAlertAction* offAction = [UIAlertAction actionWithTitle:@"TURN OFF" style:UIAlertActionStyleDefault
handler:^(UIAlertAction * action) {
[self …Run Code Online (Sandbox Code Playgroud) 我正在为我的 swift 应用程序使用谷歌登录。当用户点击登录 API 时,会给出这个UIAlert. 有什么办法可以改变它的色调吗?即“取消”和“继续”?
我什至尝试使用下面的 AppDelegate 代码全局更改它;
UIView.appearance(whenContainedInInstancesOf: [UIAlertController.self]).tintColor = Color.brandPurple
Run Code Online (Sandbox Code Playgroud)
这没有效果。
当我在iOS8 Beta5 + Xcode6中显示UIAlertController(ActionSheet)时,运行时异常即将到来.
此Bug仅在iPad设备中发生.
我在使用UIAlertController时遇到了轰鸣声异常.
*由于未捕获的异常'NSGenericException'终止应用程序,原因:'UIPopoverPresentationController(<_UIAlertControllerActionSheetRegularPresentationController:0x15794370>)应该在演示发生之前设置非零的sourceView或barButtonItem.
我的代码显示ActionSheet如下
// Cancel Button
UIAlertAction *actionCancel = [UIAlertAction
actionWithTitle:NSLocalizedString(@"IDS_LABEL_CANCEL", nil)
style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
// cancel
//action handler
[self actionHandler:nil withTag:0 withButtonIndex:0];
}];
// print button
UIAlertAction *actionPrint = [UIAlertAction
actionWithTitle:NSLocalizedString(@"IDS_LABEL_PRINT", nil)
style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
//action handler
[self actionHandler:nil withTag:kAttachmentActionSheetTag withButtonIndex:0];
}];
// Create action sheet
UIAlertController *alertController = [UIAlertController
alertControllerWithTitle:nil message:nil
preferredStyle:UIAlertControllerStyleActionSheet];
[alertController addAction:actionCancel];
[alertController addAction:actionPrint];
// show aciton sheet
[self presentViewController:alertController animated:YES
completion:nil] …Run Code Online (Sandbox Code Playgroud) swift ×7
ios ×6
uialertview ×4
objective-c ×2
cocoa-touch ×1
ios13 ×1
ios8 ×1
swiftui ×1
uikit ×1
uiview ×1
xctest ×1