简体   繁体   English

检查用户输入后的webview

[英]webview after checking user input

I'm a xcode noob, but here's what I'm looking for followed by what I've done. 我是一个xcode菜鸟,但这是我正在寻找的内容,然后是我所做的事情。 I've created a login and with that login usernameField I'm trying verify the user and open a only a web page specific to that person. 我创建了一个登录名,并使用该登录名usernameField尝试验证用户并仅打开特定于该人的网页。 The login is working here's all my code 登录正常,这是我所有的代码

//LoginViewController.h


    #import <UIKit/UIKit.h>
    @interface LoginViewController : UIViewController
    {
    IBOutlet UITextField *usernameField;
    IBOutlet UITextField *passwordField;
    IBOutlet UIButton *loginButton;   
    IBOutlet UIActivityIndicatorView *loginIndicator;
    }

    @property(nonatomic, strong) UITextField *usernameField;
    @property(nonatomic, strong) UITextField *passwordField;
    @property(nonatomic, strong) UIButton    *loginButton;
    @property(nonatomic, strong) UIActivityIndicatorView *loginIndicator;

    -(IBAction) login: (id) sender;
    @end


//LoginViewController.m


    #import "LoginViewController.h"
    @implementation LoginViewController
    @synthesize usernameField, passwordField, loginButton, loginIndicator;

    -(IBAction) login: (id) sender
    {
    if ([usernameField.text isEqualToString: @"userOne"] && [passwordField.text
    isEqualToString: @"passwordOne"])
    {
    printf("Success")
    //here is where I would like to pass usernameField.text to WebViews
    }
    else if (([usernameField.text isEqualToString: @"userTwo"] && [passwordField.text 
    isEqualToString: @"passwordTwo"])
    {
    printf("Success")
    //here is where I would like to pass usernameField.text to WebViews
    }
    else
    {
    printf("Login Failed")
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Login Failed"
                 message:@"Wrong username and password" delegate:self
                 cancelButtonTitle:@"Done"
                 otherButtonTitles:nil];
    [alert show];
    }
    loginIndicator.hidden=False;
    [loginIndicator startAnimating];
    }
    -(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
    {
    if([[segue identifier] isEqualToString:@"fromLogin"])
    {
        WebViews *wvc = [segue destinationViewController];
        wvc.usernamerField = self.usernameField.text;
    }
    }
    @end

//WebViews.h

    @interface WebViews : UIViewController
    {
    IBOutlet UIWebView *webView;
    NSString *usernameField;
    }
    @property (nonatomic, strong) NSString*usernameField;

    @end

//WebViews.m

 #import "WebViews.h"
    @interface WebViews ()//To be honest I'm not sure what this is for
    @end
    @implementation WebViews
    @synthesize usernameField;

    -(void)viewDidLoad
    {
    [super viewDidLoad];
    NSLog(@"username is = %@", self.usernameField);
    if([T.usernameField.text isEqualToString: @"userOne"])
    {
    [webView loadRequest: [NSURLRequest requestWithURL: [NSURL 
    URLWithString:@"http://www.google.com"]]];
    }
    else if([T.usernameField.text isEqualToString: @"userTwo"])
    {
    [webView loadRequest: [NSURLRequest requestWithURL: [NSURL URLWithString:@"http://www.yahoo.com"]]];
    }
    else
    {
    [webView loadRequest: [NSURLRequest requestWithURL: [NSURL URLWithString:@"http://www.wikipedia.com"]]];
    }

    @end

Any help is greatly appreciated 任何帮助是极大的赞赏

Your problem is that you are never assigning anything to T so it is nil in your method. 您的问题是,您永远不会为T分配任何东西,因此它在您的方法中为nil

In the code for your LoginViewController you need to pass the username to the WebViewController. 在LoginViewController的代码中,您需要将用户名传递给WebViewController。 You can do this by implementing the prepareForSegue method. 您可以通过实现prepareForSegue方法来实现。

Here is an example, taken from How to pass prepareForSegue: an object 这是一个示例,摘自如何传递prepareForSegue:一个对象

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    // Make sure your segue name in storyboard is the same as this line
    if ([[segue identifier] isEqualToString:@"YOUR_SEGUE_NAME_HERE"])
    {
        // Get reference to the destination view controller
        WebViewController *wvc = [segue destinationViewController];

        // Pass any objects to the view controller here, like...
        wvc.username = self.usernameField.text;
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM