簡體   English   中英

檢查用戶輸入后的webview

[英]webview after checking user input

我是一個xcode菜鳥,但這是我正在尋找的內容,然后是我所做的事情。 我創建了一個登錄名,並使用該登錄名usernameField嘗試驗證用戶並僅打開特定於該人的網頁。 登錄正常,這是我所有的代碼

//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

任何幫助是極大的贊賞

您的問題是,您永遠不會為T分配任何東西,因此它在您的方法中為nil

在LoginViewController的代碼中,您需要將用戶名傳遞給WebViewController。 您可以通過實現prepareForSegue方法來實現。

這是一個示例,摘自如何傳遞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