在iOS 8之前,我们不得不使用UIAlertView和UIActionSheet
我们不允许在它们上面混淆视图层次结构或子类.
UIAlertView类旨在按原样使用,不支持子类化.此类的视图层次结构是私有的,不得修改.
UIActionSheet不是设计为子类,也不应该为其层次结构添加视图.如果您需要提供比UIActionSheet API提供的更多自定义的工作表,您可以创建自己的工作表并使用presentViewController以模态方式呈现它:animated:completion:.
然而,iOS8 Apple已经推出UIAlertController了更换两者UIAlertView和UIActionSheet(请查看此处的预发布文档).
因此,在这个预发布文档中,没有任何关于无法子类化或更改视图层次结构,它甚至有这种方法,addTextFieldWithConfigurationHandler:因此我们能够更改视图层次和/或子类,UIAlertController而不必担心Apple是否会批准或拒绝我们的应用程序?
我试图在我的快速应用程序中显示UIAlertView
alert = UIAlertView(title: "",
message: "bla",
delegate: self,
cancelButtonTitle: "OK")
alert!.show()
Run Code Online (Sandbox Code Playgroud)
=> BAD_ACESS错误: - [_ UIAlertViewAlertControllerShim initWithTitle:message:delegate:cancelButtonTitle:otherButtonTitles:]()
所以我怀疑自己.我把它设置为零
alert = UIAlertView(title: "",
message: "bla",
delegate: nil,
cancelButtonTitle: "OK")
alert!.show()
Run Code Online (Sandbox Code Playgroud)
=> ARM_DA_ALIGN错误: - [_ UIAlertViewAlertControllerShim initWithTitle:message:delegate:cancelButtonTitle:otherButtonTitles:]()
完整的代码
import UIKit
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate, UIAlertViewDelegate {
@lazy var window = UIWindow(frame: UIScreen.mainScreen().bounds)
@lazy var locationManager = CLLocationManager()
var alert: UIAlertView? = nil
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: NSDictionary?) -> Bool {
//setup dummy window
self.window.backgroundColor = UIColor.whiteColor()
self.window.rootViewController = UIViewController() …Run Code Online (Sandbox Code Playgroud) 当我使用UIActionSheet或UIAlertController执行以下操作时,我在模拟器中看到iOS 8中的内存泄漏.UIActionSheet在IOS 8中使用UIAlertController,因此问题是相关的.
按下按钮时会调用showCameraAction.我已从委托方法中删除了所有内容,但在下面显示的情况下仍然会出现泄漏.我是否以某种方式使用UIActionSheet,我不应该这样做?我将非常感谢您解决此问题的任何帮助.相同的代码与IOS 7没有泄漏(在模拟器中).
-(IBAction)showCameraAction:(id)sender
{
UIActionSheet* actionSheet = [[UIActionSheet alloc] initWithTitle:@"Photo From:"
delegate:self
cancelButtonTitle:@"Cancel"
destructiveButtonTitle:nil
otherButtonTitles:@"Phone", @"Flickr", nil];
[actionSheet showInView:[[UIApplication sharedApplication] keyWindow]];
//also tried just showInView: self.view
}
Run Code Online (Sandbox Code Playgroud)
//空
- (void)actionSheet:(UIActionSheet *)actionSheet
clickedButtonAtIndex:(NSInteger)buttonIndex {
}
Run Code Online (Sandbox Code Playgroud)
也尝试使用UIAlertController,结果相同:
UIAlertController *alertController = [UIAlertController
alertControllerWithTitle:@"Photo From:"
message:@""
preferredStyle:UIAlertControllerStyleActionSheet];
UIAlertAction *phoneAction = [UIAlertAction
actionWithTitle:NSLocalizedString(@"Phone", @"Phone action")
style:UIAlertActionStyleCancel
handler:^(UIAlertAction *action)
{
NSLog(@"Phone action");
}];
UIAlertAction *flickrAction = [UIAlertAction
actionWithTitle:NSLocalizedString(@"Flickr", @"Flickr action")
style:UIAlertActionStyleDefault
handler:^(UIAlertAction *action)
{
NSLog(@"Flickr action");
}];
[alertController addAction:phoneAction];
[alertController addAction:flickrAction];
[self presentViewController:alertController …Run Code Online (Sandbox Code Playgroud) 如何更改UIAlertAction已添加到按钮的按钮的颜色UIAlertController?
当iOS呈现警报时,半透明度是不变的.当我的应用程序出现一个时,警报首先是白色,只有半秒钟,然后它变得半透明.这是一个小问题,但它看起来很混乱,警报应该在我认为的任何地方保持一致.无论如何这里是代码:
UIAlertController *alertVC = [UIAlertController alertControllerWithTitle:@"Invalid Credentials" message:@"Please try again." preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *ok = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
[_username becomeFirstResponder];
}];
[alertVC addAction:ok];
[self presentViewController:alertVC animated:YES completion:NULL];
Run Code Online (Sandbox Code Playgroud)
我也对这里的人们的意见感兴趣.对于任何人来说,这只是一个小问题吗?看起来这只是UIAlertController工作方式(至少在开发人员使用它时).无论如何,它看起来很乱,我认为它是一个错误(除非我做错了),因为它看起来与iOS提出警报时看起来不一样,即使它应该.
我正在尝试添加UIAlertController到我的应用程序,但它根本没有出现.我尝试过以下方法:
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"Message" message:@"Web Service is not available." preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction* ok = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:nil];
[alertController addAction:ok];
[self presentViewController:alertController animated:YES completion:nil];
Run Code Online (Sandbox Code Playgroud)
但这根本没有出现,我做错了什么?
我创造了一个UIAlertController首选的风格UIAlertControllerStyleAlert.leftBarButtonItem点击时会显示警报.我创建了一个UIBarButtonItem名为的属性backButton并设置了leftBarButtonItem = self.backButton.这是按设计工作的.我没有使用故事板.
问题是leftBarButtonItem当警报显示时,向下移动(我的猜测:大约20分钟).为什么会这样?
我知道如何显示/隐藏按钮,以便用户在向下移动时看不到该按钮.然而,这很糟糕.为什么它首先发生?
我没有在网上找到任何类似的问题.
@property (strong, nonatomic) IBOutlet UIBarButtonItem *backButton;
Run Code Online (Sandbox Code Playgroud)
在viewDidLoad中:
self.backButton = [[UIBarButtonItem alloc] initWithTitle:@"" style:UIBarButtonItemStylePlain target:self action:@selector(backButtonPressed)];
[self.backButton setImage:[UIImage imageNamed:@"back-arrow-grey"]];
self.navigationItem.leftBarButtonItem = self.backButton;
Run Code Online (Sandbox Code Playgroud)
在backButtonPressed中:
{
self.navigationItem.leftBarButtonItem = nil; //to hide backButton because it moves down
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"My title" preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *actionLeave = [UIAlertAction actionWithTitle:@"Leave" style:UIAlertActionStyleDefault handler:...//which works correctly
UIAlertAction *actionCancel = [UIAlertAction actionWithTitle:@"Go back" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) …Run Code Online (Sandbox Code Playgroud) objective-c uinavigationcontroller uinavigationitem uialertcontroller
我正在使用UIAlertController来获取用户输入和更新表格单元格.每当我尝试创建警报时,我都会在控制台中收到以下警告
2015-11-19 17:51:42.034 SimpleTableView [5488:584215]未定义UICollectionViewFlowLayout的行为,因为:
2015-11-19 17:51:42.035 SimpleTableView [5488:584215]项目高度必须小于UICollectionView的高度减去截面插入顶部和底部值,减去内容插入的顶部和底部值.
2015-11-19 17:51:42.036 SimpleTableView [5488:584215]相关的UICollectionViewFlowLayout实例是<_UIAlertControllerCollectionViewFlowLayout:0x7fd0a057c3d0>,它附加到; layer =; contentOffset:{0,0}; contentSize:{0,0}>集合视图布局:<_ UIAlertControllerCollectionViewFlowLayout:0x7fd0a057c3d0>.2015-11-19 17:51:42.036 SimpleTableView [5488:584215]在UICollectionViewFlowLayoutBreakForInvalidSizes上创建一个符号断点,以便在调试器中捕获它.
如众多博客所述,实施非常简单,
func displayAlertInfo(){
let alertController = UIAlertController(title: "New Race", message: "Type in a new race", preferredStyle: .Alert)
let cancelAction = UIAlertAction(title: "Cancel", style: .Cancel) { (_) in
alertController.dismissViewControllerAnimated(true, completion: nil)
}
let addAction = UIAlertAction(title: "Add", style: .Default) { (_) in
let textFieldInput = alertController.textFields![0] as UITextField
let newRace = textFieldInput.text?.capitalizedString
DataManager.sharedInstance.addRace(species: self.species, race: newRace!)
let newIndexPath = NSIndexPath(forRow: …Run Code Online (Sandbox Code Playgroud) 我用input accessory view我的viewController在屏幕的底部停靠视图,并在键盘坚持.
一切正常,除了显示一个alert,input accessory view隐藏.它再次被揭露解雇了alert.这导致不期望的故障.
我尝试使用[alertView show]API和新alertControllerAPI.两种情况都存在问题.我需要得到支持iOS7.0.
请提供您宝贵的意见或解决此问题的方法.
我有一个主类,AddFriendsController它运行以下代码行:
ErrorReporting.showMessage("Error", msg: "Could not add student to storage.")
Run Code Online (Sandbox Code Playgroud)
然后我有这个ErrorReporting.swift文件:
进口基金会
class ErrorReporting {
func showMessage(title: String, msg: String) {
let alert = UIAlertController(title: title, message: msg, preferredStyle: UIAlertControllerStyle.Alert)
alert.addAction(UIAlertAction(title: "Ok", style: UIAlertActionStyle.Default, handler: nil))
self.presentViewController(alert, animated: true, completion: nil)
}
}
Run Code Online (Sandbox Code Playgroud)
显然,self不会在这里工作,并给我一个错误.我如何引用当前打开的视图控制器(即AddFriendsController在这种情况下),因为我希望在许多不同的swift文件中使用相同的方法?
谢谢.
ios ×6
objective-c ×3
swift ×3
uialertview ×3
cocoa-touch ×1
ios8 ×1
memory-leaks ×1
swift2 ×1
swift2.3 ×1