2014-04-22 35 views
0

我正在使用Xcode 5.1.1中的iOS簡單應用程序。 我是Xcode和Objective-C的新手,所以我有一個小問題。 這是我的: enter image description here在Xcode5中加載不同的視圖控制器

我的應用程序的想法很簡單 - 當用戶第一次啓動應用程序,他看到授權形式。當他進入自己的登錄名和密碼,然後按登錄,我的應用程序使請求服務器,如果一切正常 - 登錄名和密碼是正確的 - 應用程序保存用戶登錄:

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

,並以「這是確定,打開視圖!」。

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

授權是偉大的工作,用戶登錄保存os作品。 但是現在,當用戶啓動應用程序再次,我需要用「這是OK」,如果他已經授權開放的觀點:

- (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上找到關於我的問題的答案,但是我找到並嘗試使用的所有內容都無濟於事。

+0

我想你應該嘗試使用的AppDelegate: ' - (BOOL)申請:(UIApplication的*)應用程序didFinishLaunchingWithOptions:(NSDictionary的*)launchOptions',並做一些檢查在那裏。 因爲我看到你正在做一個' - (void)viewDidLoad',它在ViewController中進行_already authorized_檢查。所以我想你總是打開_Authorization_視圖,如果你被授權,你去_OK_視圖?! – ySiggen

+0

是的,你是對的!嗯..我應該在哪裏放置' - (BOOL)應用程序:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions'? – Evgeny

+0

我在AppDelegate.m中找到它。這是嗎?) – Evgeny

回答

0

我的應用程序還處理一些授權流程(PIN註冊)。我使用兩個故事板。一個用於註冊/登錄進程(Login.storyboard),一個用於連接到服務。

下面的代碼是在應用中:didFinishLaunchingWithOptions:一些初始化後:

// 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代表也宣佈:

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; 
       }]; 
0

保留一個標誌,或在NSUserDefaults中存儲布爾值。這樣你可以跟蹤用戶是否已經被授權,你可以顯示第二個視圖,否則你可以加載登錄表單。

+0

嗯)但我的問題是如何顯示第二個視圖,如果用戶已經授權?) – Evgeny

+0

因此,如果用戶已被授權,你希望你的應用程序以第二個VC開始? – NKB

+0

是的!這是我需要的! ;) – Evgeny

0

代替具有的:

- (void)viewDidLoad { NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults]; NSString *authLogin = [defaults objectForKey:@"SavedUserName"]; if(![authLogin isEqualToString:@""]){ ... } }

我會把:

- (void)viewWillAppear 
{ 
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults]; 
    NSString *authLogin = [defaults objectForKey:@"SavedUserName"]; 
    if(![authLogin isEqualToString:@""]){ 
     // programmatically invoke a push segue to the next view controller 
     [self performSegueWithIdentifier:"pushToMyVC" sender:self]; 
    } 
} 

一定要給予SEGUE故事板ID在Interface Builder。對於其他選項,check out this thread.

不要在此引用我,但我相信viewWillAppear在每次視圖即將顯示時都會被調用。因此,如果您來回導航到此視圖,則需要確保用戶已登錄。

0

只需將第二個VC(不是登錄VC)設置爲初始場景,然後使用委派。事情是這樣的:

在你LoginVC:

@protocol LoginViewControllerDelegate 
    -(void)finishedLoggingInUser:(NSString *)userName; 
@end 


@interface LoginViewController : UIViewController { 
    __unsafe_unretained id <LoginViewControllerDelegate> _delegate; 
} 

@property (nonatomic, assign) id <LoginViewControllerDelegate> delegate; 

然後在LoginVC的實施,斷火的委託方法記錄它們在後:然後在初始VC

[_delegate finishedLoadingUserInfo:@"MyUsername"]; 

,聲明自己是LoginViewController的代理人:

#import "LoginViewController.h" 

@interface MyInitialViewController : UIViewController <LoginViewControllerDelegate> 

並執行在實施委託方法:

#pragma mark - LoginViewController Delegate Method 
-(void)finishedLoggingInUser:(NSString *)userName { 
    // Do something with the user info 
    self.loggedInUserName = userName; 

    [self dismissViewControllerAnimated:YES completion:nil]; 
} 
相關問題