I'm attempting a simple sign out function where the view returns to my login/sign up view after the user signs out. I have tried using these two methods, but the application crashes every time.
First Method:
#IBAction func signPressed(sender: AnyObject) {
PFUser.logOut()
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let vc = storyboard.instantiateViewControllerWithIdentifier("ViewController") as! UIViewController
presentViewController(vc, animated: true, completion: nil)
}
Second Method:
I created the following function:
func loginSetup() {
if (PFUser.currentUser() == nil) {
let vc = ViewController()
self.presentViewController(vc, animated: true, completion: nil)
}
}
then added it to my sign out function:
#IBAction func signPressed(sender: AnyObject) {
PFUser.logOut()
self.loginSetup()
}
both crashed and gave me an app delegate error.. whats the issue here?
here is screenshot of error given :
https://40.media.tumblr.com/1f044ecbdd5059836b0a360d16af9846/tumblr_nqzsobll0o1tupbydo1_1280.png
Providing you've set the root VC to the Login/Sign Up screen.
func didTapSignOut(sender: AnyObject)
{
PFUser.logOut()
self.navigationController?.popToRootViewControllerAnimated(true)
}
Then you can call this function in a UIButton IBAction but I call it on my UINavigationBar like so,
self.navigationItem.rightBarButtonItem = UIBarButtonItem(title: "Sign Out", style: .Plain, target: self, action: "didTapSignOut:")
Related
I'm learning how to make an app with just a code in Swift. I have encountered this problem:
This is action of a button.
#objc func answerAction() {
let story = UIStoryboard(name: "Main", bundle: nil)
let controller = story.instantiateViewController(withIdentifier: "AccountViewController") as! AccountViewController
self.present(controller, animated: true, completion: nil)
}
If I press it, it shows this error:
Thread 1: "Could not find a storyboard named 'Main' in bundle NSBundle </Users/mas/Library/Developer/CoreSimulator/Devices/A3BEC6D0-3AA6-4193-A755-1181DD580576/data/Containers/Bundle/Application/C80D5D36-EBD8-44D4-AF14-B64E2E7E5587/AppForTest.app> (loaded)"
As I understand, the problem is that I have deleted Main.storyboard and app cannot reach it. So how I should declare in a answerAction story constant?
#objc func answerAction() {
let controller = AccountViewController()
self.present(controller, animated: true, completion: nil)
}
I have the following problem. I try to learn coding since a few weeks and my actually project is an Login-Page.
The concept is the following:
When I open the App, there is the Login Screen and the User can login. This works so far. After the login the user is directed to another view. There is a logout button. And here is the problem. When I click the button, the user get logged out (I proofed this by printing out the current user and got nil) and is returned again to the login screen. But when he is now clicking on the login button without or with wrong data he is getting again to the screen although it appeared the error message.
Where is my fault?
Here is my code:
import UIKit
import Parse
class ViewController: UIViewController {
func displayAlert(title:String, message:String){
let alert = UIAlertController(title: title, message: message, preferredStyle: UIAlertControllerStyle.alert)
alert.addAction(UIAlertAction(title: "OK", style: .default, handler: { (action) in self.dismiss(animated: true, completion: nil)}))
self.present(alert, animated: true, completion: nil)
}
#IBOutlet weak var benutzer: UITextField!
#IBOutlet weak var passwort: UITextField!
#IBAction func ButtonLogin(_ sender: Any) {
// Hier die Loginfunktion
let activityIndiaktor = UIActivityIndicatorView(frame: CGRect(x: 0, y: 0, width: 50, height: 50))
activityIndiaktor.center = self.view.center
activityIndiaktor.hidesWhenStopped = true
activityIndiaktor.activityIndicatorViewStyle = UIActivityIndicatorViewStyle.gray
view.addSubview(activityIndiaktor)
activityIndiaktor.startAnimating()
UIApplication.shared.beginIgnoringInteractionEvents()
PFUser.logInWithUsername(inBackground: benutzer.text!, password: passwort.text!, block: { (user, error) in
if user == nil {
var errortext = "Unknown Error! Try again!"
if let error = error {
errortext = error.localizedDescription
}
self.displayAlert(title: "Login error", message: errortext)
}else{
print("Login done!")
let viewController:UIViewController = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "Main") as! UIViewController
self.present(viewController, animated: true, completion: nil)
// UIApplication.shared.endIgnoringInteractionEvents()
// activityIndiaktor.stopAnimating()
}
activityIndiaktor.stopAnimating()
UIApplication.shared.endIgnoringInteractionEvents()
return
})
}
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.
}
}
And here for the other view:
import UIKit
import Parse
class MainViewController: UIViewController {
#IBAction func Logout(_ sender: Any) {
PFUser.logOut()
let viewController:UIViewController = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "LoginSeite") as! UIViewController
self.present(viewController, animated: true, completion: nil)
}
#IBOutlet weak var LabelWillkommen: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
let user = String(describing: PFUser.current()!.username!)
LabelWillkommen.text = "Welcome \(user)!"
// Do any additional setup after loading the view.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
In your MainViewController Just change your logout button action with below code snippet
#IBAction func Logout(_ sender: Any) {
PFUser.logOut()
if PFUser.current()?.sessionToken == nil {
let viewController:UIViewController = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "LoginSeite") as! UIViewController
self.present(viewController, animated: true, completion: nil)
}
}
Hope this will help you
I checked everything again. I couldn't find the mistake. The Logout works but the if case still thinks, the user is logged in. Can I maybe reload the View or reload the if statement or something like that? Or other ideas?
I have that error when I want open new view and I don't know what does it mean.
I run that code:
func openMenu(){
let storyboard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
let vc: UIViewController = storyboard.instantiateViewControllerWithIdentifier("MenuViewController") as UIViewController
self.presentViewController(vc, animated: true, completion: nil)
}
And the error is:
Warning: Attempt to present on whose view is not in the window hierarchy!
I have that Swift code:Swift
And I want execute with Objective-C the Swift code.
Objective-C
replace the line:
self.presentViewController(vc, animated: true, completion: nil)
with:
dispatch_async(dispatch_get_main_queue(), {
self.presentViewController(vc, animated: true, completion: nil)
})
See if it works!
or
why don't you create a segue and call it using:
override func prepareForSegue(segue: UIStoryboardSegue!, sender: AnyObject!) {
if (segue.identifier == "Load View") {
// pass data to next view
}
}
Once a user signs in to my app, I need to take them to a tab view controller so they can use the app to its fullest potential. I have tried to initiate the TabBarController in the buttons onClick function with no success.
PFUser.logInWithUsernameInBackground(username, password:password) {
(user: PFUser?, error: NSError?) -> Void in
if error == nil {
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let vc = storyboard.instantiateViewControllerWithIdentifier("KlikurHomeTabs") as! UITabBarController
self.presentViewController(vc, animated: true, completion: nil) // this shows it modally
} else if error!.code == 101 {
var invalidLogin:UIAlertView = UIAlertView(title: "Please try again", message: "The username password combo you have us does not match our records, please try again or reset your password.", delegate: self, cancelButtonTitle: "Try again")
invalidLogin.show()
}
}
Can anybody spot what I am doing wrong? I have no clue and have been trying for a while now. Thanks :)
Just change this line and also take out the storyboard line of code
let vc = storyboard.instantiateViewControllerWithIdentifier("KlikurHomeTabs") as! UITabBarController
To
let vc = self.storyboard.instantiateViewControllerWithIdentifier("KlikurHomeTabs")
self.presentViewController(vc!, animated: true, completion: nil)
This is more of an elaboration on Paulw11's comment, but here is what I am doing. I have a "splash" view controller that has 2 storyboard segues, one to a signin/signup view controller, and one to my main tabview controller. Inside viewDidAppear, I check the login status and then perform one of the two segues. Here is an example of the code.
override func viewDidAppear(animated: Bool) {
if needsLogin {
performSegueWithIdentifier("SignIn/SignUP", sender: self)
} else {
performSegueWithIdentifier("MainTabBar", sender: self)
}
}
I have the following IBActions on my navigation bar
#IBAction func logoutPressed(sender: AnyObject) {
SweetAlert().showAlert("Are you sure?", subTitle: "Do you really want to logout?", style: AlertStyle.Warning, buttonTitle:"Cancel", buttonColor:UIColorFromRGB(0xD0D0D0) , otherButtonTitle: "Yep", otherButtonColor: UIColorFromRGB(0xDD6B55)) { (isOtherButton) -> Void in
if isOtherButton == false {
let popTime = dispatch_time(DISPATCH_TIME_NOW, Int64( Double(NSEC_PER_SEC) * 4.0 ))
SwiftSpinner.show("Logging out")
dispatch_after(popTime, dispatch_get_main_queue(), {
PFUser.logOut()
self.performSegueWithIdentifier("logoutSegue", sender: nil)
SwiftSpinner.hide()
})
}
else {
}
}
}
//OPTIONS MENU
#IBAction func optionsPressed(sender: AnyObject) {
let alert = SCLAlertView()
alert.addButton("Submit Feedback"){
var subjectText = "feedback"
var toRecipient = ["some email address"]
var mc:MFMailComposeViewController = MFMailComposeViewController()
mc.mailComposeDelegate = self
mc.setSubject(subjectText)
mc.setToRecipients(toRecipient)
self.presentViewController(mc, animated: true, completion: nil)
}
alert.addButton("About") {
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let vc = storyboard.instantiateViewControllerWithIdentifier("about") as UIViewController
self.presentViewController(vc, animated: true, completion: nil)
}
and so on....
And this works perfectly fine when I launch the app to the view controller containing these IBActions.
However, the problem is when I perform a modal/push transition into the said view controller, the IBAction's aren't being called....
Any ideas?
I just dealt with this issue too, may not be a general solution as for me it was a silly mistake, but the issue was that i had changed the class of the 'view' inside my ViewController:
The highlighted view was named something else until i changed it back to UIView in the identity inspector:
Hope that helps you, or someone else :)