Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/99.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios &引用;参数“调用中的1”缺少参数;UIAlertAction处理程序(swift2.1)中出错_Ios_Xcode_Swift2 - Fatal编程技术网

Ios &引用;参数“调用中的1”缺少参数;UIAlertAction处理程序(swift2.1)中出错

Ios &引用;参数“调用中的1”缺少参数;UIAlertAction处理程序(swift2.1)中出错,ios,xcode,swift2,Ios,Xcode,Swift2,我试图在swift2.1中向UIAlertAction添加简单操作,但在部署项目时出现“调用中参数#1缺少参数”错误,我尝试了不同的方法并进行了大量搜索,但找不到正确的答案。 这是我的代码: class ViewController: UIViewController { let alertController = UIAlertController(title: "menu", message: "", preferredStyle: .Alert) internal fu

我试图在swift2.1中向UIAlertAction添加简单操作,但在部署项目时出现“调用中参数#1缺少参数”错误,我尝试了不同的方法并进行了大量搜索,但找不到正确的答案。 这是我的代码:

class ViewController: UIViewController 
{
    let alertController = UIAlertController(title: "menu", message: "", preferredStyle: .Alert)

    internal func signOut()
    {
        self.preferences.setValue( nil , forKey: "nationalid")   
    }

    let buttonOne = UIAlertAction(title: "sign out", style: .Default) { action in
        print("Button One Pressed")
        signOut() // get error
    }

    override func viewDidLoad() {
        super.viewDidLoad()
        alertController.addAction(buttonOne)
    }
}

但在指定行中出现“调用中参数#1缺少参数”错误

我发现了我的愚蠢错误,我将在viewDidLoad()函数中定义我的UIAlertAction,我更改了那里的代码并解决了问题:

class ViewController: UIViewController 
{
    let alertController = UIAlertController(title: "menu", message: "", preferredStyle: .Alert)

    internal func signOut()
    {
        self.preferences.setValue( nil , forKey: "nationalid")   
    }


    override func viewDidLoad() {
        super.viewDidLoad()


    let buttonOne = UIAlertAction(title: "sign out", style: .Default) { action in
        print("Button One Pressed")
        signOut() 
    }

        alertController.addAction(buttonOne)
    }
}

您已经在viewDidLoad或函数中编写了此函数

例如:

  override func viewDidLoad() {
    super.viewDidLoad()

       let alertController = UIAlertController()


       let OKAction =  UIAlertAction(title: "Sign out", style: .Default) { (action:UIAlertAction!) in
             signOut() 
       }
       alertController.addAction(OKAction)

       self.presentViewController(alertController, animated: true, completion:nil)
   }

希望这会有所帮助

您还需要添加presentViewController来显示它。