0

I'm creating an Instagram like app, and whenever I dismiss the display alert that is displayed when an image or text is missing, it logs me out and logs me back in. How can I make it so that when I click "ok", only the display alert goes away and doesn't log me out? Here is my image posting view control code:

import UIKit
import Parse
import Foundation
class PostViewControllerPage1: UIViewController, UINavigationControllerDelegate, UIImagePickerControllerDelegate {
    func displayAlert(title:String, error:String) {
        var alert = UIAlertController(title: title, message: error, preferredStyle: UIAlertControllerStyle.Alert)
        alert.addAction(UIAlertAction(title: "OK", style: .Default, handler: { action in
            self.dismissViewControllerAnimated(true, completion: nil)
        }))
        self.presentViewController(alert, animated: true, completion: nil)
    }
    var photoSelected:Bool = false
    @IBOutlet weak var imageToPost: UIImageView!
    @IBAction func chooseImage(sender: AnyObject) {
        var image = UIImagePickerController()
        image.delegate = self
        image.sourceType = UIImagePickerControllerSourceType.PhotoLibrary
        image.allowsEditing = false
        self.presentViewController(image, animated: true, completion: nil)
    }
    func imagePickerController(picker: UIImagePickerController, didFinishPickingImage image: UIImage!, editingInfo: [NSObject : AnyObject]!) {
        println("Image selected")
        self.dismissViewControllerAnimated(true, completion: nil)
        imageToPost.image = image
        photoSelected = true
    }
    @IBOutlet weak var imageDescription: UITextField!
    @IBAction func postImage(sender: AnyObject) {
        var error = ""
        if (photoSelected == false) {
            error = "Please select an image to post"
        } else if (imageDescription.text == "") {
            error = "Please enter a message"
        }
        if (error != "") {
            displayAlert("Cannot Post Image", error: error)
        }
        else {
            var post = PFObject(className: "Post")
            post["Title"] = imageDescription.text
            post.saveInBackgroundWithBlock{(success: Bool, error: NSError?) -> Void in
                if success == false {
                    self.displayAlert("Could Not Post Image", error: "Please try again later")
                } else {
                    let imageData = UIImagePNGRepresentation(self.imageToPost.image)
                    let imageFile = PFFile(name: "image.png", data: imageData)
                    post["imageFile"] = imageFile
                    post.saveInBackgroundWithBlock{(success: Bool, error: NSError?) -> Void in
                        if success == false {
                            self.displayAlert("Could Not Post Image", error: "Please try again later")
                        } else {
                            println("posted successfully")
                        }
                    }
                }
            }
        }
    }
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
    }
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
}

What am I doing wrong? Here is my login code, because I feel like that might be a part of the issue:

import Foundation
import Parse
import UIKit
import Bolts
class LoginViewController: UIViewController, UITextFieldDelegate {      

    @IBOutlet weak var loginStatusLabel: UILabel!        
    @IBOutlet weak var emailTextField: UITextField!        
    @IBOutlet weak var passwordTextField: UITextField!
    @IBOutlet weak var loginButton: UIButton!


    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.

        emailTextField.delegate = self
        passwordTextField.delegate = self
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    @IBAction func loginButtonPress(sender: AnyObject) {

        login(emailTextField.text, password: passwordTextField.text)
    }

    func login(email: String, password: String)
    {

        PFUser.logInWithUsernameInBackground(email, password: password)
            {
            (user: PFUser?, error: NSError?) -> Void in
                if user != nil
                {
                    user!.fetchInBackground()
                    if user!.objectForKey("emailVerified") as! Bool
                    {
                        self.loginStatusLabel.text = "Success!"
                        println("successfulLogin")
                       self.performSegueWithIdentifier("login", sender: self)  
                    }
                    else if !(user!.objectForKey("emailVerified") as! Bool)
                    {
                        self.loginStatusLabel.text = "Verify your email address!"
                    }
                    else // status is "missing"
                    {
                        //TODO: Handle this error better
                        self.loginStatusLabel.text = "Verification status: Missing"
                    }
                }
                else
                {
                    if let errorField = error!.userInfo
                    {
                        self.loginStatusLabel.text = (errorField["error"] as! NSString) as String
                    }
                    else
                    {
                        // No userInfo dictionary present
                        // Help from http://stackoverflow.com/questions/25381338/nsobject-anyobject-does-not-have-a-member-named-subscript-error-in-xcode
                    }

                }
        }
    }

    override func viewDidAppear(animated: Bool) {
        if PFUser.currentUser() != nil {
            self.performSegueWithIdentifier("login", sender: self)
        }
    }

    func textFieldShouldReturn(textField: UITextField) -> Bool {
        textField.resignFirstResponder()
        return true;
    }

    override func viewWillAppear(animated: Bool) {
        self.navigationController?.navigationBarHidden = true
    }

    override func viewWillDisappear(animated: Bool) {
        self.navigationController?.navigationBarHidden = false
    }

}
jarlh
  • 42,561
  • 8
  • 45
  • 63

1 Answers1

0
alert.addAction(UIAlertAction(title: "OK", style: .Default, handler: { action in

        self.dismissViewControllerAnimated(true, completion: nil)

    }))

self.dismissViewControllerAnimated in this line will dismiss view controller that show alert and not an alert itself. So all you have to do is comment or delete this line of code

Zell B.
  • 10,266
  • 3
  • 40
  • 49