2014-02-10 51 views
0

傢伙真的很感激,如果有人在這裏可以幫助我很快,我有一個問題,我需要登錄到我的應用程序,然後自動重定向到下一個頁面,這就是真正意義是。重定向從登錄

預先感謝您

#import "ViewController.h" 

@interface ViewController() 

@end 

@implementation ViewController 

- (void)viewDidLoad { 
    [super viewDidLoad]; 

    credentialsDictionary = [[NSDictionary alloc] initWithObjects:[NSArray  arrayWithObjects:@"password", @"1234", nil] forKeys:[NSArray arrayWithObjects:@"username",@"amit", nil]]; 
    // Do any additional setup after loading the view, typically from a nib. 
    [_webview1 loadRequest:[NSURLRequest requestWithURL: [NSURL URLWithString:@"http://nutriments.info/test1/test3.php"]]]; 
} 
- (IBAction)dismiss:(id)sender { 
    [sender resignFirstResponder]; 
} 
- (IBAction)dismiss1:(id)sender { 
    [sender resignFirstResponder]; 
} 
- (IBAction)dismiss2:(id)sender { 
    [sender resignFirstResponder]; 
} 
- (IBAction)dismiss3:(id)sender { 
    [sender resignFirstResponder]; 
} 
- (IBAction)dismiss4:(id)sender { 
    [sender resignFirstResponder]; 
} 

- (IBAction)enterCredentials { 
    if ([[credentialsDictionary objectForKey:usernameField.text]isEqualToString:passwordField.text]) { 
     UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Correct Password" message:@"This password is correct." delegate:self cancelButtonTitle:@"Dismiss" otherButtonTitles:nil] 
     [alert show]; 
    } 
    else { 
     UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Incorrect Password" message:@"This  password is incorrect." delegate:self cancelButtonTitle:@"Dismiss" otherButtonTitles:nil]; 
     [alert show]; 
    } 
} 


- (void)didReceiveMemoryWarning { 
    [super didReceiveMemoryWarning]; 
    // Dispose of any resources that can be recreated. 
} 
- (IBAction)sendtoadmin:(id)sender { 
    MFMailComposeViewController *mailContoller = [[MFMailComposeViewController alloc]init]; 
    [mailContoller setMailComposeDelegate:self]; 
    NSString *email = @"******@hotmail.com"; 
    NSArray *emailArray = [[NSArray alloc]initWithObjects:email, nil]; 
    NSString *message = [@[_textview.text, _textview1.text,  _textview2.text]componentsJoinedByString: @"\n"]; 
    [mailContoller setMessageBody:message isHTML:NO]; 
    [mailContoller setToRecipients:emailArray]; 
    [mailContoller setSubject:@"Query"]; 
    [self presentViewController:mailContoller animated:YES completion:nil]; 
} 

-(void)touchesBegan4:(NSSet *)touches withEvent:(UIEvent *)event { 
    [[self textview] resignFirstResponder]; 
} 

@end 
+0

我不明白的地方要被重定向? – user2277872

+1

爲此,您需要從您的登錄服務/ API進行回調。我的代碼中看不到任何內容。 – AncAinu

+1

爲什麼你有5個IBActions做同樣的事情?... – JMarsh

回答

1

您可以使用pushViewController:animated:completion:self.navigationController推視圖。 如果您在enterCredentials函數的「成功」部分使用此方法,則可以自動重定向視圖。例如: -

- (IBAction)enterCredentials { 
    if ([[credentialsDictionary objectForKey:usernameField.text]isEqualToString:passwordField.text]) 
    { 
     InsertNextViewControllerClassHere *nextViewController = [[InsertNextViewControllerClassHere alloc] init]; 
     [self.navigationController pushViewController:nextViewController animated:YES completion: nil]; 
    } 
    else { 
     UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Incorrect Password" message:@"This  password is incorrect." delegate:self cancelButtonTitle:@"Dismiss" otherButtonTitles:nil]; 
     [alert show]; 
    } 
} 

你可能想保存在NSUserDefaults的你一個變量is_logged_in了。 這是通過下面的代碼完成:

NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults]; 
[userDefaults setBool:YES forKey:@"is_logged_in"]; 
[userDefaults synchronize]; 

然後,創建一個新的方法- viewWillAppear這將使用檢查你剛纔設置的布爾:

NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults]; 
if ([userDefaults boolForKey:@"is_logged_in"]) 
{ 
    InsertNextViewControllerClassHere *nextViewController = [[InsertNextViewControllerClassHere alloc] init]; 
    [self.navigationController pushViewController:nextViewController animated:YES completion: nil]; 
} 

請注意,這是不安全將所有密碼保存在設備上。 (看起來沒有加密) 您可能需要將這部分移動到服務器,並在發送密碼之前對其進行加密。

+0

謝謝我會試試這個,我一定會接受你的答案,一旦我檢查。 – user3293705

+0

嗨,對不起,這聽起來很愚蠢,但沒有一個更簡單的方法,讓它進入下一頁? – user3293705

+0

只需使用推動視圖控制器(InsertNextViewControllerClassHere * nextViewController)和(self.navigationController pushViewController:nextViewController)的兩行。 – Ramon

0

要出示您的登錄視圖使用

UIViewController myLoginViewController = [[MyLoginViewController alloc] initwithNibNamed:"MyLoginViewController"]; 
[myTabViewController presentModalViewController:myLoginViewController animated:YES]; 

隱藏它使用

//This should be done from the original View Controller i.e. myTabViewController preferably in a delegate called by the modal view controller. 
[self dismissModalViewControllerAnimated:YES];