Xcode Swift:登录/注册页面流程防止滑动SideBar功能

时间:2015-01-10 03:25:26

标签: ios xcode swift xcode6 uiapplicationdelegate

我正在使用xcode / swift创建应用程序,同时实现滑动侧边栏菜单以及登录/注册过程。完成登录或注册后,可立即在下一页上访问滑动侧边栏菜单功能。

我遵循的侧边栏滑动教程是:Ray Wenderlich - Sidebar Tutorial

问题是 - >仅当包含视图是打开应用程序时显示的第一个视图时,我的侧边栏菜单功能才有效。有时这是可以的,因为一旦用户登录,侧边栏菜单视图就会显示第一个视图 - 但情况并非总是如此 - 注销用户必须先完成注册/登录才能访问/转换到这个视图,从而防止侧边栏菜单功能执行。

我的猜测是这与在我的AppDelegate.swift文件中设置rootviewcontroller VS在我的故事板中设置应用程序的初始视图有关。它也可能与我在ContainerViewController.swift文件中将rootviewcontroller设置为centerviewcontroller有关。

AppDelegate.swift

class AppDelegate: UIResponder, UIApplicationDelegate {

var window: UIWindow?

func application(application: UIApplication!, didFinishLaunchingWithOptions launchOptions: NSDictionary!) -> Bool {
    window = UIWindow(frame: UIScreen.mainScreen().bounds)

    let containerViewController = ContainerViewController()

    window!.rootViewController = containerViewController
    window!.makeKeyAndVisible()

    return true

}

ContainerViewController.swift

class ContainerViewController: UIViewController, CenterViewControllerDelegate, SidePanelViewControllerDelegate, ProfileViewControllerDelegate {

var centerNavigationController: UINavigationController!
var centerViewController: CenterViewController!
var currentState: SlideOutState = .BothCollapsed
var leftViewController: SidePanelViewController!
var profileViewController: ProfileViewController!

let centerPanelExpandedOffset: CGFloat = 60

override func viewDidLoad() {
    super.viewDidLoad()

    centerViewController = UIStoryboard.centerViewController()
    centerViewController.delegate = self

    leftViewController = UIStoryboard.leftViewController()
    leftViewController.delegate = self

    profileViewController = UIStoryboard.profileViewController()
    profileViewController.delegate = self

    // wrap the centerViewController in a navigation controller, so we can push views to it
    // and display bar button items in the navigation bar
    centerNavigationController = UINavigationController(rootViewController: centerViewController)
    view.addSubview(centerNavigationController.view)
    addChildViewController(centerNavigationController)

    centerNavigationController.didMoveToParentViewController(self)
}

LoginViewController.swift

class LoginController: UIViewController,UITextFieldDelegate {

@IBOutlet weak var textUsername: UITextField!
@IBOutlet var textPassword: UITextField!

override func viewDidLoad() {
    super.viewDidLoad()

    // Do any additional setup after loading the view.
}

override func prefersStatusBarHidden() -> Bool {
    return true
}

@IBAction func signinTapped(sender: UIButton) {

    var username:NSString = textUsername.text
    var password:NSString = textPassword.text

    if ( username.isEqualToString("") || password.isEqualToString("") ) {

        var alertView:UIAlertView = UIAlertView()
        alertView.title = "Sign in Failed!"
        alertView.message = "Please enter Username and Password"
        alertView.delegate = self
        alertView.addButtonWithTitle("OK")
        alertView.show()
    } else {

        var post:NSString = "username=\(username)&password=\(password)"

        NSLog("PostData: %@",post);


        // Changed to my URL

        var url:NSURL = NSURL(string:"http://...")!

        var postData:NSData = post.dataUsingEncoding(NSASCIIStringEncoding)!

        var postLength:NSString = String( postData.length )

        var request:NSMutableURLRequest = NSMutableURLRequest(URL: url)
        request.HTTPMethod = "POST"
        request.HTTPBody = postData
        request.setValue(postLength, forHTTPHeaderField: "Content-Length")
        request.setValue("application/x-www-form-urlencoded", forHTTPHeaderField: "Content-Type")
        request.setValue("application/json", forHTTPHeaderField: "Accept")


        var responseError: NSError?
        var response: NSURLResponse?

        var urlData: NSData? = NSURLConnection.sendSynchronousRequest(request, returningResponse:&response, error:&responseError)

        if ( urlData != nil ) {
            let res = response as NSHTTPURLResponse!;

            NSLog("Response code: %ld", res.statusCode);

            if (res.statusCode >= 200 && res.statusCode < 300)
            {
                var responseData:NSString  = NSString(data:urlData!, encoding:NSUTF8StringEncoding)!

                NSLog("Response ==> %@", responseData);

                var error: NSError?

                let jsonData:NSDictionary = NSJSONSerialization.JSONObjectWithData(urlData!, options:NSJSONReadingOptions.MutableContainers , error: &error) as NSDictionary


                let success:NSInteger = jsonData.valueForKey("success") as NSInteger

                //[jsonData[@"success"] integerValue];

                NSLog("Success: %ld", success);

                if(success == 1)
                {
                    NSLog("Login SUCCESS");

                    var prefs:NSUserDefaults = NSUserDefaults.standardUserDefaults()
                    prefs.setObject(username, forKey: "USERNAME")
                    prefs.setInteger(1, forKey: "ISLOGGEDIN")
                    prefs.synchronize()

                    self.performSegueWithIdentifier("login_to_centerview", sender: self)

                }

0 个答案:

没有答案