2017-02-23 31 views
0

请帮忙!我是一个巨大的初学者。我试图得到一个picker viewa textfield into one alert controller.显然我不能添加一个选择器视图到警报控制器,因为IOS 8,而是我需要使用一个action sheet with multiple actions。为什么我不能使用default alert style?因为我需要2个以上的动作,而默认的提示样式显然最多只允许2个动作。所以,我需要使用一个操作表。唯一的问题是,我似乎无法找到一种方法,将editable textfield放入an action sheet,以具有多个操作选项 - 而不是只有两个操作选项的an editable textfield in my default alert style,有没有办法从当前警报控制器(或操作表中的文本框)中调用警报控制器? Xcode 8,Swift 3,IOS

这是我到目前为止,我editable textfield in my default alert style, with only two options (OK and cancel):

@IBOutlet weak var TEXTTESTLabel: UILabel! 

@IBAction func TEXTESTTapped(_ sender: UIButton) { 
    print("TEXTTEST Button Tapped") 
    openTEXTTESTAlert() 
} 


func openTEXTTESTAlert() { 
    //Create Alert Controller 
    let alert9 = UIAlertController (title: "Test Your Text:", message: nil, preferredStyle: UIAlertControllerStyle.alert) 

    //Create Cancel Action 
    let cancel9 = UIAlertAction(title: "Cancel", style: UIAlertActionStyle.cancel, handler: nil) 

    alert9.addAction(cancel9) 

    //Create OK Action 
    let ok9 = UIAlertAction(title: "OK", style: UIAlertActionStyle.default) { (action: UIAlertAction) in print("OK") 
     let textfield = alert9.textFields?[0] 
     print(textfield?.text!) 
     self.TEXTTESTLabel.text = textfield?.text! 
    } 

    alert9.addAction(ok9) 

    //Add Text Field 
    alert9.addTextField { (textfield: UITextField) in 
     textfield.placeholder = "TEXTTEST" 
    } 

    //Present Alert Controller 
    self.present(alert9, animated:true, completion: nil) 
} 

//////////////////////////// ////////////////////////////////////////////////// ////

既然如此,我已经帮助创建了两个单独的提醒控制器,但看起来感觉凌乱。它的设置,所以如果我点击按钮2 the default alert style with a textfield will pop并插入文本到标签。但是,如果我点击按钮1,然后点击按钮2,它会显示an action sheet with 4 actions,它将一个单词放入同一个标签。这是我对这种方法:

@IBOutlet weak var TextLabel: UILabel! 


var userWantsToShowAlert = false 

@IBAction func yourNewButtonTapped(_ sender: UIButton) { 
    userWantsToShowAlert = !userWantsToShowAlert 
    print("User wants to show alert? \(userWantsToShowAlert)") 
    //This is userWantsToShowAlert is false, it will change it to true. And if it is true, it will change it to false. 
} 

@IBAction func TextButtonTapped(_ sender: UIButton) { 
    print("Text Button Tapped") 
    if(userWantsToShowAlert){ 
     openTextAlert() 
    }else{ 
     openActionSheetAlert() 
    } 


} 

func openTextAlert() { 
    //Create Alert Controller 
    let alert9 = UIAlertController (title: "Whatever Text Your Heart Desires:", message: nil, preferredStyle: UIAlertControllerStyle.alert) 

    //Create Cancel Action 
    let cancel9 = UIAlertAction(title: "Cancel", style: UIAlertActionStyle.cancel, handler: nil) 

    alert9.addAction(cancel9) 

    //Create OK Action 
    let ok9 = UIAlertAction(title: "OK", style: UIAlertActionStyle.default) { (action: UIAlertAction) in print("OK") 
     let textfield = alert9.textFields?[0] 
     print(textfield?.text!) 
     self.TextLabel.text = textfield?.text! 
    } 

    alert9.addAction(ok9) 

    //Add Text Field 
    alert9.addTextField { (textfield: UITextField) in 
     textfield.placeholder = "Whatever text you want to enter" 
    } 

    //Present Alert Controller 
    self.present(alert9, animated:true, completion: nil) 
} 

func openActionSheetAlert(){ 
    let alert9 = UIAlertController (title: "Whatever Text Your Heart Desires:", message: nil, preferredStyle: UIAlertControllerStyle.actionSheet) 

    //Create Cancel Action 
    let cancel9 = UIAlertAction(title: "Cancel", style: UIAlertActionStyle.cancel, handler: nil) 
    alert9.addAction(cancel9) 


    let bt1 = UIAlertAction(title: "1", style: UIAlertActionStyle.default){ 
     (action) in self.TextLabel.text = "Word 1"} 

    alert9.addAction(bt1) 

    let bt2 = UIAlertAction(title: "2", style: UIAlertActionStyle.default){ 
     (action) in self.TextLabel.text = "Word 2"} 

    alert9.addAction(bt2) 


    let bt3 = UIAlertAction(title: "3", style: UIAlertActionStyle.default){ 
     (action) in self.TextLabel.text = "Word 3"} 

    alert9.addAction(bt3) 

    let bt4 = UIAlertAction(title: "4", style: UIAlertActionStyle.default){ 
     (action) in self.TextLabel.text = "Word 4"} 
    alert9.addAction(bt4) 

    alert9.popoverPresentationController?.sourceView = self.view 
    alert9.popoverPresentationController?.sourceRect = CGRect(x: self.view.bounds.size.width/2.0, y: self.view.bounds.size.height/4.0, width: 1.0, height: 1.0) 
    self.present(alert9, animated:true, completion: nil) 
} 

我发现这种方法让应用程序用户感到困惑。如果任何人有一个不同的方法来获取一个文本框到一个操作表(甚至是一个嵌入在当前警报控制器中的单独的警报控制器),我会非常感激! 谢谢:)

回答

0

对于动作片,你可以使用如

func showActionSheet() 
    { 
    let actionSheet = UIAlertController(title: nil, message: nil, preferredStyle: UIAlertControllerStyle.actionSheet) 

    actionSheet.addAction(UIAlertAction(title: "1:", style: UIAlertActionStyle.default, handler: { (alert:UIAlertAction!) -> Void in 
     ) 
     self.action1() 
    })) 

    actionSheet.addAction(UIAlertAction(title: "2", style: UIAlertActionStyle.default, handler: { (alert:UIAlertAction!) -> Void in 
     self.action2() 
    })) 

    actionSheet.addAction(UIAlertAction(title: "3", style: UIAlertActionStyle.default, handler: { (alert:UIAlertAction!) -> Void in 
     // self.action3() 
    })) 


    actionSheet.addAction(UIAlertAction(title: "Cancel", style: UIAlertActionStyle.cancel, handler: nil)) 

    self.present(actionSheet, animated: true, completion: nil) 

    } 

    // showActionSheet() 
+0

无法让您的代码正常工作 – DanielG

相关问题