按着书上做 可是还是报错了 反复对照了几次。。。没办法

[复制链接]
286 3
import UIKit

class ViewController: UIViewController {


@IBOutlet weak var nameField: UITextField!
@IBOutlet weak var numberField: UITextField!
@IBOutlet weak var sliderLabel: UILabel!
@IBOutlet weak var leftSwitch: UISwitch!
@IBOutlet weak var rightSwitch: UISwitch!
@IBOutlet weak var doSomethingButton: UIButton!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
sliderLabel.text = "50"
}

override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}

@IBAction func textFieldDoneEditing(sender:UITextField){
sender.resignFirstResponder()
}
@IBAction func backgroundTap(sender : UIControl){
nameField.resignFirstResponder()
numberField.resignFirstResponder()
}
@IBAction func sliderChanged(sender: AnyObject) {
let progress = lroundf(sender.value)
sliderLabel.text = "(progress)"
}
@IBAction func switchChanged(sender: UISwitch) {
let setting = sender.on
leftSwitch.setOn(setting, animated:true )
rightSwitch.setOn(setting,animated:true )
}

@IBAction func toggleControls(sender: UISegmentedControl) {
if sender.selectedSegmentIndex == 0{
leftSwitch.hidden = false
rightSwitch.hidden = false
doSomethingButton.hidden = true

}else{
leftSwitch.hidden = true
rightSwitch.hidden = true
doSomethingButton.hidden = false
}
}
@IBAction func buttonPressed(sender: UIButton!){
let controler = UIAlertController(title:"Are you sure?",
message:nil,preferredStyle: .ActionSheet)
let yesAction = UIAlertAction(title:"Yes, I'm sure!",
style:.Destructive,handler:{ action in
​ let msg = self.nameField.text.isEmpty //这里 text 提示报错
(报错提示:value of optional type 'String?' not unwrapped;did you mean to use'!' or '?'?
?"You can breathe easy,everyting went ok."
:"You can breathe easy,(self.nameField.text),"
+ " everything went ok."
let controller2 = UIAlertController(
title:"Something was Done",
message: msg,preferredStyle:.Alert)
let cancelAction = UIAlertAction(title:"Phew!",
style: .Cancel,handler:nil)
controller2.addAction(cancelAction)
self.presentViewController(controller2,animated:true,completion:nil)
})
let noAction = UIAlertAction(title:"no way!",style: .Cancel,handler:nil)
controler.addAction(yesAction)
controler.addAction(noAction)
if let ppc = controler.popoverPresentationController{
ppc.sourceView = sender
ppc.sourceRect = sender.bounds
}
presentViewController(controler, animated: true, completion: nil)
}
}

举报 使用道具

回复

3 个评论

wjk208  新手上路  发表于 2019-2-8 23:10:14  | 显示全部楼层
import UIKit

class ViewController: UIViewController {
   

    @IBOutlet weak var nameField: UITextField!
    @IBOutlet weak var numberField: UITextField!
    @IBOutlet weak var sliderLabel: UILabel!
    @IBOutlet weak var leftSwitch: UISwitch!
    @IBOutlet weak var rightSwitch: UISwitch!
    @IBOutlet weak var doSomethingButton: UIButton!
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
        sliderLabel.text = "50"
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

@IBAction func textFieldDoneEditing(sender:UITextField){
    sender.resignFirstResponder()
}
    @IBAction func backgroundTap(sender : UIControl){
        nameField.resignFirstResponder()
        numberField.resignFirstResponder()
    }
    @IBAction func sliderChanged(sender: AnyObject) {
        let progress = lroundf(sender.value)
        sliderLabel.text = "(progress)"
    }
       @IBAction func switchChanged(sender: UISwitch) {
        let setting = sender.on
        leftSwitch.setOn(setting, animated:true )
        rightSwitch.setOn(setting,animated:true )
    }

    @IBAction func toggleControls(sender: UISegmentedControl) {
        if sender.selectedSegmentIndex == 0{
        leftSwitch.hidden = false
        rightSwitch.hidden = false
        doSomethingButton.hidden = true
        
    }else{
        leftSwitch.hidden = true
        rightSwitch.hidden = true
        doSomethingButton.hidden = false
        }
}
    @IBAction func buttonPressed(sender: UIButton!){
        let controler = UIAlertController(title:"Are you sure?",
            message:nil,preferredStyle: .ActionSheet)
        let yesAction = UIAlertAction(title:"Yes, I'm sure!",
            style:.Destructive,handler:{ action in
​             let msg = self.nameField.text.isEmpty     //这里 text 提示报错   
(报错提示:value of optional type 'String?' not unwrapped;did you mean to use'!' or '?'?
              ?"You can breathe easy,everyting went ok."
                :"You can breathe easy,(self.nameField.text),"
                + " everything went ok."
        let controller2 = UIAlertController(
                title:"Something was Done",
                message: msg,preferredStyle:.Alert)
                let cancelAction = UIAlertAction(title:"Phew!",
                    style: .Cancel,handler:nil)
        controller2.addAction(cancelAction)
        self.presentViewController(controller2,animated:true,completion:nil)
        })
        let noAction = UIAlertAction(title:"no way!",style: .Cancel,handler:nil)
        controler.addAction(yesAction)
        controler.addAction(noAction)
        if let ppc = controler.popoverPresentationController{
            ppc.sourceView = sender
            ppc.sourceRect = sender.bounds
        }
        presentViewController(controler, animated: true, completion: nil)
    }
}

举报 使用道具

回复
PesarovswEne  新手上路  发表于 2019-2-8 23:59:10  | 显示全部楼层
let msg:String = self.nameField.text.isEmpty     //这里 text 提示报错

举报 使用道具

回复
Vladikoirota  新手上路  发表于 2019-2-9 00:57:52  | 显示全部楼层
let msg:String = self.nameField.text?
self.nameField.text.isEmpty是个Bool类型,前面接收不能用String,直接self.nameField.text?或self.nameField.text!

举报 使用道具

回复
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

ios.irunthink.com
搜索
热搜: iOS swift 下拉刷新

图文热点

更多

开发教程

更多

客服中心

400-800 周一至周日 9:30-17:30 仅收市话费

关注我们

  • 微信扫描
  • 关注微信
关于我们
关于我们
友情链接
联系我们
帮助中心
精贴推荐
源码上传
注册登录
服务支持
资源下载
关注我们
官方微博
官方空间
快速回复 返回顶部 返回列表