为什么第二个" if"不管用?

时间:2015-04-24 16:59:08

标签: ios swift

我正在尝试填充视图,我正在编写此视图的代码。代码:

class DetailViewController: UIViewController, UIScrollViewDelegate {

@IBOutlet weak var tituloView: UILabel!
@IBOutlet weak var scrollView: UIScrollView!

var txtTitulo: String?
var txt1: String?
var imageView: UIImageView?
var img1: String?
var txt2: String?
var texto2: String?
var imageView2: UIImageView?
var img2: String?
var txt3: String?
var img3: String?
var txt4: String?
var img4: String?

var arrayHeights: [Int] = []

override func viewDidLoad() {
    super.viewDidLoad()

    tituloView.text = txtTitulo
    tituloView.textAlignment = .Center
    tituloView.font = UIFont(name: "HelveticaNeue-UltraLight", size: 40)

    scrollView.zoomScale = 1

    var paragraphStyle1 = NSMutableParagraphStyle()
    paragraphStyle1.alignment = NSTextAlignment.Justified

    var attributedString1 = NSAttributedString(string: txt1!,
        attributes: [
            NSParagraphStyleAttributeName: paragraphStyle1,
            NSBaselineOffsetAttributeName: NSNumber(float: 0)
        ])

    let texto1 = UILabel(frame: CGRectMake(154, -55, 697, 200))
    texto1.attributedText = attributedString1
    texto1.numberOfLines = 0
    texto1.font = UIFont(name: "HelveticaNeue-UltraLight", size: 28)
    texto1.lineBreakMode = .ByWordWrapping
    texto1.sizeToFit()

    scrollView.addSubview(texto1)

    let imagem = UIImage(named: img1!)
    let imageView = UIImageView(image: imagem)
    imageView.frame = CGRect(x: 320, y: texto1.frame.size.height, width: 400, height: 250)
    imageView.contentMode = .ScaleAspectFit

    scrollView.addSubview(imageView)

    if (txt2 != nil) {

        var paragraphStyle2 = NSMutableParagraphStyle()
        paragraphStyle2.alignment = NSTextAlignment.Justified

        var attributedString2 = NSAttributedString(string: txt2!,
            attributes: [
                NSParagraphStyleAttributeName: paragraphStyle2,
                NSBaselineOffsetAttributeName: NSNumber(float: 0)
            ])

        let texto2 = UILabel(frame: CGRectMake(164, imageView.frame.origin.y + imageView.frame.size.height + 20, 697, 200))
        texto2.attributedText = attributedString2
        texto2.numberOfLines = 0
        texto2.font = UIFont(name: "HelveticaNeue-UltraLight", size: 28)
        texto2.lineBreakMode = .ByWordWrapping
        texto2.sizeToFit()

        scrollView.contentSize = CGSizeMake(texto1.frame.size.width, texto1.frame.size.height + imageView.frame.size.height + texto2.frame.size.height + 20)


        scrollView.addSubview(texto2)

        if (img2 != nil) {

            let imagem2 = UIImage(named: img2!)
            let imageView2 = UIImageView(image: imagem2)
            imageView2.frame = CGRect(x: 320, y: texto2.frame.origin.y + texto2.frame.size.height, width: 400, height: 250)
            imageView2.contentMode = .ScaleAspectFit

            scrollView.contentSize = CGSizeMake(texto1.frame.size.width, texto1.frame.size.height + imageView.frame.size.height + imageView2.frame.size.height + texto2.frame.size.height + 20)

            scrollView.addSubview(imageView2)

        } else {

            scrollView.contentSize = CGSizeMake(texto1.frame.size.width, texto1.frame.size.height + imageView.frame.size.height)

        }

    }

    scrollView.scrollEnabled = true

}


}

问题是:如果img2为nil,则不执行此代码并且我的scrollView设置不正确:

var paragraphStyle2 = NSMutableParagraphStyle()
        paragraphStyle2.alignment = NSTextAlignment.Justified

        var attributedString2 = NSAttributedString(string: txt2!,
            attributes: [
                NSParagraphStyleAttributeName: paragraphStyle2,
                NSBaselineOffsetAttributeName: NSNumber(float: 0)
            ])

        let texto2 = UILabel(frame: CGRectMake(164, imageView.frame.origin.y + imageView.frame.size.height + 20, 697, 200))
        texto2.attributedText = attributedString2
        texto2.numberOfLines = 0
        texto2.font = UIFont(name: "HelveticaNeue-UltraLight", size: 28)
        texto2.lineBreakMode = .ByWordWrapping
        texto2.sizeToFit()

        scrollView.contentSize = CGSizeMake(texto1.frame.size.width, texto1.frame.size.height + imageView.frame.size.height + texto2.frame.size.height + 20)


        scrollView.addSubview(texto2)

为什么会这样?如果我img2不是nil,代码运行没有问题。我添加了一个println来查看if语句中的代码是否已执行,而且是。

所有信息(txtTitulo,txt1,imageView,txt2 ....)都是由另一个视图控制器通过segue发送的。

这个视图,DetailViewController是在StoryBoard中创建的,带有标签和scrollView,其余的是以编程方式添加的,如你所见。

如果这个问题看起来太简单,我很抱歉,但这是我的第一个iOS应用。谢谢你的回答!

1 个答案:

答案 0 :(得分:0)

在评论中,你说:

  

这里的问题是,当发生这种情况时,我的意思是,当img2是   没有,

scrollView.contentSize = CGSizeMake(texto1.frame.size.width, 
  texto1.frame.size.height + imageView.frame.size.height + 
  texto2.frame.size.height + 20) 
  

永远不会被执行。看看我在说什么?

该代码未执行,因为您告诉它不要执行。您有一个if语句,表示if img2 != nil,然后是一个仅在img2 != nil时运行的代码块。现在你问为什么img2 == nil时代码没有被执行?

答案是"因为那就是语句如何运作。"