在Xcode5中加载不同的ViewController [英] Load different viewcontroller in Xcode5

查看:89
本文介绍了在Xcode5中加载不同的ViewController的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在研究Xcode 5.1.1中适用于iOS的简单应用程序. 我是Xcode和Objective-C的新手,所以我有一个小问题. 这就是我所拥有的:

I am working over a simple application for iOS in Xcode 5.1.1. I am a newbie in Xcode and Objective-C, so I have a little problem. This is what I have:

我的应用程序的想法非常简单-用户首次启动应用程序时,他会看到授权表.当他输入登录名"和密码"并按登录"时,我的应用程序向服务器发出请求,如果一切正常-登录名和密码正确-应用程序保存用户登录名:

Idea of my application is very simple - when user launch application first time, he see Authorization form. When he enter his Login and Password and press Sign In, my application make request to server, and if everything is ok - login and password are correct - application save user login:

NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
        [defaults setObject:self.txtUsername.text forKey:@"SavedUserName"];
        [defaults synchronize];

并使用"It's Ok!"打开视图.

and opens view with "It's Ok!".

[self performSegueWithIdentifier:@"login_success" sender:self];

授权非常有用,保存用户登录名os也可以. 但是现在,当用户再次启动应用程序时,如果他已经授权,则需要使用确定"打开视图.

Authorization is work great, user login saving os works to. But now, when user launch app again I need to open view with "It's OK", if he already authorized:

- (void)viewDidLoad
{
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    NSString *authLogin = [defaults objectForKey:@"SavedUserName"];
    if(![authLogin isEqualToString:@""]){
         //I think here application must load view with "It's OK!", but how to do this?
         //I tried to use here this - [self performSegueWithIdentifier:@"login_success" sender:self]; but it doesn't works.
    }
}

请帮帮我,如何解决我的问题? P.S.我试图在Google以及StackOverflow上的问题中找到答案,但是我发现并尝试使用的所有方法都无济于事.

Please, help me, how to solve my problem? P.S. I try to find answer on my question in Google and here on StackOverflow, but all that I find and try to use didn't help me.

推荐答案

我的应用程序还处理一些授权流程(PIN注册).我使用两个情节提要.一种用于注册/登录进度(Login.storyboard),另一种用于用户连接到服务时.

My application also handles some authorization flow (PIN-Registration). I work with two storyboards. One for the Registration/Login Progress (Login.storyboard) and one when the user is connected to the service.

以下代码正在应用程序中:didFinishLaunchingWithOptions:经过一些初始化之后:

The following code is under application:didFinishLaunchingWithOptions: after some initializations:

// Set rootview (Check if user is registred)
self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];

if ([ccmpService isRegistered]) {
    UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
    self.window.rootViewController = [storyboard instantiateInitialViewController];

    if (launchOptions) {
        NSDictionary *remoteNotif = [launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey];
        [self handleRemoteNotifications:remoteNotif];
    }
} else {
    UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Login" bundle:nil];
    self.window.rootViewController = [storyboard instantiateInitialViewController];
}

[self.window makeKeyAndVisible];
return YES;



当我想更改情节提要(登录/注销)时,我会调用类似(也在App Delegate中声明:



When i want to change the storyboards (Login / Logout) i call something like (also declared in the App Delegate:

UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
UIViewController *sourcetVC = self.window.rootViewController;
UIViewController *destVC = [storyboard instantiateInitialViewController];

[UIView transitionFromView: sourcetVC.view
                    toView: destVC.view
                  duration: 0.5
                   options: UIViewAnimationOptionTransitionFlipFromRight
                completion: ^(BOOL finished) {
                    self.window.rootViewController = destVC;
                }];

这篇关于在Xcode5中加载不同的ViewController的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆