我使用UIAlertController和UIActivityIndicatorView作为我的加载指示器,它将关闭,直到我的应用程序获得服务器响应.但是当我的服务器或网络出现问题时,我的应用程序永远不会得到响应,我无法从AlertView取回我的控制权,我想知道是否有一些方法可以通过触摸屏幕关闭此AlertView.而我的UIAlertView没有标题和任何按钮.任何提示都会感激不尽.
当我触摸屏幕时它崩溃了,这是我的代码:
UIAlertController *alert = [UIAlertController alertControllerWithTitle:nil
message:@"??????...\n\n"
preferredStyle:UIAlertControllerStyleAlert];
UIActivityIndicatorView *spinner = [[UIActivityIndicatorView alloc]initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
spinner.center = CGPointMake(130.5, 65.6);
spinner.color = [UIColor darkGrayColor];
[spinner startAnimating];
[alert.view addSubview:spinner];
UITapGestureRecognizer *recognizer = [[UITapGestureRecognizer alloc]initWithTarget:alert action:@selector(stopWaiting)];
[alert.view addGestureRecognizer:recognizer];
Run Code Online (Sandbox Code Playgroud) 我知道这是一个非常简单的问题,我从3小时开始研究它,但却无法使其正常工作.我试图实现UIAlertController使用Apple文档显示错误消息,但我在这一行上得到错误,没有已知的选择器presentViewController的类方法[self presentViewController:alert animated:YES completion:nil];我搜索并获得了许多解决方案,但没有在这里工作.AlertMessageViewController是我的自定义类,它继承自UIViewController.
AlertMessageViewController.h
#import <UIKit/UIKit.h>
@interface AlertMessageViewController : UIViewController
+(instancetype)showAlert: (NSString *) title withMessage: (NSString*) message preferredStyle:(UIAlertControllerStyle)preferredStyle;
@end
AlertMessageViewController.m
#import "AlertMessageViewController.h"
#import <UIKit/UIKit.h>
@interface AlertMessageViewController ()
@end
@implementation AlertMessageViewController
- (void)viewDidLoad {
[super viewDidLoad];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
}
+(instancetype)showAlert: (NSString *) title withMessage: (NSString*) message preferredStyle:(UIAlertControllerStyle)preferredStyle
{
UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"title" message:@"alertMessage" preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *ok =[UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action){NSLog(@"ok action");}];
[alert addAction:ok];
[self presentViewController:alert animated:YES completion:nil];
}
@end
Run Code Online (Sandbox Code Playgroud) 我想调用一个函数,而不是使用带有UIAlertAction的闭包。是否可以获取对拥有UIAlertAction的UIAlertController的引用?
alert = UIAlertController(title: "City", message: "Enter a city name", preferredStyle: UIAlertControllerStyle.Alert)
UIAlertActionStyle.Default, handler: okAlert)
//...
func okAlert(action: UIAlertAction) {
// Get to alert here from action?
}
Run Code Online (Sandbox Code Playgroud) 我将使用UIAlertController为用户选择一个项目.要选择的项目如下:
let arraySelect = ["NewYork", "Washington", "Seoul", "Tokyo", "Peking", "Sidney", ... ]
let alert = UIAlertController(title: nil, message: nil, preferredStyle: .Alert)
// Add items in array to Alert
for var i = 0; i < arraySelect.count; i++ {
alert.addAction(UIAlertAction(title: arrayBibleVersions[i], style: .Default, handler: {(_) in }))
}
// Add cancel button.
alert.addAction(UIAlertAction(title: "??", style: .Cancel, handler: {(_) in }))
self.presentViewController(alert, animated: false, completion: nil)
Run Code Online (Sandbox Code Playgroud)
当用户触摸一个项目时,我必须获取用户触摸的项目的索引.但我不知道如何获得索引..请帮帮我.
我正在构建一个动态的动作列表.按动作时,我想跳到表格视图中的一个部分.为此,我想知道所选的操作索引并将其替换为代码部分:"inSection:0".这样做的方式?
这是我的代码:
let optionMenu = UIAlertController(title: nil, message: "Jump to", preferredStyle: .ActionSheet)
for item in items {
let action = UIAlertAction(title: item, style: .Default, handler: { (alert: UIAlertAction!) -> Void in
let indexPath = NSIndexPath(forRow: 0, inSection: 0)
self.tableView.scrollToRowAtIndexPath(indexPath, atScrollPosition: UITableViewScrollPosition.Bottom, animated: true)
})
optionMenu.addAction(action)
}
self.presentViewController(optionMenu, animated: true, completion: nil)
Run Code Online (Sandbox Code Playgroud) 这个问题与此类似
但是,UIAlertController我不想改变每一种颜色,而是像'AppDelegate'那样改变它的通用性.因此,如果我在一个地方更改颜色,则所有警报控制器操作按钮都应更改为新颜色.我的问题是:
我想弄清楚如何创建一个弹出窗口,只有在你启动应用程序时才出现一次,然后除非你关闭应用程序并重新启动它,否则不会再出现.但是,如果您查看下面的代码,您将意识到每次ViewController出现时都会弹出警报.例如,如果您转到设置选项卡然后返回主ViewController菜单,则会弹出警报.
override func viewDidAppear(animated: Bool) {
let alertController = UIAlertController(title: "Disclaimer", message: "WARNING: Please ride carefully!", preferredStyle: UIAlertControllerStyle.Alert)
alertController.addAction(UIAlertAction(title: "Accept", style: UIAlertActionStyle.Default, handler: nil))
self.presentViewController(alertController, animated: true, completion: nil)
}
Run Code Online (Sandbox Code Playgroud) 我是ios的新手,用swift开发我的第一个应用程序.此应用程序包含许多弹出窗口与应用程序播放.我想让这个非常有吸引力.
内置的UIAlertcontroller破坏了我的外观和感觉.那么有没有办法完全自定义UIAlertController.
我想改变字体和颜色的title,消息还背景颜色和按钮太多.
谢谢.
我一直在使用以下帖子作为如何显示UIAlertController与特定无关的from代码的指导UIViewController。现在,我要对该代码进行单元测试:
func showAlert(alert: UIAlertController, animated: Bool, completion: (()->Void)?)
{
let alertWindow = UIWindow(frame: UIScreen.mainScreen().bounds)
// Keep a strong refence to the window.
// We manually set this to nil when the alert is dismissed
self.alertWindow = alertWindow
alertWindow.rootViewController = UIViewController()
if let currentTopWindow = UIApplication.sharedApplication().windows.last {
alertWindow.windowLevel = currentTopWindow.windowLevel + 1
}
else {
// This case only happens during unit testing
Logger.trace(ICELogLevel.Error, category: .Utility, message: "The application doesn't have a window being displayed!") …Run Code Online (Sandbox Code Playgroud) 尝试提供时,我的应用崩溃了UIAlertController。
我有一个UIViewController模态呈现的,然后在该呈现的视图控制器上,当点击某个按钮时,我想呈现一个actionSheet警报。
执行此操作时,应用程序崩溃了,我无法弄清楚原因。
这是代码:
let alertController = UIAlertController(title: nil, message: nil, preferredStyle: .actionSheet)
alertController.addAction(UIAlertAction(title: "Image from Camera", style: .default, handler: { (_) in
let cameraController = CameraController()
self.present(cameraController, animated: true, completion: nil)
}))
alertController.addAction(UIAlertAction(title: "Image from Library", style: .default, handler: {(_) in
let imagePickerController = UIImagePickerController()
imagePickerController.delegate = self
imagePickerController.allowsEditing = true
self.present(imagePickerController, animated: true, completion: nil)
}))
alertController.addAction(UIAlertAction(title: "Cancel", style: .cancel, handler: nil))
alertController.view.tintColor = UIColor.rgb(red: 46, green: 94, blue: 120)
self.present(alertController, animated: …Run Code Online (Sandbox Code Playgroud) swift ×7
ios ×6
objective-c ×2
uialertview ×2
indexing ×1
ios11 ×1
ios8 ×1
swift2 ×1
uiwindow ×1
unit-testing ×1