2015-02-05 2 views
9

사용자가 추가 버튼을 누르면 팝업됩니다. 경고보기에 이미지를 추가하려면 어떻게합니까?이미지를 경고보기에 추가하십시오.

스택 오버플로에서 참조를 가져온 일부 코드를 추가했습니다. 이미지보기 위해 ... 블루 컬러로 나타나는 이미지와 이미지로 대체됩니다

코드 경고보기

var alert = UIAlertController(title: "Spring Element \(springNumber)", 
      message: "Add spring properties", 
      preferredStyle: .Alert) 

     let saveAction = UIAlertAction(title: "Save", 
      style: .Default) { (action: UIAlertAction!) -> Void in 

       let textField1 = alert.textFields![0] as UITextField 
       self.txtField1.append(textField1.text) 
       self.tableView.reloadData() 

       let textField2 = alert.textFields![1] as UITextField 
       self.txtField2.append(textField2.text) 
       self.tableView.reloadData() 

       println(self.txtField1) 
       println(self.txtField2) 
     } 

     let cancelAction = UIAlertAction(title: "Cancel", 
      style: .Default) { (action: UIAlertAction!) -> Void in 
     } 

     //adding textfield1 
     alert.addTextFieldWithConfigurationHandler { 
      (textField1: UITextField!) -> Void in 
      textField1.placeholder = "Force" 
     } 

     //adding textfield2 
     alert.addTextFieldWithConfigurationHandler { 
      (textField2: UITextField!) -> Void in 
      textField2.placeholder = "Stiffness" 
     } 

     alert.addAction(saveAction) 
     alert.addAction(cancelAction) 

     presentViewController(alert, 
      animated: true, 
      completion: nil) 

코드 저장 버튼을 내

let image = UIImage(named: "springAtWall") 
    saveAction.setValue(image, forKey: "image") 
    alert.addAction(saveAction) 

답변

21

예,을 경고보기에 하위보기로 추가 할 수 있습니다.

var imageView = UIImageView(frame: CGRectMake(220, 10, 40, 40)) 
imageView.image = yourImage 

alert.view.addSubview(imageView) 
+0

내 이미지의 이름으로 yourImage을 교체했다. 확인되지 않은 식별자 'springAtWall'을 사용하면 오류가 발생합니다. 마지막 줄에 오류가 있습니다. 'UIAlerController'에 'addSubView'라는 멤버가 없습니다. –

+0

답을 편집했습니다. alert.view.addSubview를 호출해야합니다. – Christian

+0

그것은 작동합니다. 이 하위보기는 경고보기의 제목 오른쪽에 나타나고 표시된 이미지가 너무 작습니다. 메시지 아래에 배치 할 수있는 방법이 있습니까? 그리고 차원을 증가시키고 싶습니다. 숫자 (220, 10, 40, 40)는 무엇을 나타 냅니까? –

2

스위프트 4 :

var imageView = UIImageView(frame: CGRect(x: 220, y: 10, width: 40, height: 40)) 
imageView.image = <#yourImage#> 
alert.view.addSubview(imageView) 
관련 문제