我从我的添加栏按钮项目中调用下面的代码,其中我提出要求用户输入的警报视图。它第一次正常工作,并在之后给出以下错误:
代码:
var alert = UIAlertController(title: "Enter Blog Link", message: nil, preferredStyle: .Alert)
func userBlogLinkEntryPopover() {
// let alert = UIAlertView(title: "Enter Blog Link", message: nil, delegate: self, cancelButtonTitle: "Cancel")
alert.addTextFieldWithConfigurationHandler { (textField) -> Void in
textField.placeholder = "Enter Blog URL!"
}
alert.addAction(UIAlertAction(title: "Ok", style: .Default, handler: { (action) -> Void in
if let tf = self.alert.textFields?.first as? UITextField{
println(tf.text)
}
}))
alert.addAction(UIAlertAction(title: "Cancel", style: .Cancel, handler: nil))
presentViewController(alert, animated: true, completion: nil)
}
错误:' UIAlertController只能有一个样式的动作 UIAlertActionStyleCancel'
我认为每次按下添加按钮时都会尝试添加操作,因此错误。如果我错了请纠正我,还请建议解决。
感谢您的帮助。
答案 0 :(得分:3)
如果你这样做,你也会得到这个,仔细阅读看看你是否能看到错误
resendAlert.addAction(UIAlertAction(title: "Cancel", style: .cancel, handler: { (action: UIAlertAction!) in
//cancel clicked
return
}))
resendAlert.addAction(UIAlertAction(title: "Check Server", style: .cancel, handler: { (action: UIAlertAction!) in
//check server clicked
return
}))
请注意---> .cancel
不使用TWICE需要.default
才能使用 resendAlert.addAction(UIAlertAction(title: "Check Server", style: .default, handler: { (action: UIAlertAction!) in
//check server clicked
return
}))
import java.awt.*;
import java.awt.event.*;
import javax.swing.Timer;
public class Game2v4 extends Frame implements ActionListener, KeyListener
{
int x = 100;
int y = 100;
Timer myTimer = new Timer(10,this);
Panel southPanel = new Panel();
Button startButton = new Button("Start");
Button clearButton = new Button("Clear");
public Game2v4()
{
addKeyListener(this);
setFocusable(true);
this.setLayout(new BorderLayout());
southPanel.setLayout(new GridLayout(1,2));
add(southPanel, BorderLayout.SOUTH);
southPanel.add(startButton);
startButton.addActionListener(this);
southPanel.add(clearButton);
clearButton.addActionListener(this);
addWindowListener
(
new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
}
);
}
public void keyPressed(KeyEvent e)
{
if(e.getKeyCode() == KeyEvent.VK_UP)
{
y = y - 5;
repaint();
}
if (e.getKeyCode() == KeyEvent.VK_DOWN)
{
y = y + 5;
repaint();
}
if (e.getKeyCode() == KeyEvent.VK_RIGHT)
{
x = x + 5;
repaint();
}
if (e.getKeyCode() == KeyEvent.VK_LEFT)
{
x = x - 5;
repaint();
}
while(x >= 400)
{
x = 1;
repaint();
}
while(x <= -10)
{
x = 400;
repaint();
}
while(y >= 400)
{
y = 1;
repaint();
}
while(y <= 0)
{
y = 400;
repaint();
}
}
public void keyReleased(KeyEvent e)
{
}
public void keyTyped(KeyEvent e)
{
}
public void actionPerformed(ActionEvent e)
{
Graphics g = getGraphics();
if(e.getSource() == startButton)
{
for(int i=250; i>0; i-=5)
{
g.drawRect(i,300,25,25);
}
}
this.requestFocus();
}
public void paint(Graphics g)
{
g.drawRect(x,y,25,25);
}
public static void main(String[] args)
{
Game2v4 f = new Game2v4();
f.setBounds(50,100,400,400);
f.setTitle("Gamev4");
f.setVisible(true);
}
}
工作正常
答案 1 :(得分:2)
我发现因为警报是在函数之外声明的,所以它保留了所有操作,并因此引发了异常。我更正了我的代码如下,它工作正常。
func userBlogLinkEntryPopover() {
var alert = UIAlertController(title: "Enter Blog Link", message: nil, preferredStyle: .Alert)
alert.addTextFieldWithConfigurationHandler { (textField) -> Void in
textField.placeholder = "Enter Blog URL!"
}
alert.addAction(UIAlertAction(title: "Ok", style: .Default, handler: { (action) -> Void in
if let tf = alert.textFields?.first as? UITextField{
println(tf.text)
}
}))
alert.addAction(UIAlertAction(title: "Cancel", style: .Cancel, handler: nil))
presentViewController(alert, animated: true, completion: nil)
}
此致
答案 2 :(得分:0)
这就是让我绊倒的原因-自动填充错误。使用ObjC,而不是在操作上使用样式UIAlertActionStyleDefault,而是使用UIAlertControllerStyleAlert常量。没有警告,但是这两个都解析为枚举值1,因此我无意中向同一个控制器添加了两个Cancel操作。