2015-02-23 17 views
0

我想在用戶插入正確的用戶名和密碼後轉到其他頁面。怎麼做 ? 第一個菜單是ViewController。第二種觀點是Utama。用戶名是從用戶名NSString和密碼獲取從密碼NSStringiOS - 如果有條件,轉到頁面

正確的用戶名是 「Andika」 和正確的密碼是1234

故事板 enter image description here

ViewController.h

#import <UIKit/UIKit.h> 

@interface ViewController : UIViewController 
@property (weak, nonatomic) IBOutlet UILabel *appTittle; 
@property (weak, nonatomic) IBOutlet UITextField *usernameTextField; 
@property (weak, nonatomic) IBOutlet UITextField *passwordTextField; 
- (IBAction)okPressed:(id)sender; 

ViewController.m

#import "ViewController.h" 

@interface ViewController() 

@end 

@implementation ViewController 

    - (void)viewDidLoad { 
     [super viewDidLoad]; 
    } 

    - (void)didReceiveMemoryWarning { 
     [super didReceiveMemoryWarning]; 
    } 

    - (IBAction)okPressed:(id)sender { 

     NSString *username = [_usernameTextField text]; 
     NSString *password = [_passwordTextField text]; 

     if([username compare:@"andika"] && [password compare:@"1234"]) { 
       //???? 
     } 

    } 
@end 

Utama.m

#import "Utama.h" 

@interface Utama() 

@end 

@implementation Utama 

- (void)viewDidLoad { 
    [super viewDidLoad]; 
} 

- (void)didReceiveMemoryWarning { 
    [super didReceiveMemoryWarning]; 
} 

/* 
#pragma mark - Navigation 

// In a storyboard-based application, you will often want to do a little preparation before navigation 
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender { 
    // Get the new view controller using [segue destinationViewController]. 
    // Pass the selected object to the new view controller. 
} 
*/ 

更新:對不起,我是新手。我仍然不能去另一個頁面。

我給這樣 enter image description here

該標識符修改後的代碼在ViewController.m enter image description here

+1

如果比較字符串,你應該使用這個方法isEqualToString: – 2015-02-23 07:55:44

+0

好的,謝謝重要信息 – 2015-02-23 08:14:54

回答

1

萬達是一個的UITabBarController右!?如果你想去上單擊確定第零標籤,給喜歡「DummyTab」到萬達視圖的標識符,並使用下面的代碼:

UITabBarController *controller = [self.storyboard instantiateViewControllerWithIdentifier:@"DummyTab"]; 
[self.navigationController pushViewController:controller animated:YES]; 
[controller setSelectedIndex:0]; 
+0

仍然不能去其他頁面。你可以在我的問題中看到我的更新信息 – 2015-02-23 08:12:42

+0

你是否嘗試使用isEqualtoString而不是比較!? – 2015-02-23 08:44:21

+0

仍然沒有工作:( – 2015-02-23 09:32:09

0

使用此代碼段來解決你的問題:

Utama *controller = [self.storyboard instantiateViewControllerWithIdentifier:@"Utama"]; 
    [self.navigationController pushViewController:controller animated:YES]; 
+0

仍然不能去到另一個頁面。你可以看到我的問題 – 2015-02-23 08:13:58

2

您可以像這樣實例化Utama

Utama *vc = [[UIStoryboard storyboardWithName:@"nameOfStoryboard" bundle:nil] instantiateViewControllerWithIdentifier:@"Utama"]; 

如果ViewControllerUINavigationController,您可以使用

[self.navigationController pushViewController:vc animated:YES] 

但是,如果ViewController是窗口的RootViewController的,你可以設置vcrootViewController

[window setRootViewController:vc]; 

確保window是你的應用程序的關鍵窗口的實例。

+0

我更新的信息還是不能去到另一個頁面,你可以看到在我的問題我更新的信息 – 2015-02-23 08:13:20

+0

這個答案是一個很好的一,IMO,使用導航控制器比使用segue更好,尤其是對於新手來說,爲了使它成功,你必須使用'UINavigationController'並設置你的'ViewController'的根視圖控制器 – 2015-02-23 08:43:38

+0

我添加錯誤「[窗口setRootViewController:VC];」 有人說,沒有確定窗口 – 2015-02-23 09:46:27

0

您需要嵌入i的導航控制器。E添加一個導航控制器,並將其設置爲初始視圖控制器和設置登錄視圖控制器作爲根視圖控制器用於導航控制器,然後在成功的憑證檢查可以推到其他視圖控制器如下

- (IBAction)okPressed:(id)sender { 

    NSString *username = [_usernameTextField text]; 
    NSString *password = [_passwordTextField text]; 

    if([username compare:@"andika"] && [password compare:@"1234"]) { 
[self.navigationController pushViewController:[self.storyboard instantiateViewControllerWithIdentifier:@"Utama storyboard identifier" animated:YES]; 
//also remove login view from stack 
[self removeFromParentViewController]; 
} 
} 
+0

仍然無法進入其他頁面。您可以看到我的更新信息在我的問題 – 2015-02-23 09:32:31

0
- (IBAction)okPressed:(id)sender 
{ 

    NSString *username = [_usernameTextField text]; 
    NSString *password = [_passwordTextField text]; 

    if ([username isEqualToString:@"andika"] == YES && [password isEqualToString:@"1234"] ==YES) 
    { 
     Utama *NextView = [[Utama alloc]initWithNibName:@"Utama" bundle:nil]; 
     [self.navigationController pushViewController:NextView animated:YES]; 
    } 

}

+0

仍然不能。我不知道 :( – 2015-02-23 11:39:06