ios - 在展开 UIButton 设置标题的可选值时意外发现 nil

标签 ios swift uibutton viewcontroller

import UIKit

class PopOverViewController: UIViewController, UIGestureRecognizerDelegate, UIPopoverPresentationControllerDelegate {

@IBOutlet weak var textForPopUp: UILabel!
@IBOutlet weak var imge1: UIImageView!
@IBOutlet weak var imge2: UIImageView!
@IBOutlet weak var imge3: UIImageView!
@IBOutlet weak var imge4: UIImageView!
@IBOutlet weak var imge5: UIImageView!
@IBOutlet weak var yesButton: UIButton!
@IBOutlet weak var noButton: UIButton!
override func viewDidLoad() {
    super.viewDidLoad()
    let tapGestureRecognizer = UITapGestureRecognizer(target:self, action:Selector("imageTapped:"))
    tapGestureRecognizer.delegate = self
    print("Inside POP over View Controller viewDidLoad")    
    imge1.image = UIImage(named: "like")
    imge1.tag = 0
    imge1.addGestureRecognizer(tapGestureRecognizer)
    imge1.userInteractionEnabled = true
    imge2.image = UIImage(named: "like")
    imge2.tag = 1
    imge2.addGestureRecognizer(tapGestureRecognizer)
    imge2.userInteractionEnabled = true
    imge3.image = UIImage(named: "like")
    imge3.tag = 2
    imge3.addGestureRecognizer(tapGestureRecognizer)
    imge3.userInteractionEnabled = true
    imge4.image = UIImage(named: "like")
    imge4.tag = 3
    imge4.addGestureRecognizer(tapGestureRecognizer)
    imge4.userInteractionEnabled = true
    imge5.image = UIImage(named: "like")
    imge5.tag = 4
    imge5.userInteractionEnabled = true
    imge5.addGestureRecognizer(tapGestureRecognizer)

    // Do any additional setup after loading the view.
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}
func imageTapped(sender : UIImageView){
    if(sender.tag < 3){
        print("Why didn't you like our app?")
    }
    else {
        print("We will be redirecting you shortly")
    }

}

func initializeData(buttonTitle1 button1: String? = nil , buttonTitle button2 : String? = nil , titleText title : String? = nil){

    if(button1 != nil) {

        yesButton.setTitle(button1, forState: .Normal)
    }
    if(button2 != nil){
        noButton.setTitle(button2, forState: .Normal)
    }
        if(title != nil){
        textForPopUp.text = title
    }


}
}

错误在行 yesButton.setTitle(button1, forState: .Normal) 意外发现nil是报错。 我叫

initializeData()

来自另一个 Controller 。 我在 XCode 控制台中看到我所有的 ImageView 和 Button 对象都是零。我认为这是导致错误的原因。我该如何解决?

编辑: 这里调用了 initializeData()

func popView(sender : MyOrdersViewController){
    let storyBoard : UIStoryboard = UIStoryboard(name : "Main" , bundle: nil)
    var popViewController : PopOverViewController = storyBoard.instantiateViewControllerWithIdentifier("popUp") as! PopOverViewController
    popViewController.initializeData(buttonTitle1: "Yes", buttonTitle: "No", titleText: "How do you you find our app?")
    let popOverMenuViewController = popViewController.popoverPresentationController
    popOverMenuViewController?.permittedArrowDirections = .Any
    popOverMenuViewController?.delegate = self
    popOverMenuViewController?.sourceView = sender.view
    presentViewController(popViewController, animated: true, completion: nil)

}

最佳答案

这里的问题是,您已经创建了一个 PopOverViewController 实例,并且在加载 View 之前(当您所有的导出仍然是 nil 时)您正在调用 initializeData(_:) 正在访问您的网点。

你要么必须做一些让 View 首先加载的事情,要么你可以先传递按钮标题并将其加载到 viewWillAppear/viewDidLoad PopOverViewController.

一定要检查你的 socket 的连接。人们经常忘记连接或更改连接后调整连接。

一个传递标题的小例子:

func popView(sender: MyOrdersViewController){
    let storyBoard = UIStoryboard(name : "Main" , bundle: nil)
    var controller = storyBoard.instantiateViewControllerWithIdentifier("popUp") as! PopOverViewController
    controller.titles = ["title1": "yes", "title2": "no"]
    presentViewController(popViewController, animated: true, completion: nil)
}

以及如何设置标题:

class PopOverViewController: UIViewController {

    var titles: [String: String] = [:]

    override viewWillAppear() {
        super.viewWillAppear()

        yesButton.setTitle(titles["title1"] ?? "No title", forState: .Normal)
         noButton.setTitle(titles["title2"] ?? "No title", forState: .Normal)
    }
}

使用 标题["title1"] ??如果 titles["title1"]nil,则“No title” 会将按钮的标题设置为 “No title” .这是一种安全的方法,因为它不会隐式地展开一个可选的。它相当于:

titles["title1"] != nil ? titles["title1"]! : "No title" }

关于ios - 在展开 UIButton 设置标题的可选值时意外发现 nil,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35520651/

相关文章:

ios - 如何在多个线程上处理 NSManagedObjectContext 和 NSManagedObject 创建和编辑?

ios - 使用 AFNetworking 进行 x-www-form-urlencoded POST 时出现问题

ios - 午夜后刷新iOS UIDatePicker的内部状态

iphone - 如何创建椭圆形UIButton

ios - 为什么 UIButton 不响应触摸?

iphone - 使用自定义键盘调用 textFieldShouldReturn

ios - RestKit 一对多关系逆映射

swift - 有没有办法获取变量中advertisementData的值

ios - 当我 popToRootViewControllerAnimated 时导航栏消失

ios - 我们如何在自定义 UIView 类中调用操作?需要在所有 View Controller 中重用 UIView 类