2014-12-06 54 views
1

我能夠從PhotoLibrary中選擇圖片,但是我的UIButton的背景圖片不會改變。 didFinishPickingImage和imagePickerControllerDidCancel中的println不顯示在控制檯中,所以我不認爲這些函數正在被調用。未被UIImagePicker設置的Swift UIButton背景圖片

class AddTeamTableViewController: UITableViewController, UITextFieldDelegate, UINavigationControllerDelegate, UIImagePickerControllerDelegate, UIAlertViewDelegate { 
var picker:UIImagePickerController? = UIImagePickerController() 
@IBOutlet var teamNumber: UITextField! 
@IBOutlet var schoolName: UITextField! 
@IBOutlet var teamImage: UIButton! 
@IBAction func cancelButton(sender: AnyObject) { 
    dismissViewControllerAnimated(true, completion: nil) 
} 
@IBAction func addTeam(sender: AnyObject) { 
    var newTeam = Team() 
    var onlineTeam = PFObject(className: "Team") 
    // add new team to the list 
    newTeam.name = teamNumber.text 
    newTeam.schoolName = schoolName.text 
    teamList.append(newTeam) 
    // add online 
    onlineTeam["name"] = newTeam.name 
    onlineTeam["fromUser"] = PFUser.currentUser() 
    onlineTeam["schoolName"] = newTeam.schoolName 
    onlineTeam.save() 
    //close the view 
    dismissViewControllerAnimated(true, completion: nil) 
} 
override func viewDidLoad() { 
    super.viewDidLoad() 

    // Uncomment the following line to preserve selection between presentations 
    // self.clearsSelectionOnViewWillAppear = false 
    // Uncomment the following line to display an Edit button in the navigation bar for this view controller. 
    // self.navigationItem.rightBarButtonItem = self.editButtonItem() 
} 

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

// MARK: - Table view data source 

override func numberOfSectionsInTableView(tableView: UITableView) -> Int { 
    // #warning Potentially incomplete method implementation. 
    // Return the number of sections. 
    return 1 
} 

override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
    // #warning Incomplete method implementation. 
    // Return the number of rows in the section. 
    return 1 
} 

override func touchesBegan(touches: NSSet, withEvent event: UIEvent) { 
    self.view.endEditing(true) 
} 
func textFieldShouldReturn(textField: UITextField) -> Bool { 
    textField.resignFirstResponder() 
    return true; 
} 
@IBAction func pickImage(sender: AnyObject) { 
    var image = UIImagePickerController() 
    image.delegate = self 
    var alert:UIAlertController = UIAlertController(title: "Choose Image", message: nil, preferredStyle: UIAlertControllerStyle.ActionSheet) 
    var cameraAction = UIAlertAction(title: "Camera", style: UIAlertActionStyle.Default) { 
     UIAlertAction in 
     self.openCamera() 
    } 
    var galleryAction = UIAlertAction(title: "Gallery", style: UIAlertActionStyle.Default) { 
     UIAlertAction in 
     self.openGallery() 
    }  
    var cancelAction = UIAlertAction(title: "Cancel", style: UIAlertActionStyle.Cancel) { 
     UIAlertAction in 
    } 
    alert.addAction(cameraAction) 
    alert.addAction(galleryAction) 
    alert.addAction(cancelAction) 
    self.presentViewController(alert, animated: true, completion: nil) 
    /* 
    alert.delegate = self 
    alert.message = "Choose Image Source" 
    alert.addButtonWithTitle("Camera") 
    alert.addButtonWithTitle("Photo Library") 
    alert.show() 
    */ 
    //image.sourceType = UIImagePickerControllerSourceType.Camera 
    //image.allowsEditing = false 
    //self.presentViewController(image, animated: true, completion: nil) 
} 
func openCamera() { 
    if(UIImagePickerController .isSourceTypeAvailable(UIImagePickerControllerSourceType.Camera)) { 
     picker!.sourceType = UIImagePickerControllerSourceType.Camera 
     self.presentViewController(picker!, animated: true, completion: nil) 
    } else { 
     openGallery() 
    } 
} 
func openGallery() { 
    picker!.sourceType = UIImagePickerControllerSourceType.PhotoLibrary 
    self.presentViewController(picker!, animated: true, completion: nil) 
} 
func imagePickerController(picker: UIImagePickerController!, didFinishPickingImage image: UIImage!, editingInfo: [NSObject : AnyObject]!) { 
    println("Image selected") 
    self.dismissViewControllerAnimated(true, completion: nil) 
    teamImage.setBackgroundImage(image, forState: UIControlState.Normal) 
} 
func imagePickerControllerDidCancel(picker: UIImagePickerController!) { 
    println("picker cancel") 
} 
} 
+0

當您在設置圖像之前設置按鈕的內容和大小? – elkraneo 2014-12-06 23:13:48

回答

1

按鈕似乎並不初始化

let teamImage: UIButton = UIButton(frame: CGRect(x: 0, y: 0, width: 380, height: 300)) 
let imageTest = UIImage(named: "Hypnotoad") 

teamImage.setTitle("HypnotoadTitle", forState: .Normal) 
teamImage.setBackgroundImage(imageTest, forState: .Normal) 

在操場測試這一點,工作正常。

Playground test of UIButton with backgroundImage

+0

非常感謝您的時間和建議。我已經在Main.storyboard中設置了按鈕大小和初始背景圖像,但是我嘗試了您的建議並使用了您使用的程序代碼。但是,不,我可以設置背景圖像,如果我設置圖像,但我希望用戶能夠從他們的照片庫或相機中選擇圖像文件,並且它不會更改爲用戶選擇的圖像。 – 2014-12-07 06:27:14

+0

嘗試設置「UIImagePickerController」的委託。在你的代碼「openGallery」中設置'picker?.delegate = self' – elkraneo 2014-12-07 09:23:40

0

我想通了。我有2個不同類型的UIImagePickerController變量,我保存到錯誤的一個。我刪除了第一個var picker聲明,並將代碼合併到一個UIImagePickerController中,並按預期工作。